LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-29-2009, 02:11 PM   #1
shivarajM
LQ Newbie
 
Registered: Apr 2009
Posts: 22

Rep: Reputation: 15
Arrow matching a pattern in a file & prefixing a word


Hi,

In my shell script i have to match a patten in a file , if found i have to prefix the entair line by a "word"

eg. pattern = "aaa" prefix= #123
file: bbbb xxx
zzzz aaaa
qqqq kkkk

outPut file: bbbb xxx
#123zzzz aaaa
qqqq kkkk


Waiting your precious reply.....
 
Old 04-29-2009, 02:13 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 681Reputation: 681Reputation: 681Reputation: 681Reputation: 681Reputation: 681
Look into using sed. The -i option will allow you to do inplace editing making changes in the original file.

You might not to do this until your sed command is tested.
 
Old 05-28-2009, 12:47 PM   #3
esoukenka
LQ Newbie
 
Registered: Feb 2009
Posts: 19

Rep: Reputation: 2
sed

Yes sed is what you want.

Test this on your file

sed -e 's/^aaa/wordaaa/g' ../lyrics

This command will replace all lines starting with aaa to wordaaa. Remove the ^ to do them all.

that will not modify file to do that use the -i option
 
Old 05-28-2009, 01:14 PM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1249Reputation: 1249Reputation: 1249Reputation: 1249Reputation: 1249Reputation: 1249Reputation: 1249Reputation: 1249Reputation: 1249
If the replacement needs to take place only in lines containing the pattern 'aaa', you could try the following:
Code:
sed -e '/aaa/ s/^/#123/g' file
It will insert '#123' at the beginning of each line containing the pattern 'aaa'
As it was mentioned before, you need to use the switch -i to actually modify the file.
HTH
edit: hope it's not your homework, is it?

Last edited by sycamorex; 05-28-2009 at 01:17 PM.
 
  


Reply

Tags
match, prefix, script, shell, word


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Pattern matching in a text file - use of AWK?? wtaicken Programming 19 02-06-2009 05:54 PM
PERL pattern matching while reading a file content is dead slow gaynut Programming 13 08-13-2008 06:59 AM
pattern matching in file amitpardesi Linux - Software 5 02-08-2008 07:06 AM
pattern matching nadeemr Linux - Newbie 8 06-13-2007 11:05 AM
Linux/Unix script for file pattern matching varunnarang Programming 1 08-07-2006 01:14 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:31 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