folky
folky

Reputation: 91

SED groups output

I am trying to find patterns in file using SED command. Somehow my regular expression does not work although i would expect it to work.

Details: Text file aaa.log contains the following data:

007ba20: fa2e1438 fa3e1438 d1af9a57 d0000000  8...8.>.W.......
007ba30: d0006813 d3af9a17 d2000009 d200001d  .h..............
007ba40: d1af9a52 d0004a43 d000505f d3af9b12  R...CJ.._P......
007ba50: d2000009 d20000d3 04100012 04404350  ............PC@.
007ba60: 0440004a 04100001 059a4393 059a0002  [email protected]......
007ba70: 04100005 04100000 04100000 059a4153  ............SA..
007ba80: 059a4154 059a4162 059c40fc 04100012  TA..bA...@......
007ba90: 04404350 0440004a 04100001 059c4392  [email protected][email protected]..
007baa0: 059c40f5 0440a350 0440004a 04404350  [email protected][email protected][email protected]@.
007bab0: 0440004a 04400068 04400000 04100000  [email protected].@...@.....
007bac0: 04100000 059c410f 059e4193 03a00001  .....A...A......
007bad0: 03a04255 03a04292 03a04294 02400000  UB...B...B....@.
007bae0: 02408009 02100000 03a04215 028005b1  [email protected]......

My purpose is to trace all 8 characters patterns start with d1 and d0 followed by 6 any characters and then space

Accordingly, I have formed the following line:

p=".*(d1.{6}|d0.{6})[[:space:]].*"; cat aaa.log | tr '\r\n' ' ' | sed -rn "s/$p$p$p$p$p$p/\1 \2 \3 \4 \5 \6\n/p"

As you can see in aaa.log six group of $p existed. and indeed the output is found:

d1af9a57 d0000000 d0006813 d1af9a52 d0004a43 d000505f

however as there is no 7th $p pattern in aaa.log file, when i use the following syntax :

p=".*(d1.{6}|d0.{6})[[:space:]].*"; cat aaa.log | tr '\r\n' ' ' | sed -rn "s/$p$p$p$p$p$p$p/\1 \2 \3 \4 \5 \6 \7\n/p"

it fails and do not print any output.

I assumes the output is not printed due to the fact that pattern 7 does not exist.

Any idea how to print the matched group as output even if some of the patterns was not found ?

Upvotes: 0

Views: 138

Answers (2)

NeronLeVelu
NeronLeVelu

Reputation: 10039

sed -rn '/ d[01][0-9a-f]{6} / s/\r//p' aaa.log
  • print any line that have your groupe starting with d0/d1 and followed by 6 hexa char in it

sed -rn '/^[[:xdigit:]]{7}:.*[[:space:]]d[01][[:xdigit:]]{6}[[:space:]].*.{16}$/ s/\r//p' aaa.log

  • a more exact selector but a bit heavier

Upvotes: 0

Marc Bredt
Marc Bredt

Reputation: 955

what about grep -o "d1[^ ]\{6\}\|d0[^ ]\{6\}" ?

Upvotes: 1

Related Questions