Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 05-12-2008, 08:28 PM   #1
LQ Newbie
Registered: Aug 2007
Posts: 9

Rep: Reputation: 0
shell script to find an word or words from a line


What is the command to find the word or words from a given line.

I will be opening a file and reading line by line. Need to check for an certain word or words from that line, if its present then have to append the next 3 line ( including the present line into a file).

This i need to implement using shell script.

Please let me know ur suggestions on it.
Thanks in advance.

Old 05-12-2008, 08:46 PM   #2
Senior Member
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,125

Rep: Reputation: 165Reputation: 165
Tools like grep and sed can find patterns on lines within files. To append lines, I'd recommend sed. There's a tutorial here that should help with what you want to do.

I'm not at a box where I can write/test an example for you at the moment.
Old 05-12-2008, 11:08 PM   #3
Registered: Jan 2006
Distribution: Slackware Current
Posts: 272

Rep: Reputation: 33
Setup a test file and see if this is what your wanting. This should print the following three lines of the pattern and the pattern line. If the pattern is in one of the extra lines it prints it will not match that as a pattern. So this really may not help if it's what you want then all you have to do is decide how to use it in the scrip.

 sed -ne '/pattern/{N;N;N;p}' file
Old 05-12-2008, 11:52 PM   #4
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,406

Rep: Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396
> grep -A3 qwe t.t

> cat t.t

man grep
Old 05-13-2008, 11:41 AM   #5
LQ Newbie
Registered: Aug 2007
Posts: 9

Original Poster
Rep: Reputation: 0
That doesnt solve my problem.

file ex.txt has the following lines:
abc hfgsdf kjf sd kj
gfh kjf df efh rakesh fh
jsdhf sjhf sjhdf hf
hsd hsd ljhsd ds s
hsf f vs fsdg
using the shell script i will be opening the file and reading line by line. what my requirement is when ever i read a line, i need to check for the word rakesh, if its present then i need to get the 3 line including the line which has rakesh..

so i need below 3 lines when i check for word rakesh in a line
gfh kjf df efh rakesh fh
jsdhf sjhf sjhdf hf
hsd hsd ljhsd ds s

Note this file will have more then one line with the word rakesh.

Please let me know how to get it.

Old 05-13-2008, 01:57 PM   #6
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Look at the -A option to GREP. You can specify the number of lines after the match. ("man grep" for all the details)

Also, someone already posted how to do it with SED. Did you try that?

After either grep or sed, you'll need the redirection operator to write to a new
grep -AX <pattern> filename > newfilename


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 06:07 PM
How to identify a line and replace another string on that line using Shell script? Sid2007 Programming 10 10-01-2007 08:49 PM
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 07:42 AM
shell script find a line and the next line (grep?) metalx1000 Programming 5 07-24-2007 08:41 PM
shell script: find subwords with 'compilation' word suresheva31 Programming 11 10-05-2005 04:17 AM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:22 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration