Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Sed and Awk (http://www.programmingforums.org/forum22.html)
-   -   Sed, insert string to the beginning of a line (http://www.programmingforums.org/showthread.php?t=4491)

Ken2 Jun 17th, 2005 12:52 PM

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

jim mcnamara Jun 20th, 2005 9:07 AM

awk solution:

:

awk ' {if(/AB/)
      { print "CDE" $0}
      else
        {print $0}
      } ' filename

filename contents:
:

AB
PDQ
PLMAB

output from awk:
:

CDEAB
PDQ
CDEPLMAB


Ken2 Jun 20th, 2005 12:09 PM

Thank you very much :)


All times are GMT -5. The time now is 11:23 PM.

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