2
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) I tried to print a list of Rank, performers and songs with new header so I tried:

nawk  'BEGIN { FS=":" 
printf "%-10s %-35s %-55s\n", "RANK", "PERFORMER","SONG"
print "=====================================================================\n"}
{printf "%-10s %-35s %-55s\n", $1, $3, $2}' songs

but the output still have the default header under the line so how i remove the first line under the new header?

(2)I need to count the number of songs from each type of Genre and report totals of each genre Desired output is example:

classic 10
R&B 5
Soul 9 etc..

I tried but failed to get the desired output:

awk '{count+$5} END {print $5}' songs 
2

You must skip the first line:

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

If you don't mind the order of output, try:

$ awk -F':' 'FNR==1{next}{a[$5]+=1} END{for(i in a){print i,a[i]}}' songs
Soul 1
Classic 6
Pop 1
  • Thank you your commands did work successfully ... I really appreciate your help – Moaledma Apr 18 '14 at 18:39
  • @Intermd11: My answer to second question seems wrong, what is the number of songs in your input? – cuonglm Apr 18 '14 at 18:42
  • it did work successfully. output returned without the first line. I have 500 songs in the file – Moaledma Apr 18 '14 at 18:44
  • 1
    FNR==1{next}{a[$5]+=1} can be written in a more awkish way as NR>1{a[$5]+=1}. – jaypal singh Apr 18 '14 at 20:01
  • 1
    @JS웃: Yeap, my answer is aim to the clarity for the OP. – cuonglm Apr 18 '14 at 20:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.