Join and awk max column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Join and awk max column
# 8  
Old 01-27-2012
Code:
awk 'NR==FNR{a[$0]=$1;next}{for(i in a){if(a[i]==$1){print i,substr($0,length($1)*2)}}}' file1 file2

--ahamed

Last edited by ahamed101; 01-27-2012 at 10:53 AM.. Reason: Updated the code!
# 9  
Old 01-27-2012
Quote:
Originally Posted by ahamed101
Code:
awk 'NR==FNR{a[$0]=$1;next}{for(i in a){if(a[i]==$1){print i,substr($0,length($1)*2)}}}' file1 file2

--ahamed
What does the updated one do?
# 10  
Old 01-27-2012
Strip the first value from the second file... i.e. the common field

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 11  
Old 01-27-2012
Quote:
Originally Posted by ahamed101
Strip the first value from the second file... i.e. the common field

--ahamed

Ok. You mean it wouldn't print the first value from second file in the final output. Did I get it right?

Also, the max column code you have given me, prints the maximum number of columns but not the maximum value in a column.

All your time is appreciated ahamed!

I owe you a long time for saving a lot of my time.

Thanks in advance.
# 12  
Old 01-27-2012
Yeah thats right...

Anf for the max, try this... I thought you wanted the max column number, my bad!
Code:
awk '{for(i=1;i<=NF;i++){max=$i+0>max?$i+0:max}print max; max=0}' infile

Did I get you wrong again? You want the max value in a column or row?

--ahamed

Last edited by ahamed101; 01-27-2012 at 11:51 AM.. Reason: Updated Code!
# 13  
Old 01-27-2012
Quote:
Originally Posted by ahamed101
Yeah thats right...

Anf for the max, try this... I thought you wanted the max column number, my bad!
Code:
awk '{for(i=1;i<=NF;i++){max=$i+0>max?$i+0:max}print max; max=0}' infile

Did I get you wrong again? You want the max value in a column or row?

--ahamed
OK. Let's think cool.

I have a file like this

x y z 1 2 3
a b c 4 9 22
g h k 2 1 -9

Now, I want to see maximum value in a column. Say, fourth. The answer would be 4. Say fifth, the answer would be 9 and for sixth it would be 22.

I would appreciate, if in addition to the maximum value in a column, is there a way to print the range too?

Thanks ahamed. I doubt if you are the author of awk. :P
# 14  
Old 01-27-2012
Code:
awk '{for(i=1;i<=NF;i++){a[i]=$i+0>a[i]?$i+0:a[i]}}END{for(i=1;i<=length(a);i++){printf (a[i]?a[i]:0)OFS}printf "\n"}1' infile

For the range
Code:
awk '{for(i=1;i<=NF;i++){b[i]=b[i]?b[i]:$i+0;a[i]=$i+0>a[i]?$i+0:a[i]; b[i]=$i+0<b[i]?$i+0:b[i];}}
END{for(i=1;i<=length(a);i++){printf (a[i]?a[i]:0)"/"(b[i]?b[i]:0)OFS}printf "\n"}1' infile

--ahamed

Last edited by ahamed101; 01-27-2012 at 01:19 PM..
This User Gave Thanks to ahamed101 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Join, merge, fill NULL the void columns of multiples files like sql "LEFT JOIN" by using awk

Hello, This post is already here but want to do this with another way Merge multiples files with multiples duplicates keys by filling "NULL" the void columns for anothers joinning files file1.csv: 1|abc 1|def 2|ghi 2|jkl 3|mno 3|pqr file2.csv: 1|123|jojo 1|NULL|bibi... (2 Replies)
Discussion started by: yjacknewton
2 Replies

2. UNIX for Dummies Questions & Answers

Join with awk different column

hi guys, i need help I need to join file2 to file1 when column 3 in my file1 and column 1 in my file2 in the same string file1 AA|RR|ESKIM RE|DD|RED WE|WW|SUPSS file2 ESKIM|ES SUPSS|SS Output AA|RR|ESKIM|ES RE|DD|RED| WE|WW|SUPSS|SS (3 Replies)
Discussion started by: radius
3 Replies

3. Shell Programming and Scripting

Get the MAX value out of a column

I've the following data set. I would like to look at the column 3 and only use the rows which has the max value for column 3 Can we use the awk or sed to achieve it. 10 2 10 100 11 2 20 100 12 2 30 100 13 2 30 100 14 ... (7 Replies)
Discussion started by: rudoraj
7 Replies

4. UNIX for Dummies Questions & Answers

How to use the the join command to join multiple files by a common column

Hi, I have 20 tab delimited text files that have a common column (column 1). The files are named GSM1.txt through GSM20.txt. Each file has 3 columns (2 other columns in addition to the first common column). I want to write a script to join the files by the first common column so that in the... (5 Replies)
Discussion started by: evelibertine
5 Replies

5. Shell Programming and Scripting

AWK script to create max value of 3rd column, grouping by first column

Hi, I need an awk script (or whatever shell-construct) that would take data like below and get the max value of 3 column, when grouping by the 1st column. clientname,day-of-month,max-users ----------------------------------- client1,20120610,5 client2,20120610,2 client3,20120610,7... (3 Replies)
Discussion started by: ckmehta
3 Replies

6. Shell Programming and Scripting

extracting row with max column value using awk or unix

Hello, BC106081_abc_128240811_128241377 7.96301 BC106081_abc_128240811_128241377 39.322 BC106081_cde_128240811_128241377 1.98628 BC106081_def_128240811_128241377 -2.44492 BC106081_abc_128240811_128241377 69.5504 FLJ00075_xyz_14406_16765 -0.173417 ... (3 Replies)
Discussion started by: Diya123
3 Replies

7. Shell Programming and Scripting

loop in awk - column max for each column

Hello all, this should really be easy for you... I need AWK to print column maxima for each column of such input: Input: 1 2 3 1 2 1 1 3 2 1 1 2 Output should be: 2 2 3 3 This does the sum, but i need max instead: { for(i=1; i<=NF; i++) sum +=$i } END {for(i=1; i in sum;... (3 Replies)
Discussion started by: irrevocabile
3 Replies

8. Shell Programming and Scripting

Join multiple files by column with awk

Hi all, I searched through the forum but i can't manage to find a solution. I need to join a set of files placed in a directory (~1600) by column, and obtain an output with first and second column common to each file, but following columns are taken from the file in the list (precisely the fourth... (10 Replies)
Discussion started by: macsx82
10 Replies

9. Shell Programming and Scripting

sed/awk to retrieve max year in column

I am trying to retrieve that max 'year' in a text file that is delimited by tilde (~). It is the second column and the values may be in Char format (double quoted) and have duplicate values. Please help. (4 Replies)
Discussion started by: CKT_newbie88
4 Replies

10. UNIX for Dummies Questions & Answers

Join 2 files with multiple columns: awk/grep/join?

Hello, My apologies if this has been posted elsewhere, I have had a look at several threads but I am still confused how to use these functions. I have two files, each with 5 columns: File A: (tab-delimited) PDB CHAIN Start End Fragment 1avq A 171 176 awyfan 1avq A 172 177 wyfany 1c7k A 2 7... (3 Replies)
Discussion started by: InfoSeeker
3 Replies
Login or Register to Ask a Question