Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|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 '12 at 9:31
"Certain linebreaks"? Which ones? – m0skit0 Feb 9 '12 at 9:40
those that occur in the pattern, i.e. surrounded by , and ] – Bastl Feb 10 '12 at 12:01
I can post an answer using Perl if you're interested. – m0skit0 Feb 10 '12 at 12:07
yes, pls, anything is appreciated, though perl is not at all my favourite ... :-) – Bastl Feb 10 '12 at 12:14

This should work:

perl -e "$_ = join('', <>); s/,\n]/,]/g; print;" < input_file > output_file
share|improve this answer
aii, that's a long one-liner, the actual vim command is so simple ... – Bastl Feb 10 '12 at 13:34
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 '12 at 13:40
*** If anyone knows how to read a file in one single scalar local $/ = undef;$string = <FILE>; – YordanGeorgiev Nov 5 '12 at 19:41
I learned it in the meantime, but thanks anyway :) – m0skit0 Nov 5 '12 at 22:45

This might work for you:

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

or this:

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

multiline replace for all *.c files

perl -p -i.bak -e 's#to_find\n\n#to_replace#g' *.c

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .