4

I'm trying to port a GNU sed command to BSD sed (in OSX). The command is:

cat -- "$1" | sed -n -e "\${/^#/H;x;/${tapPrintTapOutputSedPattern}/p;}" \
-e "/${tapPrintTapOutputSedPattern}/{x;/${tapPrintTapOutputSedPattern}/p;b;}" \
-e "/^#/{H;b;}" \
-e "x;/${tapPrintTapOutputSedPattern}/p" \
-e "/^Bail out!/q"

It works on GNU sed, but BSD sed gives this error:

sed: 2: "/^Bail out!/q
": unexpected EOF (pending }'s)

This is the command after the variable expansions, in case it's relevant:

cat -- "test021.tap" | sed -n \
-e "\${/^#/H;x;/^not ok\|^ok \([0-9]\+ \)\?# [tT][oO][dD][oO]\|^Bail out!/p;}" \
-e "/^not ok\|^ok \([0-9]\+ \)\?# [tT][oO][dD][oO]\|^Bail out!/{x;/^not ok\|^ok \([0-9]\+ \)\?# [tT][oO][dD][oO]\|^Bail out!/p;b;}" \
-e "/^#/{H;b;}" \
-e "x;/^not ok\|^ok \([0-9]\+ \)\?# [tT][oO][dD][oO]\|^Bail out!/p" \
-e "/^Bail out!/q"

Any ideas about why/how to fix it?

Cheers!

Raimondi
  • 5,163
  • 31
  • 39
  • Why don't you remove bits and pieces until the problem goes away? You can probably solve it yourself, the same way we would. – John Zwinck Mar 18 '13 at 00:23
  • I've been trying that, but my knowledge of sed is poor and I was hoping that someone more familiar with sed could recognize the issue by simple inspection. I keep trying, though ;) – Raimondi Mar 18 '13 at 01:55
  • UUOC; you don't need to use `cat` here. – Jonathan Leffler Mar 18 '13 at 06:38

1 Answers1

9

Try using newlines instead of a semicolons, at least before the branch commands (b) in the statements. See if this works:

sed -n "
  \${
    /^#/H
    x
    /${tapPrintTapOutputSedPattern}/p
  }
  /${tapPrintTapOutputSedPattern}/{
    x
    /${tapPrintTapOutputSedPattern}/p
    b
  }
  /^#/{
    H
    b
  }
  x
  /${tapPrintTapOutputSedPattern}/p
  /^Bail out!/q
" "$1"
Scrutinizer
  • 9,608
  • 1
  • 21
  • 22
  • I had given up because the sed shipped with the latest OSX is from 2005, but this works perfectly well. Thanks a lot! – Raimondi Mar 21 '13 at 07:06
  • Any idea why using newlines instead of semicolons works? – Adam Michalak Sep 18 '21 at 15:04
  • 1
    Hi @AdamMichalak, POSIX sed requires newlines, rather than semicolons in many cases. Some versions are more to tolerant, while GNU sed basically allows them everywhere. To create a portable sed script, one needs to adhere more to the POSIX specifications: `Editing commands other than {...}, a, b, c, i, r, t, w, :, and # can be followed by a , optional characters, and another editing command.` https://pubs.opengroup.org/onlinepubs/9699919799/utilities/sed.html#tag_20_116_13_03 – Scrutinizer Sep 19 '21 at 11:23