With GNU awk for RT :
$ printf 'abc}def}ghi\n' | awk -v RS='}' '{ORS=(RT?"}\n":"")}1' abc} def} ghi
with other awks:
$ printf 'abc}def}ghi\n' | awk -v RS='}' -v ORS='}\n' 'NR>1{print p} {p=$0} END{printf "%s",p}' abc} def} ghi
I decided to test all currently existing solutions for functionality and runtime using the input file generated by this command:
awk 'BEGIN{for(i=1;i<=1000000;i++)printf "foo}"; print "foo"}' > file1m
and here is what I got:
1) awk (both awk scripts above had similar results):
time awk -v RS='}' '{ORS=(RT?"}\n":"")}1' file1m
Expected output, time =
real 0m0.608s user 0m0.561s sys 0m0.045s
2) shell loop :
$ cat tst.sh
Expected output, time =
real 1m52.152s user 1m18.233s sys 0m32.604s
3) tr + sed :
$ time tr '}' '\n' < file1m | sed 's/$/}/'
Failed to get the expected result (unwanted added } at the end of the file), timing =
real 0m0.577s user 0m0.468s sys 0m0.078s
Using settings to remove this final unwanted } :
$ time tr '}' '\n' < file1m | sed 's/$/}/; $s/}//' real 0m0.718s user 0m0.670s sys 0m0.108s
4) fold + sed + tr :
$ time fold -w 1000 file1m | sed 's/}/}\n\n/g' | tr -s '\n'
Expected output, time =
real 0m0.811s user 0m1.137s sys 0m0.076s
5) split + sed + cat :
$ cat tst2.sh mkdir tmp$$ pwd="$(pwd)" cd "tmp$$" split -b 1m "${pwd}/${1}" sed -i 's/}/}\n/g' x* cat x* rm -fx* cd "$pwd" rmdir tmp$$ $ time ./tst2.sh file1m
Expected output, time =
real 0m0.983s user 0m0.685s sys 0m0.167s
source share