Processing an alternative bash line

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 newlines on each alternate line to concatenate them like

 line1: this is line1 line2: this is line2 line3: this is line3 

How to do this using awk or sed

+4
source share
4 answers
 $ 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 $ 
+3
source

Using sed :

 sed -n '${s/$/:/p};N;s/\n/: /p' inputFile 

For in-place editing with backing up the source file

 sed -n -i~ '${s/$/:/p};N;s/\n/: /p' inputFile 
+2
source

This might work for you:

 sed -i '$!N;s/\n/: /' file 
+2
source
 sed 's/^\(line.*\)/\1:/' filename | paste - - 

And the analogue of Perl:

 perl -ape 's/^(line.+)\n/$1: /' filename 
+1
source

Source: https://habr.com/ru/post/1415674/


All Articles