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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find and replace 1

Status
Not open for further replies.

BIS

Technical User
Jun 1, 2001
1,893
NL
Hallo All,
Hopefully someone can point me in the right direction.
I am trying to modify an incoming email. To simplify things, suppose the email only contains 3 lines of text:

Some text here.
Name: Bob
Some more text here.

How would I with this simple text file, search for a line starting with "Name", catch the textstring after the : and replace the line with something like

<Name>Bob</Name>

Any ideas?
 
man awk

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Many thanks. Reading through the man page, being a beginner, I have a small question. I can't seem to find

a) If the search string is "First Name" and not just "Name" , how would I modify the above line?

b) In the {$0="<Name>"$2"</Name>"} I see that if there are for example two words, I can do {$0="<Name>"$2 $3"</Name>"} , but this removes the space between $2 and $3 - how do I keep a space?

Sorry to be such a pain, its just that getting started by reading this man page is a tad confusing...

 
You may try this:
awk -F: '$1~"Name"{x="</"$1">";$1="<"$1">";$0=$0 x}1' /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Many many thanks - I am getting there. One last question (hopefully), can I have more than one command?

I am trying to use the -f function. In my test.awk file I have:

/^Subject:/ {$0="<Subject>"$2" "$3"</Subject>"}1
/^Model/ {$0="<Model>"$2" "$3"</Model>"}1

but this gives a weird double lined output.

I am trying to simply awk-f test.awk testmail

What would be the correct format to have two or more find and replace funstions?



 
mrn - you must have posted while I was typing. I will take a look at the sed pages, but honestly, learning two new languages will be even more difficult...

Would you recommend sed for this? Why? Well why awk for that matter...
 
Code:
/^Subject:/ {$0="<Subject>"$2" "$3"</Subject>"}
/^Model/ {$0="<Model>"$2" "$3"</Model>"}
{print}
 
Would you recommend sed for this? Why? Well why awk for that matter...

For this alone, both do a good job, if you need to process more stuff than just that awk is def. the way. Where awk is kindof a whole language, sed is a stream editor.. It may come way more difficult to form a good sed cmd than awk. Speed speaks for proper awk'ing.

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
Speed speaks for proper awk'ing
Really ?
I think sed is faster than awk.
 
Yeah well, so much is relative. All depends, my experience mostly goes to sed being more bloated and slower, but i usually do more complex stuff.

I benchmark my awk stuff with mawk, which may be some faster than nawk.

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top