Manipulation row order in file

Hello,
I am trying to replace the position of each row by the next row.
OS: Ubuntu 18.04, bionic
I'd appreciate your help.

input_file:

-O fileA
wget http://x.y.z./a
-O fileB
wget http://a.b.c./d
-O fileC
wget http://q.f.s/t
..
..
..
-O fileZZ
wget http://r.t.y/u

I expect:

wget http://x.y.z./a
-O fileA
wget http://a.b.c./d
-O fileB
wget http://q.f.s/t
-O fileC
..
..
..
wget http://r.t.y/u
-O fileZZ

or

wget http://x.y.z./a -O fileA
wget http://a.b.c./d -O fileB
wget http://q.f.s/t -O fileC
..
..
..
wget http://r.t.y/u -O FileZZ

I tried different combinations of below command but not as expected.

sed -n '{p;n;};h;p;n;p' input_file

many thanks
Boris

------ Post updated at 06:13 AM ------

Weird method but okay for me:

awk 'NR % 2 == 1' input_file > odd_lines
awk 'NR % 2 == 0' input_file > even_lines
paste -d'\n' even_lines odd_lines > expected_output

many thanks
Boris

Would this come close?

sed -n 'h;n;G;p' file
wget http://x.y.z./a
-O fileA
wget http://a.b.c./d
-O fileB
wget http://q.f.s/t
-O fileC
wget http://r.t.y/u
-O fileZZ
1 Like

Dear Rudic,
Could you please teach me how to catch the fish.
I looked up sed -help before opening this thread, but there was no info regarding n and p and other letters.
I'd appreciate if you would let us know how to find more info regarding functions of this notations

many thanks
Boris

My wisdom comes from the man pages: man sed , plus, infrequently, info sed .
And, DON'T forget, regular consumption of these forums! Many a trick I learned in here. Above little one copies a line into the hold buffer (h), reads the next line (n), appends from the hold buffer (G), and prints (p). It depends on the lines always coming in pairs.

1 Like

Dear Rudic,
It gives expected output. learnt that --help and man have not the same concept.

Many thanks
Boris

If, instead of switching every pair of lines in a file, you wanted to switch every line containing the string wget with the line preceding it, you could try:

ed -s input_file <<-EOF
	g/wget/ .-1m.
	,p
	Q
EOF

And, if after switching the order of those pairs of lines you also want to join them, you could try:

ed -s input_file <<-EOF
	g/wget/ .-1m.\\
		s/^/ /\\
		.-1,.j
	,p
	Q
EOF

or, if as in this example, you don't need to perform any parameter expansions in the here-document:

ed -s input_file <<-"EOF"
	g/wget/ .-1m.\
		s/^/ /\
		.-1,.j
	,p
	Q
EOF
1 Like

one awk:

awk 'FNR%2{f=$0;next}{print $0, f}' myFile
1 Like