Sunday, March 16, 2008

Find longest string in a field - AWK


names.txt is file with the following format:

FirstName|LastName|DOB|Location

Intention: Find the longest LastName (2nd field) among all the entries.

$ cat names.txt
Anish|Saikia|1982|India
Jack|King|1978|London
Rajesh|Gupta|1980|India
Steve|Spitzer|1977|Scotland
Lias|Hazarik|1983|India

The Code:


$ awk ' BEGIN { OFS=FS="|"; cur=max=0; seen=""}
{
cur = length($2)
if (cur > max ) {
seen = $2 "(" $1" "$2 " from " $NF")"
max = cur
} else if (cur == max) {
seen = seen "\n" $2 "(" $1" "$2 " from " $NF")"
}
}
END { print seen }' names.txt

Output:
Spitzer(Steve Spitzer from Scotland)
Hazarik(Lias Hazarik from India)

1 comment:

Karan Bohra said...

sed -e '
# --> 1 2 3
# Anish|Saikia|1982|India

#1: change 3rd field to "from"
s/|/\n/2; s/|/\n/2; s/\n.*\n/|from|/

#2: bring out the 2nd field to the left
s/|/\n/; s/|/\n/
s/.*\n\(.*\)\n/\1(&/; s/$/)/; y/\n/|/; y/|/ /

H; 1bend; g

s/(.*\n/\n/; s/(.*//

y/\n_/_\n/; s/[^_]/./g; y/_/\n/

/^\(.*\)\n\1$/beq
/^\(.*\)\n\1./blt

:gt
g; s/^\(.*\)\n.*/\1/; bend

:lt
g; s/.*\n//; bend

:eq
g

:end
h; $!d; g
' names.txt

© Jadu Saikia www.UNIXCL.com