1

I'm using the following sed command:

cat version | sed -e 's/[0-9][0-9][0-9][0-9][0-9:M]*-[abp]/Ver\n/'

Which normalizes like this:

4330M-p  ->  Ver<newline>

This works fine with GNU SED, but with BSD SED on Mac OS X, the \n turns into an n like this:

4330M-p  ->  Vern

To normalize some output that contains a version number that changes over time to a static string, followed by a newline. This is used as part of testing where the output after sed processing is compared via diff with a known golden file and we don't want failures due to the version number changing and further need a newline added for our purposes.

How can I replace with a string that adds a newline in a way that works on both GNU and BSD version of sed, or should I just install GNU sed on Mac OS X because this cannot be done?

WilliamKF
  • 7,778
  • 34
  • 103
  • 145
  • I haven't tried BSD sed, but perhaps something like `\d13` or something like `\x0a`? that works in GNU does it work in BSD? – barlop Jun 03 '15 at 23:11
  • @barlop, Nope, the backslash is eaten by BSD `sed` and you get `Verd13` and `verx0a`. – WilliamKF Jun 03 '15 at 23:17
  • this may be of interest http://stackoverflow.com/questions/1421478/how-do-i-use-a-new-line-replacement-in-a-bsd-sed and http://unix.stackexchange.com/questions/42321/how-can-i-instruct-bsd-sed-to-interpret-escape-sequences-like-n-and-t and http://superuser.com/questions/307165/newlines-in-sed-on-mac-os-x – barlop Jun 03 '15 at 23:26

1 Answers1

0

You can make a literal newline in your script, like so:

cat version | sed -e 's/[0-9][0-9][0-9][0-9][0-9:M]*-[abp]/Ver\
/'
tekknolagi
  • 1,370
  • 3
  • 15
  • 34