# Print rows in reverse order if values decrease along the column

**URL:** <https://community.unix.com/t/print-rows-in-reverse-order-if-values-decrease-along-the-column/262042>\
**Category:** Shell Programming and Scripting\
**Created:** [March 19, 2010, 1:24am UTC](https://community.unix.com/t/print-rows-in-reverse-order-if-values-decrease-along-the-column/262042 "2010-03-19T01:24:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sam\_2921](https://community.unix.com/letter_avatar/sam_2921/32/6_449c533f586939b6e5f246632a46421f.png) [@sam\_2921](https://community.unix.com/u/sam_2921)\
**Post date:** [March 19, 2010, 1:24am UTC](https://community.unix.com/t/print-rows-in-reverse-order-if-values-decrease-along-the-column/262042/1 "2010-03-19T01:24:58Z")

</div>

Hi, Guys. Please help me to find solution to this problem using shell scripting.

I have an INPUT file with 4 columns separated by tab. Each block of records is separated by -----

```nohighlight
-----
Sample1 5402 6680 Pattern01
Sample2 2216 2368 Pattern02
Sample3 1932 2122 Pattern09
-----
Sample1 75209 76057 Pattern05
Sample2 76269 76713 Pattern01
Sample3 82346 82510 Pattern07
Sample4 82606 82796 Pattern06
-----
sample1 41587 44548 Pattern90
-----

```

I need to print rows in reverse order if values decrease along the column in a particular Block and to print the Block as it is if there is a single record or the column values are in increasing order.

Required OUTPUT is :

```nohighlight
-----
Sample3 1932 2122 Pattern09
Sample2 2216 2368 Pattern02
Sample1 5402 6680 Pattern01
-----
Sample1 75209 76057 Pattern05
Sample2 76269 76713 Pattern01
Sample3 82346 82510 Pattern07
Sample4 82606 82796 Pattern06
-----
sample1 41587 44548 Pattern90
-----

```

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![thillai\_selvan](https://community.unix.com/user_avatar/community.unix.com/thillai_selvan/32/1831_2.png) [@thillai\_selvan](https://community.unix.com/u/thillai_selvan)\
**Post date:** [March 19, 2010, 1:28am UTC](https://community.unix.com/t/print-rows-in-reverse-order-if-values-decrease-along-the-column/262042/2 "2010-03-19T01:28:32Z")

</div>

Use the following command

```nohighlight
sort -k 3 input_file

```

Here input\_file will contain your data.  
According to the 3rd column value it will sort the rows.

---

<div class="post-metadata">

**Author:** ![rdcwayx](https://community.unix.com/letter_avatar/rdcwayx/32/6_449c533f586939b6e5f246632a46421f.png) [@rdcwayx](https://community.unix.com/u/rdcwayx)\
**Post date:** [March 19, 2010, 2:11am UTC](https://community.unix.com/t/print-rows-in-reverse-order-if-values-decrease-along-the-column/262042/3 "2010-03-19T02:11:01Z")

</div>

```nohighlight
 awk '{if ($0~/^-/) {print $0 ORS b;a=0;b=""}
      else {if ($2>a) {b=b ORS $0}
            else {b=$0 ORS b}
            {a=$2}
            }
     }' INPUT |grep -v ^$

```

---

<div class="post-metadata">

**Author:** ![sam\_2921](https://community.unix.com/letter_avatar/sam_2921/32/6_449c533f586939b6e5f246632a46421f.png) [@sam\_2921](https://community.unix.com/u/sam_2921)\
**Post date:** [March 19, 2010, 3:51am UTC](https://community.unix.com/t/print-rows-in-reverse-order-if-values-decrease-along-the-column/262042/4 "2010-03-19T03:51:32Z")

</div>

Thanks Selwan and rdcwayx. Athough sorting on the 3rd column will not sort within blocks of data but awk code is working fine. Thanks again 🙂

---------- Post updated at 01:21 PM ---------- Previous update was at 12:20 PM ----------

Hi, how can I do the reverse that is print all the rows in decreasing order values in each Block?

---

<div class="post-metadata">

**Author:** ![summer\_cherry](https://community.unix.com/letter_avatar/summer_cherry/32/6_449c533f586939b6e5f246632a46421f.png) [@summer\_cherry](https://community.unix.com/u/summer_cherry)\
**Post date:** [March 22, 2010, 2:17am UTC](https://community.unix.com/t/print-rows-in-reverse-order-if-values-decrease-along-the-column/262042/5 "2010-03-22T02:17:37Z")

</div>

```nohighlight
local $/="-----\n";
while(<DATA>){
	s/-----//;
	if($_ ne "\n" ){
		my @tmp=split("\n",$_);
	  print "-----\n";
		print join "\n", map {$_->[0]} sort {$a->[1]<=>$b->[1]} map {my @t=split; [$_,$t[1]]} @tmp;
		print "\n";
	}
}
__DATA__
-----
Sample1 5402 6680 Pattern01
Sample2 2216 2368 Pattern02
Sample3 1932 2122 Pattern09
-----
Sample1 75209 76057 Pattern05
Sample2 76269 76713 Pattern01
Sample3 82346 82510 Pattern07
Sample4 82606 82796 Pattern06
-----
sample1 41587 44548 Pattern90
-----

```

---

<div class="post-metadata">

**Author:** ![dennis.jacob](https://community.unix.com/user_avatar/community.unix.com/dennis.jacob/32/798_2.png) [@dennis.jacob](https://community.unix.com/u/dennis.jacob)\
**Post date:** [March 22, 2010, 2:52am UTC](https://community.unix.com/t/print-rows-in-reverse-order-if-values-decrease-along-the-column/262042/6 "2010-03-22T02:52:06Z")

</div>

Another one in perl,

```nohighlight
perl -lane 'if (/^-/) { foreach (sort keys(%h)) { print "$h{$_}"; } %h=();print; } else { $h{$F[1]}=$_; }' file

```

and the reverse sort,

```nohighlight
perl -lane 'if (/^-/) { foreach (reverse sort keys(%h)) { print "$h{$_}"; } %h=();print; } else { $h{$F[1]}=$_; }' file

```

---

<div class="post-metadata">

**Author:** ![sam\_2921](https://community.unix.com/letter_avatar/sam_2921/32/6_449c533f586939b6e5f246632a46421f.png) [@sam\_2921](https://community.unix.com/u/sam_2921)\
**Post date:** [March 29, 2010, 1:26am UTC](https://community.unix.com/t/print-rows-in-reverse-order-if-values-decrease-along-the-column/262042/7 "2010-03-29T01:26:34Z")

</div>

Thanks summer cherry and Thanks Dennis.
