LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-27-2009, 02:11 AM   #1
abhigrkist
LQ Newbie
 
Registered: Sep 2008
Posts: 8

Rep: Reputation: 0
adding a line if pattern found


Hi Guys,
need your help.
i am finding a pattern in file.if that pattern found then I have to add a line after that matching pattern containing some worrds(say match found)through shell script or commmands
 
Old 11-27-2009, 02:55 AM   #2
rikijpn
Member
 
Registered: Jun 2007
Location: Japan
Distribution: Debian lenny, DSL, Solaris 10
Posts: 157

Rep: Reputation: 33
this is not homework right?

It's very simple. You just test on an "if" line whether the pattern was found or not, then you do echo or printf to print a message. Something like
Code:
if [ `grep someword somefile|wc -l` -gt 0 ] ; then echo "pattern found"; else echo "pattern not found"; fi
If you have to put it like right in the file, just use sed and replace the string by itself plus some line.
 
Old 11-27-2009, 03:05 AM   #3
abhigrkist
LQ Newbie
 
Registered: Sep 2008
Posts: 8

Original Poster
Rep: Reputation: 0
thanks riki ...
 
Old 11-27-2009, 03:07 AM   #4
abhigrkist
LQ Newbie
 
Registered: Sep 2008
Posts: 8

Original Poster
Rep: Reputation: 0
Riki I have to add a comment after the NEXT LINE OF paTTERN IN FILE .....PLZ HELP
 
Old 11-27-2009, 03:10 AM   #5
centosboy
Senior Member
 
Registered: May 2009
Location: london
Distribution: centos5
Posts: 1,137

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by abhigrkist View Post
Riki I have to add a comment after the NEXT LINE OF paTTERN IN FILE .....PLZ HELP

try

Code:
sed -i '/pattern/ a found' file
use first without -i for testing
 
Old 11-27-2009, 05:06 AM   #6
abhigrkist
LQ Newbie
 
Registered: Sep 2008
Posts: 8

Original Poster
Rep: Reputation: 0
thanks dude
 
Old 11-27-2009, 06:46 AM   #7
centosboy
Senior Member
 
Registered: May 2009
Location: london
Distribution: centos5
Posts: 1,137

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by abhigrkist View Post
thanks dude
did it work??
 
Old 11-30-2009, 02:22 AM   #8
abhigrkist
LQ Newbie
 
Registered: Sep 2008
Posts: 8

Original Poster
Rep: Reputation: 0
yaa its working thanks
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
search for a pattern and comment the line swaroop.tata Programming 2 09-04-2009 07:23 PM
[SOLVED] Adding (not replacing) a pattern match with a similar pattern? b-bri Linux - Newbie 2 08-31-2009 12:36 AM
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 06:17 AM
delete a line containing a pattern and the next line of a text file powah Programming 3 01-31-2007 05:34 PM
Pattern search in a line jitz Linux - General 2 12-06-2003 04:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:51 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration