Menu

sed 4.1.4 s/regexp/replace/g, regexp mismatch

Help
2005-06-21
2012-07-26
  • Nobody/Anonymous

    I have a stream of data, e.g.:

    11111'222?'22'33?'333'44444'55555'

    I want to insert a \n after those apostrophes that are not preceded by a ?.

    Expected output:

    11111'
    222?'22'
    33?'333'
    44444'
    55555'

    I used the substitution:

    s/([^?])'/\1'\n/g

    but this didn't work... it replaced those that did have a ? in front - I want the opposite.

    Any suggestions?

     
    • Keith Marshall

      Keith Marshall - 2005-06-22

      On my GNU/Linux box, this...

      echo "1111'222?'22'33?'333'44444'555" | sed "s/([^?])'/\1'\n/g"

      produces the effect you are seeking, except that I simply get a literal letter "n" instead of a newline -- this is because I still have GNU sed v3.02, which doesn't interpret C style character escapes.

      Your expression looks correct, but you may have to adjust the quoting and number of escapes (backslashes) -- it will undoubtedly be different on Win32, from what I would use on GNU/Linux. If you use it in a sed script, it should be correct as it stands.

      HTH.

      Keith.

       
    • Nobody/Anonymous

      Keith - you are a star!

      It was literally your comment "adjust the quoting"! I was using the construct:

      sed {s/([^?])'/\1'\n/g} which a scan through the man page indicated I could use (I could have misread).

      I change my construct to be:

      sed "s/([^?])'/\1'\n/g" and it works perfectly.

      I yearn for the day I can return from the dark side that is Windows to the pure light of UNIX :-)

       
MongoDB Logo MongoDB