12

Here is my script:

openscad $1 -D generate=1 -o $1.csg 2>&1 >/dev/null |
sed 's/ECHO: \"\[LC\] //' |
sed 's/"$//' |
sed '$a;' >./2d_$1

That output:

sed: 1: "$a;": command a expects \ followed by text
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • You're redirecting your output to `/dev/null`, how is `sed` going to get anything at all? What is `sed '$a;'` supposed to do? – miken32 May 20 '16 at 22:04
  • @miken32: The errors from `openscad` go to `sed`; the standard output from `sed` goes to `/dev/null`. – Jonathan Leffler May 20 '16 at 22:33
  • @JonathanLeffler doesn't `2>&1 >/dev/null` mean stderr goes to stdout, which goes to null? – miken32 May 20 '16 at 22:34
  • What are you trying to do? Add a line that contains a semicolon at the end of the output? Which variant of `sed` are you using? GNU `sed` allows barbaric constructions like the one you're trying to use — if I've guessed correctly — but standard versions of `sed` (those conforming to the POSIX standard) require `sed -e '$a\'` and then the semicolon in either a second `-e` argument or after a newline in the single quoted string (which can't be shown in comment on SO). – Jonathan Leffler May 20 '16 at 22:37
  • 1
    @miken32: No — see [How to pipe stderr and not stdout?](https://stackoverflow.com/questions/2342826/how-to-pipe-stderr-and-not-stdout/2342841#2342841) – Jonathan Leffler May 20 '16 at 22:38
  • @JonathanLeffler that's counterintuitive! Thanks for the tip. – miken32 May 20 '16 at 22:45
  • I didn't even know there were different sed versions, I'm actually using the Mac OSX one, the same as FreeBSD according to my Internet searches. –  May 25 '16 at 17:18

2 Answers2

12

Your version of sed is not GNU sed which allows what you use. You need to write:

openscad $1 -D generate=1 -o $1.csg 2>&1 >/dev/null |
sed 's/ECHO: \"\[LC\] //' |
sed 's/"$//' |
sed '$a\
;' >./2d_$1

Also, three copies of sed is a little excessive (to be polite); one suffices:

openscad $1 -D generate=1 -o $1.csg 2>&1 >/dev/null |
sed -e 's/ECHO: \"\[LC\] //' \
    -e 's/"$//' \
    -e '$a\' \
    -e ';' >./2d_$1

or:

openscad $1 -D generate=1 -o $1.csg 2>&1 >/dev/null |
sed -e 's/ECHO: \"\[LC\] //' -e 's/"$//' -e '$a\' -e ';' >./2d_$1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 4
    if you are on mac and you want to use gnu sed (you really do). you can install it with `brew install gnu-sed` and run gsed – Josh Beauregard Sep 21 '21 at 16:57
  • For the record, the `i` and `c` commands in `sed` behave similarly. If you require a portable solution, maybe don't write it in `sed`. Awk and Perl let you write simila scripts, albeit with different syntax. Some simple `sed` scripts can be rewritten in Perl with very minor changes; maybe also check out the tool `s2p`. – tripleee Feb 01 '23 at 15:54
1

On MacOS, the following works when trying to use command 'a'

sed '/REGEX/a \
    HELLO_WORLD \
    HOW_ARE_YOU \
' <filePath>
Damodar P
  • 1,283
  • 8
  • 3