This seems to be a question whose answer uses sed or awk.
For a file like:
a
b
c
d
e
How to swap the order of the line pairs, to end up with:
b
a
d
c
e
All lines from the original file need to wind up in the output file.
This seems to be a question whose answer uses sed or awk.
For a file like:
a
b
c
d
e
How to swap the order of the line pairs, to end up with:
b
a
d
c
e
All lines from the original file need to wind up in the output file.
Hi Try this,
awk '{if(NR%2){a=$0}else{print $0"\n"a;a=""}} END {if(a!=""){print a}}' inputfile
awk '{p=$0}getline;{print p}' infile
ruby -ne 'prv=$_;print gets;print prv' file
Shouldn't that be:
ruby -ne 'prv=$_;print if gets;print prv' file
it does not matter since we are iterating the file anyway.
It seems to matters on the last line. Without the if statement I get:
b
a
d
c
nile
If there is an odd number of lines..
I don't have a problem. I am using 1.9.1. (not the latest, but good enough than v1.8++ )
Mostly for fun, I decided to try it with sed:
$ cat swaptxt
one
two
333
fore
555
$
$
$ cat swap
#! /usr/bin/ksh
sed -e '
N;s/\n/&/;tswap
b
:swap
s/^\([^\n]*\)\(\n\)\(.*\)$/\3\2\1/'
$
$
$ ./swap < swaptxt
two
one
fore
333
555
$