unix - To count quickly until first match and stop in megastring -


i want count number of characters until pattern 030 in megarow (do not read data forward point) such not read whole megarow in in memory. should return 28.

megastring data

48000000fe5a1eda480000000d00030001000000cd010000020000000000000000000000000000000000000000000000000000000200000001000000ffffffff57ea5e55ff640c00585e0000fe5a1eda480000000d00030007000000cd010000010000000000000002000000000000800000000000000000000000 

my initial idea split @ first instance of 030 did not succeed this. not familiar split command's capability read until end of pattern.

how can count until first match?

if megarow in file named megarow_file following:

#!/bin/bash  input=megarow_file search_string="030"  comp_string=""  while ifs= read -r -n1 char     char_count=`expr $char_count + 1`     comp_string="${comp_string}${char}"      comp_string_length=${#comp_string}      if [ $comp_string_length -eq 3 ];        # echo comparing value $comp_string        if [ $comp_string = $search_string ];            # echo match            break        fi     fi      if [ $comp_string_length -gt 3 ];         # echo bigger 3, strip 1st char         comp_string="${comp_string:1:3}"         # echo comparing value $comp_string         if [ $comp_string = $search_string ];             # echo match             break         fi     fi  done < "$input"  count_up_to_comp_string=`expr $char_count - ${#search_string}` echo "length ${search_string} ${count_up_to_comp_string} characters" 

Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -