LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices

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

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


Hi,

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.

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

Rep: Reputation: 151Reputation: 151
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
davimint
Member
 
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.

Code:
 sed -ne '/pattern/{N;N;N;p}' file
 
Old 05-12-2008, 11:52 PM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,269

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
> grep -A3 qwe t.t
qwe
asd
zxc
iop

> cat t.t
12345
qwe
asd
zxc
iop


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

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

Example:
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.

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

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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 file...eg:
grep -AX <pattern> filename > newfilename
 
  


Reply


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 51 07-04-2013 08:16 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


All times are GMT -5. The time now is 07:37 AM.

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