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
(1) 我尝试打印带有新标题的排名、表演者和歌曲列表,所以我尝试:
nawk 'BEGIN { FS=":"
printf "%-10s %-35s %-55s\n", "RANK", "PERFORMER","SONG"
print "=====================================================================\n"}
{printf "%-10s %-35s %-55s\n", $1, $3, $2}' songs
但输出仍然在该行下有默认标题,那么我如何删除新标题下的第一行?
(2)我需要计算每种流派的歌曲数量并报告每种流派的总数所需的输出示例:
classic 10
R&B 5
Soul 9 etc..
我尝试过但未能获得所需的输出:
awk '{count+$5} END {print $5}' songs
答案1
您必须跳过第一行:
awk 'BEGIN { FS=":"
printf "%-10s %-35s %-55s\n", "RANK", "PERFORMER","SONG"
print "=====================================================================\n"}
FNR==1{next}{printf "%-10s %-35s %-55s\n", $1, $3, $2}' songs
如果您不介意输出顺序,请尝试:
$ awk -F':' 'FNR==1{next}{a[$5]+=1} END{for(i in a){print i,a[i]}}' songs
Soul 1
Classic 6
Pop 1