How would I join two lines using awk or sed?
I have data that looks like this:
abcd
joinabcd
efgh
joinefgh
ijkl
joinijkl
I need an output like the one below:
joinabcdabcd
joinefghefgh
joinijklijkl
How would I join two lines using awk or sed?
I have data that looks like this:
abcd
joinabcd
efgh
joinefgh
ijkl
joinijkl
I need an output like the one below:
joinabcdabcd
joinefghefgh
joinijklijkl
awk '!(NR%2){print$0p}{p=$0}' infile
You can use printf with a ternary:
awk '{printf (NR%2==0) ? $0 "\n" : $0}'
awk 'BEGIN{i=1}{line[i++]=$0}END{j=1; while (j<i) {print line[j+1] line[j]; j+=2}}' yourfile
No need for sed.
They say imitation is the sincerest form of flattery.
Here's a Perl solution inspired by Dimitre's awk code:
perl -lne 'print "$_$p" if $. % 2 == 0; $p = $_' infile
$_
is the current line
$.
is the line number
Some improvement to the "sed" script above that will take the following:
1008
-2734406.132904
2846
-2734414.838455
4636
-2734413.594009
6456
-2734417.316269
8276
-2734414.779617
and make it :
1008 -2734406.132904
2846 -2734414.838455
4636 -2734413.594009
6456 -2734417.316269
8276 -2734414.779617
the "sed" is : "sed 'h;s/.*//;G;N;s/\n/ /g'"