linux - How to insert a new line between 2 specific characters -


is there anyway insert new lines in-between 2 specific sets of characters? want insert new line every time }{ occurs in text file, want new line inserted between 2 curly brackets. example }\n{

you can run

sed -i -e 's/}{/}\n{/g' filename.ext

where

  • sed stream editor program
  • -i option edit file filename.ext in place
  • -e means regular expression follows
  • s/}{/}\n{/g regular expression meaning find (g) instances of }{ in every line , replace them }\n{ \n regex new line. if omit g, replace first occurrence of search pattern still in every line.

to test before committing changes file, omit -i option , print result in stdout.

example:

create file:

echo "first }{ last" > repltest.txt 

run

sed -e 's/}{/}\n{/g' repltest.txt 

prints following stdout:

first } { last 

to effect change in same file, use -i option.

to run against stdin instead of file, omit -i , filename in piped command after outputs stdin, example:

cat repltest.txt | sed -e 's/}{/}\n{/g' 

which same sed -e 's/}{/}\n{/g' repltest.txt


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -