1753259 Members
5331 Online
108792 Solutions
New Discussion юеВ

Shell Script Help

 
SOLVED
Go to solution
Steven Chen_1
Super Advisor

Shell Script Help

Hope someone can help:

I have the following script:

sed -e 's/EMAIL_ADDR_TXT/AGENCY_LOCATION/'
-e 's/CONT_EMAIL_ADDR_TXT/AGENCY_CONTACT/'
-e 's/EMX_ADDR/WEB_APPLICANT/'
-e 's/SITE_EMAIL_ADDR_TXT/RESOURCE_LOCATION/' $BADMAIL>badmail1

but the result is all words of EMAIL_ADDR_TXT have been changed to AGENCY_LOCATION, the first sed choice.

I need some field protection and change the word to exactly what it means. But using [ ] to include words still not working. Nor I succeeds in using " " to protect.

Any comments are appreciated.



Steve
12 REPLIES 12
Peter Godron
Honored Contributor
Solution

Re: Shell Script Help

Steven,
is there a field seperator in the file?

If, for example it is "," you could do
sed -e's/,EMAIL_ADDR_TXT,/,AGENCY_LOCATION,/

James R. Ferguson
Acclaimed Contributor

Re: Shell Script Help

Hi Steven:

You need to provide a better regular expression -- one that uniquely identifies the boundries of a match/substitution.

For example, anchor EMAIL_ADDR_TXT to the beginning of a line with a caret:

sed -e 's/^EMAIL_ADDR_TXT/

You can anchor at the end of a line with a dollar sign ($). You can use spaces and tabs too to form logical boundries.

Far, far superior to 'sed' is 'perl'. Perl has some of the very best regular expression support you can find, in my opinion.

Regards!

...JRF...
renarios
Trusted Contributor

Re: Shell Script Help

Hi Steven,

I guess you need to use the global option (/g) option, just like in vi :%s///g

So the syntax would be:
sed -e 's/EMAIL_ADDR_TXT/AGENCY_LOCATION/g'\
-e 's/CONT_EMAIL_ADDR_TXT/AGENCY_CONTACT/g'\
-e 's/EMX_ADDR/WEB_APPLICANT/g'\
-e 's/SITE_EMAIL_ADDR_TXT/RESOURCE_LOCATION/g'\
$BADMAIL>badmail1

Cheers,

Renarios
Nothing is more successfull as failure
Arunvijai_4
Honored Contributor

Re: Shell Script Help

Hi,

A very good reading about "sed"

http://www.student.northpark.edu/pemente/sed/sed1line.txt

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Peter Nikitka
Honored Contributor

Re: Shell Script Help

Hi,

as a first change, you could change the order of the sed-commands: since the command sequence of sed is in serial order, put the longest strings first: they have been changed already when sed executes the following command(s).
sed -e 's/CONT_EMAIL_ADDR_TXT/AGENCY_CONTACT/'
-e 's/SITE_EMAIL_ADDR_TXT/RESOURCE_LOCATION/'
-e 's/EMAIL_ADDR_TXT/AGENCY_LOCATION/' ...

If there is always at least one character of text before the search pattern, you can anchor it (like the other posters suggested). Pattern starting at the beginning of a line must be added seperately:

sed -e 's/\([^_]\)EMAIL_ADDR_TXT/\1AGENCY_LOCATION/' -e 's/^EMAIL_ADDR_TXT/AGENCY_LOCATION/'
..
and so on. If the overlapping search pattern differ not in the '_' unterline, the anchor pattern has to be refined, perhaps you need a different anchor pattern for the end of the search pattern.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Sandman!
Honored Contributor

Re: Shell Script Help

Hello!

I'm not sure if your question has been answered by one of the aforementioned posts, but it'll help if you could attach a portion of the input file on which these sed substitutions have to occur. These would definitely help in providing an accurate solution to your problem.

cheers!
Arturo Galbiati
Esteemed Contributor

Re: Shell Script Help

Hi,
try:
sed -e 's/SITE_EMAIL_ADDR_TXT/RESOURCE_LOCATION/'
-e 's/CONT_EMAIL_ADDR_TXT/AGENCY_CONTACT/'
-e 's/EMX_ADDR/WEB_APPLICANT/'
-e 's/EMAIL_ADDR_TXT/AGENCY_LOCATION/' $BADMAIL>badmail1

Since Sed exceutes the commands in order, first chnage the long pattern and after the pattern contained into previous pattern already chnaged.
HTH,
Art
Steven Chen_1
Super Advisor

Re: Shell Script Help

I am going to attach original source file "badmail.lst" (=$BADMAIL) and the resulting file using my script. Maybe you could find out the solution.

Thanks a lot and have fun.
Steve
Arunvijai_4
Honored Contributor

Re: Shell Script Help

Hello Steven,

What exactly you want to do ? Its always better to use perl for regular expressions.

http://www.english.uga.edu/humcomp/perl/regex2a.html

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"