Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 17th, 2005, 1:52 PM   #1
Ken2
Newbie
 
Join Date: Jun 2005
Posts: 2
Rep Power: 0 Ken2 is on a distinguished road
Sed, insert string to the beginning of a line

Hello,
I would need to add a specific string to the beginning of every line in a document which contains an other specific string, by using Sed or Awk.
I've tried my best to figure out this myself by using examples and tutorials, but I cant find any documentation about this. I did find out how to add something to the beginning of every line (s/^.*/text-to-add/ ), but I didn't manage to convert that so that it would work as i need it.

Ie. If a line contains the string <AB>, i would like to add a the text 'CDE' to the very beginning of that same line.

Im using Sed for Windows, but I asume the commands are almost the same as for Linux.

Thank you in advance,
Ken2

Last edited by Ken2; Jun 17th, 2005 at 4:20 PM.
Ken2 is offline   Reply With Quote
Old Jun 20th, 2005, 10:07 AM   #2
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
awk solution:

awk ' {if(/AB/) 
       { print "CDE" $0} 
       else 
         {print $0}
       } ' filename
filename contents:
AB
PDQ
PLMAB
output from awk:
CDEAB
PDQ
CDEPLMAB
jim mcnamara is offline   Reply With Quote
Old Jun 20th, 2005, 1:09 PM   #3
Ken2
Newbie
 
Join Date: Jun 2005
Posts: 2
Rep Power: 0 Ken2 is on a distinguished road
Thank you very much
Ken2 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:27 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC