我有一个文件叫做 songs 并且我试图用 AWK 找到最长和最短的歌曲名称

我有一个文件叫做 songs 并且我试图用 AWK 找到最长和最短的歌曲名称
RANK  NAME                    BAND  YEAR   GENERE  DOMESTIC/INTERNATIONAL 

206:Reach Out, I'll Be There:The Four Tops:1978:Pop:3/2
207:Bye Bye Love:The Everly Brothers:1950:Classic:3/2
208:Gloria:Them:1965:Classic:1/1
209:In My Room:The Beach Boys:1985:Classic:5/7
210:96 Tears:? & the Mysterians:1964:Classic:20/15
211:Caroline, No:The Beach Boys:1975:Classic:5/7
212:1999:Prince:1958:Classic:5/7
213:Your Cheatin' Heart:Hank Williams:1988:Soul:7/6
214:Rockin' in the Free World:Neil Young:1960:Pop:5/7
215:Sh-Boom:The Chords:1967:Alternative:3/2
216:Do You Believe in Magic:The Lovin' Spoonful:1988:Classic
217:Jolene:Dolly Parton:1998:Classic:7/6
218:Boom Boom:John Lee Hooker:1966:Classic:7/6

答案1

假设标题不是文件的一部分:

awk -F: '
    NR == 1 {max=$2; min=$2; next} 
    length($2) > length(max) {max=$2} 
    length($2) < length(min) {min=$2} 
    END {print "longest=" max; print "shortest=" min}
' songs
longest=Rockin' in the Free World
shortest=1999

相关内容