1

I have a file with 100 lines and i have to search the word happy but the search must start after the 50th line of the file. what's the exact grep command is used in linux?

3

You could also use head + grep and group the commands with {...} to share the same input:

{ head -n 50 >/dev/null; grep PATTERN; } <infile

that way head gets only the first n lines, dumps them to /dev/null and the remaining lines are processed by grep.

2

Here is a sed way to get from 50th line to the end of the file and then using grep to search for the pattern:

sed -n '50,$p' file.txt | grep "happy"

Or just using sed to do it altogether:

sed -n '50,$s/happy/&/p' file.txt

Here are few more sed ways (Thanks to 'Stéphane Chazelas'):

sed '50,$!d;/happy/!d' file.txt
sed -n '50,${/happy/p;}' file.txt
  • Why not just search with sed, while you are at it? – orion Mar 17 '15 at 10:57
  • @orion: Good point..answer edited – heemayl Mar 17 '15 at 11:03
  • 1
    Or sed '50,$!d;/happy/!d' or sed -n '50,${/happy/p;}' – Stéphane Chazelas Mar 17 '15 at 11:04
  • @StéphaneChazelas: Thanks, i have added these too.. – heemayl Mar 17 '15 at 11:13
2

Use tail to get the contents of the file from line 51 onwards, and then grep that. Use -i to ignore case (so you also match "Happy", "HAPPY", etc) and a space before to match only "happy" and not any other words that contain that sequence of letters.

tail -n +51 filename.txt | grep -i " happy"
  • 1
    +1, but note that the word "happy" might have parentheses or quotes around it or it might be followed by any of ,``.``; etc or it might be at the beginning/end of line; also since the search must start after the 50th line you could tail -n +51 – don_crissti Mar 17 '15 at 15:21
  • Good points. Edited accordingly. – Carl H Mar 17 '15 at 18:07
  • I think you could just grep pattern. The question is about how to grep after a certain line and IMO the word problem is beyond the scope of this question (and it most likely has been already addressed on this site). Up to you though. – don_crissti Mar 17 '15 at 18:17
0

You will need awk:

awk 'NR>50' filename.txt | grep '\bhappy\b'
  • That's wrong. You got it backwards. "NR>50" makes no sense after you already grepped out only the lines that contain the word happy. It's also a good idea to use word boundaries to avoid matching partial words (this version also matches unhappy which is probably not what you want). – orion Mar 17 '15 at 10:59
  • You are right. Corrected. – jcbermu Mar 17 '15 at 11:02
  • That only matches cases when the word is alone in the line. I meant \bhappy\b. At least in GNU grep. And cat is unnecessary. Just give the file to awk. – orion Mar 17 '15 at 11:11
  • @orion, grep -w happy may be slightly more portable than \b. Neither is standard. The GNU one doesn't work in multi-byte locales (grep -w phane or grep '\bphane\b' match on Stéphane for instance). – Stéphane Chazelas Mar 17 '15 at 11:31

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.