Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Software
User Name
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.


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

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


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, 03:13 PM   #2
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 658Reputation: 658Reputation: 658Reputation: 658Reputation: 658Reputation: 658
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, 01:47 PM   #3
LQ Newbie
Registered: Feb 2009
Posts: 19

Rep: Reputation: 2

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, 02:14 PM   #4
LQ Veteran
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,692
Blog Entries: 1

Rep: Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106
If the replacement needs to take place only in lines containing the pattern 'aaa', you could try the following:
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.
edit: hope it's not your homework, is it?

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


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 06:54 PM
PERL pattern matching while reading a file content is dead slow gaynut Programming 13 08-13-2008 07:59 AM
pattern matching in file amitpardesi Linux - Software 5 02-08-2008 08:06 AM
pattern matching nadeemr Linux - Newbie 8 06-13-2007 12:05 PM
Linux/Unix script for file pattern matching varunnarang Programming 1 08-07-2006 02:14 PM

All times are GMT -5. The time now is 03:14 AM.

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