Reverse match in sed, replace opposite of what was found

23,764
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.

Share:
23,764

Related videos on Youtube

Q Q
Author by

Q Q

Updated on September 18, 2022

Comments

  • Q Q
    Q Q over 1 year

    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?

    • klerk
      klerk almost 10 years
      which language you used for those regexps ?
    • Q Q
      Q Q almost 10 years
      I inserted \'s and put them into sed. Like this -e 's/\(^\([a-zA-Z0-9]\{2\}[\s]\{1\}\)\+\)//g