Computing.Net > Forums > Unix > search & insert text into a string

search & insert text into a string

Reply to Message Icon

Original Message
Name: sandeeprk
Date: May 12, 2003 at 21:44:18 Pacific
Subject: search & insert text into a string
OS: kp-ux
CPU/Ram: p2
Comment:

hello all,
i have a string "start(hello) welcome to unix end(hello)start(hi) welcome 2 unix end(hi)". note that this is a string & not a file. i want to look for the word end & insert (xxxx) before it.
so my string at the end of the operation shud b
"start(hello) welcome to unix (xxxxx)end(hello)start(hi) welcome 2 unix(xxxxx) end(hi)".
if putting the string in a file & then manipulating is easier, i don't mind doing that. but i wud prefer direct string manipulation.
thanx in advance
sandeep


Report Offensive Message For Removal


Response Number 1
Name: nail
Date: May 12, 2003 at 22:33:27 Pacific
Subject: search & insert text into a string
Reply: (edit)

Hi:

A way ....

# all on one line
echo "start(hello) welcome to unix end(hello)start(hi) welcome 2 unix end(hi)" |awk ' { gsub("end","\(xxxx\)end"); print $0 } '

The above uses awk to substitute xxxxend for end. Note escaping the special characters ()

Regards,


Nails


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: search & insert text into a string

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge