Oneliner for multiline regex s/,\n]/]/g?

0

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?

Bastl

Posted 2012-02-09T09:21:05.200

Reputation: 173

First remove newlines, then use sed.

tr -d '\r\n' | sed -e "s/,\n\]/\]/g"

But now everything is on one long line ? – Bastl – 2012-02-09T09:31:23.673

4"Certain linebreaks"? Which ones? – m0skit0 – 2012-02-09T09:40:37.890

those that occur in the pattern, i.e. surrounded by , and ] – Bastl – 2012-02-10T12:01:50.257

I can post an answer using Perl if you're interested. – m0skit0 – 2012-02-10T12:07:22.043

yes, pls, anything is appreciated, though perl is not at all my favourite ... :-) – Bastl – 2012-02-10T12:14:02.177

Answers

1

This should work:

perl -e "$_ = join('', <>); s/,\n]/,]/g; print;" < input_file > output_file

m0skit0

Posted 2012-02-09T09:21:05.200

Reputation: 1 317

aii, that's a long one-liner, the actual vim command is so simple ... – Bastl – 2012-02-10T13:34:59.597

1Who 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 – 2012-02-10T13:40:14.817

1*** If anyone knows how to read a file in one single scalar local $/ = undef;$string = <FILE>; – Yordan Georgiev – 2012-11-05T19:41:54.390

I learned it in the meantime, but thanks anyway :) – m0skit0 – 2012-11-05T22:45:28.300

1

This might work for you:

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

or this:

sed 'N;s/,\n]/]/;P;D' file

potong

Posted 2012-02-09T09:21:05.200

Reputation: 156

0

multiline replace for all *.c files

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

Yordan Georgiev

Posted 2012-02-09T09:21:05.200

Reputation: 133