Extract a known length substring starting from known substring

-1

I am trying to parse a text file with a known format, however each line is not 100% consistent.

Each line of the file contains some data in the format ...x03: 0xDEADBEEF...x04: 0xDEADBEEF...x05: 0xDEADBEEF...ect

I want to be able to extract particular x values from this string and print them to stdout.

I cannot figure out the best way to do this string search, I am sure there is a solution using sed that I haven't figured out, hopefully someone can help.

I have tried a very simple pamaeter expansion using offsets, but found the string wasnt formatted exactly the same on every line.

I have now tried this:

for line in data.txt; do grep -Po 'x10:[^.]*' $line; grep -Po 'x05:[^.]*' $line; grep -Po 'x06:[^.]*' $line;grep -Po 'x07:[^.]*' $line; done

this seems to print all of the x10 followed by all of x05 ect.

string
bash
shell
text
asked on Stack Overflow Nov 27, 2019 by user3552845 • edited Nov 27, 2019 by user3552845

1 Answer

0

This is the solution to my problem.

let count=1
while read line; do
    echo "Test Result: ${count}"
    let count=count+1
    grep -Po 'x10:[^.]*' <<< "$line"
    grep -Po 'x05:[^.]*' <<< "$line"
    grep -Po 'x06:[^.]*' <<< "$line"
    grep -Po 'x07:[^.]*' <<< "$line"
    grep -Po 'x22:[^.]*' <<< "$line"
    grep -Po 'x23:[^.]*' <<< "$line"
    grep -Po 'x24:[^.]*' <<< "$line"
    grep -Po 'x25:[^.]*' <<< "$line"        
    grep -Po 'x26:[^.]*' <<< "$line"
    grep -Po 'x27:[^.]*' <<< "$line"
    echo
done < data.txt
answered on Stack Overflow Nov 27, 2019 by user3552845

User contributions licensed under CC BY-SA 3.0