Can someone provide a simple one-liner to remove certain line breaks?

In vim I use %s/,\n]/]/g

This should be possible with a very simple one-liner IMHO, but how?

link|improve this question
First remove newlines, then use sed. tr -d '\r\n' | sed -e "s/,\n]/]/g" But now everything is on one long line ? – Bastl Feb 9 at 9:31
4  
"Certain linebreaks"? Which ones? – m0skit0 Feb 9 at 9:40
those that occur in the pattern, i.e. surrounded by , and ] – Bastl Feb 10 at 12:01
I can post an answer using Perl if you're interested. – m0skit0 Feb 10 at 12:07
yes, pls, anything is appreciated, though perl is not at all my favourite ... :-) – Bastl Feb 10 at 12:14
feedback

2 Answers

This should work:

perl -e "$_ = join('', <>); s/,\n]/,]/g; print;" < input_file > output_file
link|improve this answer
aii, that's a long one-liner, the actual vim command is so simple ... – Bastl Feb 10 at 13:34
1  
Who cares? It works... The problem actually is the file input (<>). I have to convert the resulting array read from the file to a single line to process ($_ = join('', <>);) because file is splitted precisely by "\n". If anyone knows how to read a file in one single scalar, I'd be glad to modify it into something shorter. – m0skit0 Feb 10 at 13:40
feedback

This might work for you:

sed ':a;$!{N;ba};s/,\n]/]/g' file

or this:

sed 'N;s/,\n]/]/;P;D' file
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.