I'm trying to print out all the lines up to the appearance of a particular song and 5 more songs with that. The problem i'm new to the shell what I have to fix in the bellow commands?

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     
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   
213:Your Cheatin' Heart:Hank Williams:1988:Soul:7/6 
awk '{print * "For What It's Worth"} ' songs            
awk '/For What It's Worth/, {print $1 $2}' songs
  • Can you try explaining this a bit more, it's unclear what you're asking.
    – slm
    Apr 19, 2014 at 0:05
  • @slm I want a command to print out all the lines up to "Caroline, No" and 5 songs after that. like print from the beginning up to song name "Caroline" and 5 more songs (lines) after that.
    – Moaledma
    Apr 19, 2014 at 0:07
  • 1
    In general: If you ask something like that then you should always provide example input and example output. Apr 19, 2014 at 0:13
  • @Intermd11 - Also since you're pretty new to our site if you notice under your Q there's a edit link. You can refine your Q as you get feedback from others or if you want to expand it so that it better illustrates your problem. The comment you left me for example could be adapted into the Q.
    – slm
    Apr 19, 2014 at 0:15
  • @HaukeLaging Sure
    – Moaledma
    Apr 19, 2014 at 0:33

2 Answers 2


Not sure what your attempted solution has to do with the problem you state; if your problem description is accurate the following should work

awk 'NR==1,/Caroline, No/{print;next};c++<5{print};' file
  • Probably easier to understand with c++<5 {print} (and adding the info that {print} is not necessary). Apr 19, 2014 at 0:18
  • I appreciat it your help but what if I have a song named "For What It's Worth". probably that command will not work because the song name contain the symbol (It's).
    – Moaledma
    Apr 19, 2014 at 0:23
  • @Intermd11, awk "NR==1,/For What It's Worth/{print;next};c++<5{print}" file should take care of it
    – iruvar
    Apr 19, 2014 at 0:25
  • @HaukeLaging, you are probably right, amended answer. I usually get upbraided on SO for being too verbose with awk but when it comes to newbies i see your point
    – iruvar
    Apr 19, 2014 at 0:26
  • @1_CR that actually worked, thank you for your assistance.
    – Moaledma
    Apr 19, 2014 at 0:28
sed '/For What It.s Worth/{N;N;N;N;N;q}'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .