Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert $string to a file

Status
Not open for further replies.

blueeyedme

Technical User
Nov 27, 2002
39
0
0
NL
Hiya ;)

I got the following.

I am trying to get a script which inserts a string/line to a file.
Before i do this i want to search for a typical string in this document and insert my string about this line.

I thought this was possible with sed or ? i cant figure it out
 
To insert text in an existing line:

[tt]sed 's/originalstring/newstring/' file > file2
mv file2 file[/tt]

To insert text on a new line after an existing one:

[tt]sed '/originalstring/anewstring' file > file2
mv file2 file[/tt]

Use 'i' instead of 'a' to insert the text on the line before. Annihilannic.
 
Thanks i am going to try this one..

Thankyou :D
 
i am getting the following error :

ed: -e expression #1, char 7: Extra characters after command

i used it like this

sed -e '/MX/i\mx' test6 > test7
 
You actually need to separate it into two lines like in my example. It's a strange syntax, I know. Annihilannic.
 
Hmm... what UNIX are you using? Works fine on Solaris, Linux and SCO OpenServer, which are all I have access to. Annihilannic.
 
i tried it on unix too.. on solaris and it works there.. but when i try it on debian linux it gives me this error
 
Oh well, a workaround:

[tt]awk '/originalstring/ { print "newstring" }
{ print }' file > file2
mv file2 file[/tt]
Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top