Print rows in reverse order if values decrease along the column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print rows in reverse order if values decrease along the column
# 1  
Old 03-19-2010
MySQL Print rows in reverse order if values decrease along the column

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

Code:
-----
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 :

Code:
-----
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.
# 2  
Old 03-19-2010
Use the following command
Code:
sort -k 3 input_file

Here input_file will contain your data.
According to the 3rd column value it will sort the rows.
# 3  
Old 03-19-2010
Code:
 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 ^$

# 4  
Old 03-19-2010
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 Smilie

---------- 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?
# 5  
Old 03-22-2010
Code:
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
-----

# 6  
Old 03-22-2010
Another one in perl,

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

and the reverse sort,

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

# 7  
Old 03-29-2010
Thanks summer cherry and Thanks Dennis.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

2. Shell Programming and Scripting

awk command to print only selected rows in a particular column specified by column name

Dear All, I have a data file input.csv like below. (Only five column shown here for example.) Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 3,2,4,5,6 5,3,5,5,6 From this I want the below output Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 where the second column... (4 Replies)
Discussion started by: ks_reddy
4 Replies

3. Shell Programming and Scripting

Remove rows with e column values

Hi All, I have a big file with 232 columns and 9 million rows, I want to delete all rows with same column values in col3 through col232. Also the output should be sorted based on first 2 columns. Here is a reduced example with 6 columns. I want to remove rows with duplicate values in col3... (9 Replies)
Discussion started by: alpesh
9 Replies

4. Shell Programming and Scripting

print in reverse order

Hi, I want to print the item in reverse order such that the output would look like 00 50 50 23 40 22 02 96 Below is the input: 00 05 05 32 04 22 20 69 Video tutorial on how to use code tags in The UNIX and Linux Forums. (5 Replies)
Discussion started by: reignangel2003
5 Replies

5. Shell Programming and Scripting

Compare values in two files. For matching rows print corresponding values from File 1 in File2.

- I have two files (File 1 and File 2) and the contents of the files are mentioned below. - I am trying to compare the values of Column1 of File1 with Column1 of File2. If a match is found, print the corresponding value from Column2 of File1 in Column5 of File2. - I tried to modify and use... (10 Replies)
Discussion started by: Santoshbn
10 Replies

6. Shell Programming and Scripting

comparing column of two different files and print the column from in order of 2nd file

Hi friends, My file is like: Second file is : I need to print the rows present in file one, but in order present in second file....I used while read gh;do awk ' $1=="' $gh'" {print >> FILENAME"output"} ' cat listoffirstfile done < secondfile but the output I am... (14 Replies)
Discussion started by: CAch
14 Replies

7. UNIX for Dummies Questions & Answers

How to print arguments in reverse order?

Hey all, How do I make a script print its arguments in reverse order? Thanks (5 Replies)
Discussion started by: unclepickle1
5 Replies

8. Shell Programming and Scripting

print unique values of a column and sum up the corresponding values in next column

Hi All, I have a file which is having 3 columns as (string string integer) a b 1 x y 2 p k 5 y y 4 ..... ..... Question: I want get the unique value of column 2 in a sorted way(on column 2) and the sum of the 3rd column of the corresponding rows. e.g the above file should return the... (6 Replies)
Discussion started by: amigarus
6 Replies

9. Shell Programming and Scripting

How to manipulate first column and reverse the line order in third and fourth column?

How to manipulate first column and reverse the line order in third and fourth column as follws? For example i have a original file like this: file1 0.00000000E+000 -1.17555359E-001 0.00000000E+000 2.00000000E-002 -1.17555359E-001 0.00000000E+000 ... (1 Reply)
Discussion started by: Max Well
1 Replies
Login or Register to Ask a Question