Monday, August 31, 2009

Sort strings by length using awk and bash


Input file:

$ cat usragnt.txt
Win3.11 for Windows 3.11
WinNT3.51 for Windows NT 3.11
WinNT4.0 for Windows NT 4.0
Windows NT 5.0 for Windows 2000
Windows NT 5.1 for Windows XP
Windows NT 5.2 for Windows Server 2003; Windows XP x64
Windows NT 6.0 for Windows Vista
Win95 for Windows 95
Win98 for Windows 98
Win 9x 4.90 for Windows Me
WindowsCE for Windows CE


Required: I was trying to find the length of the longest useragent value from the above file.
This is how we can sort a particular field based on its length.

The awk one liner:

$ awk '{ print length($0),$0 | "sort -n"}' usragnt.txt

Output:

20 Win95 for Windows 95
20 Win98 for Windows 98
24 Win3.11 for Windows 3.11
24 WindowsCE for Windows CE
26 Win 9x 4.90 for Windows Me
27 WinNT4.0 for Windows NT 4.0
29 WinNT3.51 for Windows NT 3.11
29 Windows NT 5.1 for Windows XP
31 Windows NT 5.0 for Windows 2000
32 Windows NT 6.0 for Windows Vista
54 Windows NT 5.2 for Windows Server 2003; Windows XP x64

2 comments:

Mahesh Kharvi said...

non-awk

while read line; do
echo `echo $line | wc -c` $line
done < usragnt.txt | sort -n

But wc counts new-line also

Jadu Saikia said...

@Mahesh, thanks.

© Jadu Saikia www.UNIXCL.com