How to extract few hex numbers from a line?

0

I have a file containing these lines

Entry :  12300000
F Blocks:   0x00000020 0x00000000 0x000a1b00
S Blocks:   0x00100000 0x0000001c 0x00000150

I would like to extract only the numbers associated to the line starting with F Blocks as string.

Line can be easily extracted using :

sed -n '/F Blocks:/p' filename

However, I am still extract line numbers and check format using regex

blocks="$(sed -n 's/F Blocks:[\t ]\+\([0-9a-f]\+\)[ ]\+\([0-9a-f]\+\)[ ]\+\([0-9a-f]\+\)/0x\1 0x\2 0x\3/p' filename)"

What's going wrong with the given regex ?

EDIT1: Update F Blocks.. line.

regex
sed
asked on Stack Overflow May 28, 2020 by ogs • edited May 28, 2020 by Inian

2 Answers

1

You can optionally match the (0x)? before the number using sed -E and the replacement groups will be 0x\2 0x\4 0x\6/

blocks="$(sed -nE 's/F Blocks:[\t ]+(0x)?([0-9a-f]+)[ ]+(0x)?([0-9a-f]+)[ ]+(0x)?([0-9a-f]+)/0x\2 0x\4 0x\6/p' filename)"

Regex demo | Bash demo

The replacement will look like:

0x00000020 0x00000000 0x000a1b00
answered on Stack Overflow May 28, 2020 by The fourth bird • edited May 28, 2020 by The fourth bird
0

You can do this as well :

grep '^F' input.txt | awk -F':' '
{split($2,numbers, " "); 
  for(ix in numbers) { 
     if(numbers[ix] ~ /0x[0-9]+/){  
         printf("%s ",numbers[ix]) 
     }
  }
}'

And it will print :

0x00000000 0x000a1b00

Regards!

answered on Stack Overflow May 28, 2020 by Matias Barrios

User contributions licensed under CC BY-SA 3.0