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

Looking for the right tool for the job

Status
Not open for further replies.

rogers42

Technical User
Mar 30, 2007
64
CA
Hi Folks,

I have a shell script that processes some data and emails the results to a list of receipients. The email addresses are hard coded in the shell script.

I have written an awk utility to globally search and to replace certain email addresses with the new ones.

What I want to do is to update the original file (with the new email addresses) using my awk utility.

However, what I am getting is as follows

1, The original file is not updated
2, The utility prints the entire line where the change was made
3, If three changes are made on a line, the line is printed three times over.

Re-directing the output to a new file won't help because I will get the delta.

For starters, is awk the right tool for the job at hand ?

Can anybody suggest a work around (using awk) ? or a better tool to get the job done ?

PS: I don't know Perl. However, I am familiar with Korn shell and sed (a little bit)

Thanks in advance.

rogers42




 
What is your actual code ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi,

The awk code is as follows

# Search for the old string and replace with a white space
gsub(/david@yahoo.com/, "");
gsub(/david@hotmail.com/, "");
gsub(/[Tt]amy@infinity.com/, "");
gsub(/[Aa]rthur@infinity.com/, "");

# Search for any of the following strings and replace with two new strings
gsub(/[Kk]athy@yahoo.com/, "howard@yahoo.com charles@yahoo.com");


The awk utility is invoked as follows

nawk -f search_and_replace.awk test1.txt

Thanks

rogers42
 
Why not just use sed;

sed 's/search string/new string/g' yourfile > newfile | mv newfile yourfile.
 
Yup, sed is less painful. Thanks for the suggestion.

Can somebody please give me the (sed) syntax to replace an existing string with nothing (as opposed to a white space). Following is what I currently have

s/david@yahoo.com//g

The above code leaves a white hole.

Thanks in advance.

rogers42

 
Think you must have a whitespace right before or after
the original mailadress, so eighter:
Code:
s/\ david@yahoo.com//g
or
Code:
s/david@yahoo.com\ //g
should remove it.
 
Perhaps this ?
Code:
s/ *david@yahoo.com *//g

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks geirendre and PHV. Both methods work well.

Correct me if I am wrong, but geirendre's code is trying to escape white spaces?

I am unclear as to how PHV's code fixes the problem. Are "*" used as wild characters to include white spaces ?

Thanks in advance

rogers42
 
In sed, * means: any number of the previous character or wildcard

so:
[tt]/ */[/tt] means: zero or more spaces
[tt]/ *john */[/tt] means: the word john surrounded at either end by zero or more spaces

for more info: see the man page for sed


HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top