Solved sed: 1: ":a;N;s/\n/,/g;ta": unused label 'a;N;s/\n/,/g;ta'

Hi,
I have this line in a script to join all the lines of file and separate them using ,
Code:
#! /bin/sh
list=`wget -qO- https://somesite.com/file | awk NF | sed "a;N;s/\n/,/g;ta"`
When I run the script, it gives me message sed: 1: ":a;N;s/\n/,/g;ta": unused label 'a;N;s/\n/,/g;ta'

What am I doing wrong here?
 
Last edited by a moderator:
How about the following?
Code:
#! /bin/sh
list=`wget -qO- https://somesite.com/file | tr '\n' ','`

Or you specifically want to learn sed functions/labels?
 
How about the following?
Code:
#! /bin/sh
list=`wget -qO- https://somesite.com/file | tr '\n' ','`

Or you specifically want to learn sed functions/labels?
Not necessarily have to be sed, I want the script to work.
That said, i still wonder what is wrong with the line, for the sake of learning.
 
The error message doesn't match the oneliner you've posted. Anyway, label ends with a new line, so everything after “:” is a label. Try sed -e :a -e 'N;s/\n/,/g;ta'.
 
Equivalent Perl code would be something like perl -pe '$/=$1;$_=($_.<>)=~s/\n(?!$)/,/gr'. That's the shortest I could come up with 😈

---
...shortest without using any meaningful words. This one's 4 characters shorter: perl -e '$/=$1;print<>=~s/\n(?!$)/,/gr', but it has alien looking “print” inside.

---
v3.0, another unnecessary space removed, improved code readibility.
perl '-pe$/=$1;$_=($_.<>)=~s/\n(?!$)/,/gr'

---
v4.0, now it looks more mysterious with $4 instead of boring $1.
perl '-pe$/=$4;$_.=<>;s/\n(?!$)/,/g'
 
The error message doesn't match the oneliner you've posted. Anyway, label ends with a new line, so everything after “:” is a label. Try sed -e :a -e 'N;s/\n/,/g;ta'.
This one doesn't seem to work. It turns out the variable list comes empty.
 
Last edited by a moderator:
Yup, looks like FreeBSD's sed exits when there's no more input for command N.

Try this then: sed -ne '1h;2,$H;$g;$s/\n/,/gp'
 
Back
Top