Say I have this output from my syslog,

precalculated src_hash => 20 bytes @ 0x7f811c001e20
A6 2D E5 CD 2A BA F0 42 56 66 19 D4 61 1A E3 A0  .-..*..BVf..a...
C7 5E 5F 77                                      .^_w
precalculated dst_hash => 20 bytes @ 0x7f811c0020d0
15 EC 34 59 89 0F 4F F8 C5 68 3A DC BB 09 27 91  ..4Y..O..h:...'.
5D C1 E7 28                                      ]..(

My goal is to strip off the ASCII junk on the right and be left with only the hex bytes of the hash, while still saving the lines starting with the variable names. The regular expression I'm using is

(^([a-zA-Z_]+\s+)+[=]{1}) | (^([a-zA-Z0-9]{2}[\s]{1})+)

Which correctly matches a line starting with either a variable name or a hex string containing its value. Is there any way I could invert what gets matched from sed to strip out sort of "the other" stuff in the stream? Can it be piped to a file somehow?

  • which language you used for those regexps ?
    – klerk
    Jul 21 '14 at 20:51
  • I inserted \'s and put them into sed. Like this -e 's/\(^\([a-zA-Z0-9]\{2\}[\s]\{1\}\)\+\)//g
    – Q Q
    Jul 21 '14 at 20:56
sed -e '/^precalculated/!s/^\(.\{47\}\).*$/\1/' < h

This keeps first 47 characters on any line not matching precalculated at the beginning. Lines matching are just copied.


I found this pattern:

sed  -e 's/^\(..\s.\{45\}\).*/\1/'

You could match any initial space-separated sequence of pairs of hex digits into a substitution group, and then just re-substitute that group, e.g.

sed -r 's/^(([[:xdigit:]]{2}\s+)+).*$/\1/'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.