# Manipulation row order in file

**URL:** <https://community.unix.com/t/manipulation-row-order-in-file/373753>\
**Category:** Shell Programming and Scripting\
**Created:** [September 19, 2018, 7:13am UTC](https://community.unix.com/t/manipulation-row-order-in-file/373753 "2018-09-19T07:13:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![baris35](https://community.unix.com/letter_avatar/baris35/32/6_449c533f586939b6e5f246632a46421f.png) [@baris35](https://community.unix.com/u/baris35)\
**Post date:** [September 19, 2018, 7:13am UTC](https://community.unix.com/t/manipulation-row-order-in-file/373753/1 "2018-09-19T07:13:05Z")

</div>

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:

```nohighlight
-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:

```nohighlight
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

```nohighlight
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.

```nohighlight
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:

```nohighlight
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

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [September 19, 2018, 7:26am UTC](https://community.unix.com/t/manipulation-row-order-in-file/373753/2 "2018-09-19T07:26:28Z")

</div>

Would this come close?

```nohighlight
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

```

---

<div class="post-metadata">

**Author:** ![baris35](https://community.unix.com/letter_avatar/baris35/32/6_449c533f586939b6e5f246632a46421f.png) [@baris35](https://community.unix.com/u/baris35)\
**Post date:** [September 19, 2018, 7:30am UTC](https://community.unix.com/t/manipulation-row-order-in-file/373753/3 "2018-09-19T07:30:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [September 19, 2018, 7:36am UTC](https://community.unix.com/t/manipulation-row-order-in-file/373753/4 "2018-09-19T07:36:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![baris35](https://community.unix.com/letter_avatar/baris35/32/6_449c533f586939b6e5f246632a46421f.png) [@baris35](https://community.unix.com/u/baris35)\
**Post date:** [September 19, 2018, 7:42am UTC](https://community.unix.com/t/manipulation-row-order-in-file/373753/5 "2018-09-19T07:42:43Z")

</div>

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

Many thanks  
Boris

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [September 19, 2018, 9:19am UTC](https://community.unix.com/t/manipulation-row-order-in-file/373753/6 "2018-09-19T09:19:09Z")

</div>

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:

```nohighlight
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:

```nohighlight
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:

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

```

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [September 19, 2018, 9:32am UTC](https://community.unix.com/t/manipulation-row-order-in-file/373753/7 "2018-09-19T09:32:37Z")

</div>

one awk:

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

```
