I have a file like this
line1
this is line1
line2
this is line2
line3
this is line3
I wanted to use awk or sed to remove trailing newline characters at every alternate line to merge them like this
line1: this is line1
line2: this is line2
line3: this is line3
How do I do it using awk or sed
$ cat input
line1
this is line1
line2
this is line2
line3
this is line3
$ awk 'NR%2==1 {prev=$0} NR%2==0 {print prev ": " $0} END {if (NR%2==1) {print $0 ":"}}' input
line1: this is line1
line2: this is line2
line3: this is line3
$
Using sed
:
sed -n '${s/$/:/p};N;s/\n/: /p' inputFile
For in-place editing with backup of original file,
sed -n -i~ '${s/$/:/p};N;s/\n/: /p' inputFile
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With