largest field , awk , help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting largest field , awk , help
# 1  
Old 03-15-2008
largest field , awk , help

Hi All,

My file is like this:

Code:
$ cat max.txt
abcd:1982:a
efghij:1980:e
klmn:1923:k
opqrst:1982:o

I have to find out the largest first field and the corresponding line. i.e

Output required:

Code:
efghij efghij:1980:e
opqrst opqrst:1982:o

HTH,
jkl_jkl
# 2  
Old 03-15-2008
try this code:
Code:
#!/bin/bash

#constant
INFILE="max.txt"

#core script
awk ' BEGIN { OFS=FS=":"; cur=max=0; seen=""}
        {
           cur = length($1)
           if(cur > max ){
              seen = $1 " " $0
           }
           else if(cur == max){
              seen = seen "\n"  $1 " " $0
           }
        }
        END { print seen }'  $INFILE
#exit normally
exit 0

.Aaron
# 3  
Old 03-15-2008
aaron,

It prints only

Code:
opqrst opqrst:1982:o

And not

Code:
efghij efghij:1980:e
opqrst opqrst:1982:o

i.e. if there are 2 longest fields, its printing the last one.
# 4  
Old 03-15-2008
Just add "max=cur" and both lines are printed out as you want.

Code:
awk ' BEGIN { OFS=FS=":"; cur=max=0; seen=""}
        {
           cur = length($1)
           if (cur > max ) {
              seen = $1 " " $0
              max = cur
           } else if (cur == max) {
              seen = seen "\n"  $1 " " $0
           }
        }
        END { print seen }'  $INFILE

# 5  
Old 03-15-2008
Another sol:
Code:
awk '{l=length($1);if(l>=max){a[$1" "$0]=l;max=l}}END{for(i in a)if (a[i]==max)print i}' FS=':' file

# 6  
Old 03-16-2008
Quote:
Originally Posted by jkl_jkl
aaron,

It prints only

Code:
opqrst opqrst:1982:o

And not

Code:
efghij efghij:1980:e
opqrst opqrst:1982:o

i.e. if there are 2 longest fields, its printing the last one.
oh,sorry, I forgot the "max =cur", when I input my code into the reply!

.Aaron
# 7  
Old 11-29-2008
How would you find the lowest field and the corresponding line?

Hi i was wondering how you would do the opposite meaning find the lowest field and the corresponding line?

thanks in advance.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Problem with getting awk to multiply a field by a value set based on condition of another field

Hi, So awk is driving me crazy on this one. I have searched everywhere and read man, docs and every related post Google can find and still no luck. The actual files I need to run this on are sensitive in nature, but it is the same thing as if I needed to calculate weighted grades for multiple... (15 Replies)
Discussion started by: cotilloe
15 Replies

2. Shell Programming and Scripting

awk to update field using matching value in file1 and substring in field in file2

In the awk below I am trying to set/update the value of $14 in file2 in bold, using the matching NM_ in $12 or $9 in file2 with the NM_ in $2 of file1. The lengths of $9 and $12 can be variable but what is consistent is the start pattern will always be NM_ and the end pattern is always ;... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

awk to adjust coordinates in field based on sequential numbers in another field

I am trying to output a tab-delimited result that uses the data from a tab-delimited file to combine and subtract specific lines. If $4 matches in each line then the first matching sequential $6 value is added to $2, unless the value is 1, then the original $2 is used (like in the case of line... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. Shell Programming and Scripting

awk to combine all matching fields in input but only print line with largest value in specific field

In the below I am trying to use awk to match all the $13 values in input, which is tab-delimited, that are in $1 of gene which is just a single column of text. However only the line with the greatest $9 value in input needs to be printed. So in the example below all the MECP2 and LTBP1... (0 Replies)
Discussion started by: cmccabe
0 Replies

5. Shell Programming and Scripting

awk to parse field and include the text of 1 pipe in field 4

I am trying to parse the input in awk to include the |gc= in $4 but am not able to. The below is close: awk so far: awk '{sub(/\|]+]++/, ""); print }' input.txt Input chr1 955543 955763 AGRN-6|pr=2|gc=75 0 + chr1 957571 957852 AGRN-7|pr=3|gc=61.2 0 + chr1 970621 ... (7 Replies)
Discussion started by: cmccabe
7 Replies

6. UNIX for Dummies Questions & Answers

Printf awk: argument to adjust automatically width to the largest value

Hi ! Here is an example of a tab-delimited file with variable width values in field 2 (space separated values). As the real file contains lot of lines, I can adjust manually the width to the largest value in field 2. Does it exist an argument to do it automatically? input.tab:... (4 Replies)
Discussion started by: lucasvs
4 Replies

7. Shell Programming and Scripting

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

8. Shell Programming and Scripting

awk second largest, third largest value

I have two text files like this: file1.txt: 133 10 133 22 133 13 133 56 133 78 133 98 file2.txt: 158 38 158 67 158 94 158 17 158 23 I'm basically trying to have awk check the second largest value of the second column of each text file, and cat it to its own text file. There... (13 Replies)
Discussion started by: theawknewbie
13 Replies

9. Shell Programming and Scripting

awk, comma as field separator and text inside double quotes as a field.

Hi, all I need to get fields in a line that are separated by commas, some of the fields are enclosed with double quotes, and they are supposed to be treated as a single field even if there are commas inside the quotes. sample input: for this line, 5 fields are supposed to be extracted, they... (8 Replies)
Discussion started by: kevintse
8 Replies

10. Shell Programming and Scripting

AWK (how) to get smallest/largest nr of ls -la

Hey, This is a long-shot however, I am stuck with the following problem: I have the output from ls -la, and I want to sort some of that data out by using AWK to filter it. ls -la | awk -f scriptname.awk Input: For example: drwxr-xr-x 3 user users 4096 2010-03-14 20:15 bin/... (5 Replies)
Discussion started by: abciscool
5 Replies
Login or Register to Ask a Question