Sample Header Ad - 728x90

How to collect both full lines, and matching part of line?

2 votes
3 answers
52 views
Is it possible to output both the full line and the matched parts of some line? Suppose I have this input
low [ 0]: 0xffff0000 Interesting description A
hi  [ 0]: 0xffff00a0 Interesting description B
low [ 1]:     0x5000 Interesting description C
hi  [ 1]:     0x6000 Interesting description D
...
hi  :   0x806000 ...
And I would like to extract the hex value as an interesting part, and then the full line as well. I have used paste and 2 grep commands, but it feels really bulky and I would like to avoid process substitution (<()). This is what I got:
paste -d'\n' <(grep    '0x[0-9a-zA-Z]*' "$file") \
             <(grep -o '0x[0-9a-zA-Z]*' "$file")
What's a more, to-the-point way of doing this? I was thinking about awk, but not sure if it's possible to easily grab the matching part and print that (??? below):
/0x[0-9a-zA-Z]*/ { print $0 ; print ??? }
----- Example output:
low [ 0]: 0xffff0000 Interesting description A
0xffff0000 
hi  [ 0]: 0xffff00a0 Interesting description B
0xffff00a0 
low [ 1]:     0x5000 Interesting description C
0x5000 
hi  [ 1]:     0x6000 Interesting description D
0x6000 
...
hi  :   0x806000 ...
0x806000
Asked by Moberg (187 rep)
Dec 14, 2022, 04:33 PM
Last activity: Dec 15, 2022, 12:42 PM