Find min.max value if matching columns found using AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find min.max value if matching columns found using AWK
# 1  
Old 11-19-2011
Find min.max value if matching columns found using AWK

Input_ File :
Code:
2 3 4 5
1 1 0 1
2 1 -1 1
2 1 3 1
3 1 4 1

6 5 6 6
6 6 6 7
6 7 6 8

5 8 6 7

Desired output :
Code:
2 3 4 5
-1 1 4 1
6 5 6 8
5 8 6 7

Explanation:

If ( $2==$4) then
Minimum value of $1 and $3 =becomes==>col1
Maximum value of $1 and $3 =becomes==> col3

If($1==$3) then
Minimum value of $2 and $4 ==becomes==>col2
Maximum value of $2 and $4 ==becomes==>col4

Else
print as it is.
# 2  
Old 11-19-2011
Quote:
Originally Posted by vasanth.vadalur
Explanation:

If ( $2==$4) then
Minimum value of $1 and $3 =becomes==>col1
Maximum value of $1 and $3 =becomes==> col3

If($1==$3) then
Minimum value of $2 and $4 ==becomes==>col2
Maximum value of $2 and $4 ==becomes==>col4

Else
print as it is.
The following line should solve your problem.
Code:
awk '$2==$4 && $3!=$1{if($1>$3){min=$3;max=$1}else{min=$1;max=$3};$1=min;$3=max}$1==$3 && $2!=$4{if($2>$4){min=$4;max=$2}else{min=$2;max=$4};$2=min;$4=max}1' file

.. however I don't understand how you should get this output, can you elaborate Smilie
Quote:
Desired output :
Code:
2 3 4 5
-1 1 4 1
6 5 6 8
5 8 6 7

# 3  
Old 11-19-2011
HI,

Thanks .

Its is simple that i need to check input file and i need to reduce the lines by two ways.


way1: checking column2 and column4 if it is equal, i will replace those line by single line with total 4 columns.

i.e ) column 1 contains min value
col3 contains max. value.
col 2 and col 4 is same

way2 : here as explained in the prev example.


It is actually reducing the records...


Hi, ur code is printing all the values... expected output is less lines as per the explained ways.

---------- Post updated at 11:53 AM ---------- Previous update was at 09:19 AM ----------

To be clear consider the below input_file

Code:
2 3 4 5
1 1 0 1
2 1 -1 1
2 1 3 1
3 1 4 1

here other than first record, all other records are $2 == $4, then i can reduce those $2==$4 records to a single record.

Desired output
Code:
2 3 4 5
-1 1 4 1                 (  =====> HERE -1 is min among $1&$3 and 4 is max among $1&$3)

The desired output contains second line with Col1 is min value(-1) of $2==$4 rows and Col2(4) is max value of $2==$4 rows.
# 4  
Old 11-20-2011
Code:
awk '
END{if(t)print p[1],p[2],p[3],p[4]}
NF{
        t=$1==$3?2:(($2==$4)?1:0)
        if(l!=t){print p[1],p[2],p[3],p[4]}
        if(!t){printf;next}
        u=$t;if($t>$(t+2)){$t=$(t+2);$(t+2)=u}
        if(l==t){$t=$t<p[t]?$t:p[t];$(t+2)=$(t+2)>p[$(t+2)]?$(t+2):p[$(t+2)]}
        split($0,p);l=t
}' file


Last edited by danmero; 11-21-2011 at 10:17 PM.. Reason: Fix small error
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find min and max time taken from a log file

You have a log file as attached in sample input with various operations and time taken by each of them. Write a script to find the min and max time taken for each operation. Sample output is attached. Sample Input is given as below: operation1,83621 operation2,72321 operation3,13288... (1 Reply)
Discussion started by: Chandan_Bose
1 Replies

2. Shell Programming and Scripting

How to get min and max values using awk?

Hi, I need your kind help to get min and max values from file based on value in $5 . File1 SP12.3 stc 2240806 2240808 + ID1_N003 ID2_N003T0 SP12.3 sto 2241682 2241684 + ID1_N003 ID2_N003T0 SP12.3 XE 2239943 2240011 + ID1_N003 ID2_N003T0 SP12.3 XE 2240077 2241254 + ID1_N003 ... (12 Replies)
Discussion started by: redse171
12 Replies

3. Shell Programming and Scripting

awk script to find min and max value

I need to find the max/min of columns 1 and 2 of a 2 column file what contains the special character ">". I know that this will find the max value of column 1. awk 'BEGIN {max = 0} {if ($1>max) max=$1} END {print max}' input.file But what if I needed to ignore special characters in the... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

4. Shell Programming and Scripting

Get the min avg and max with awk

aaa: 3 ms aaa: 2 ms aaa: 5 ms aaa: 10 ms .......... to get the 3 2 5 10 ...'s min avg and max something like min: 2 ms avg: 5 ms max: 10 ms (2 Replies)
Discussion started by: yanglei_fage
2 Replies

5. UNIX for Dummies Questions & Answers

[Solved] Print a line using a max and a min values of different columns

Hi guys, I already search on the forum but i can't solve this on my own. I have a lot of files like this: And i need to print the line with the maximum value in last column but if the value is the same (2 in this exemple for the 3 last lines) i need get the line with the minimum value in... (4 Replies)
Discussion started by: MetaBolic0
4 Replies

6. Shell Programming and Scripting

to find min and max value for each column!

Hello Experts, I have got a txt files which has multiple columns, I want to get the max, min and diff (max-min) for each column in the same txt file. Example: cat file.txt a 1 4 b 2 5 c 3 6 I want ouput like: cat file.txt a 1 4 b 2 5 c 3 6 Max 3 6 Min 1 4 Diff 2 2 awk 'min=="" ||... (4 Replies)
Discussion started by: dixits
4 Replies

7. Shell Programming and Scripting

How to find the average,min,max ,total count?

Hi , Below is my sample data,I have this 8 column(A,B,C,D,E,F,G,H) in csv file. A , B ,C ,D ,E ,F,G ,H 4141,127337,24,15,20,69,72.0,-3 4141,128864,24,15,20,65,66.0,-1 4141,910053,24,15,4,4,5.0,-1 4141,910383,24,15,22,3,4.0,-1 4141,496969,24,15,14,6,-24.0,-18... (7 Replies)
Discussion started by: vinothsekark
7 Replies

8. UNIX for Dummies Questions & Answers

Awk search for max and min field values

hi, i have an awk script and I managed to figure out how to search the max value but Im having difficulty in searching for the min field value. BEGIN {FS=","; max=0} NF == 7 {if (max < $6) max = $6;} END { print man, min} where $6 is the column of a field separated by a comma (3 Replies)
Discussion started by: Kirichiko
3 Replies

9. Shell Programming and Scripting

how to find min, max dates in a file

hello friends...:-) i need some help i have a file cantain like this Star1 ,NetWork,09/02/2008 Star1 ,NetWork,10/02/2008 Star1 ,NetWork,11/02/2008 Star2 ,NetWork,08/03/2008 Star2 ,NetWork,09/04/2008 Star2 ,NetWork,10/05/2008 i need to find out min, max dates the output look like... (6 Replies)
Discussion started by: gemini106
6 Replies

10. UNIX for Dummies Questions & Answers

How to find whenther given value is in betwwen min and Max in unix shell scripting

Hi I wanted to write a shell script with an if condition Example MinValue=10 MaxValue=30 logvalue = some integer value that script reads from the command line arguement I wanted to check whether log value greater than or equal to10 and less than equal to 30 and proceed with the rest of... (5 Replies)
Discussion started by: pinky
5 Replies
Login or Register to Ask a Question