Hi Rick,
Thanks for the suggestion,
I have tried the read command and it still does not work.
The perl version keeps going after the last record in the list and it does
not change anything.
I used the read command in my script (thanks for the idea) and it is much
faster. 
As you quite rightly said, the $ sign is seen as a control character but
enclosing the sed command within quotes fixes that. 
The main problem was using -e instead of -i in sed, spotted it after looking
at your script.
. My solution was:

cat Mapping/$1-Mapping1 | while read replacestring searchstring 
do
  if [ $searchstring != $replacestring ];
        then
  sed -i "s/ $searchstring / $replacestring /g" $2
fi
done 

Thus only replacing when required
I would like to get your script working too but it just keeps running after
it hits the last record.
I suspect it has something to do with Windows files (the text files are from
a Windows environment).

Thank you again Rick and Nima.

Regards, Mehmet

> ----- Original Message -----
> Subject: Re: [SLUG] Search and replace  question
> From: Rick Welykochy <r...@praxis.com.au>
> To: "Mehmet Yousouf" <mehm...@carramar.auslin.com.au>
> Date: 06-03-2010 11:48
> 
> 
> Mehmet,
> 
> Be aware that there are special characters in regular expressions
> as used by grep and sed. $ is special: it matches the end of the line.
> There are others, like ^ [ ] + * etc.
> 
> If any of these special chars are in your searchstring, grep will fail
> and sed will fail.
> 
> 
> Mehmet Yousouf wrote:
> 
> > I'm having trouble with the following search and replace script:
> >
> > #!/bin/bash
> > # two parameters are passed in, a mapping file and the text file to
replace
> > in
> >
> > for fields in $(cat ./Mapping/$1-Mapping |awk 'BEGIN{FS=","}{print $2}')
> > do
> >    searchstring= $fields
> >    replacestring="`grep -w $fields ./Mapping/$1-Mapping |awk
> > 'BEGIN{FS=","}{print $1}'`"
> 
> >    sed -e "s/$searchstring, / $replacestring, /g" $2>TestResult
> > done
> >
> >   it fails if there is a $ sign in the variable $field e.g. amount_$ .
How
> > can I get grep to "behave" the way I want?
> 
> One solution:
> 
> 1. read in the replace and search strings in one go, which makes the
script
>     run faster and avoids the need for grep. bash's read command is fine
for
>     this if we set the field separator (IFS) to a comma
> 
> 2. use perl to perform the substitution, incorporating the special
characters
>     \Q at the beginning of the search string, which tells perl to ignore
the
>     special characters I spoke of above.
> 
> 3. we use the -i switch in perl to edit the file TestResult in situ and
the -p
>     switch to wrap the perl command (after -e) in an input loop followed
by a print
> 
> Try this:
> 
> #/bin/bash
> 
> cp "$2" TestResult
> 
> export IFS=','
> cat Mapping/$1-Mapping | while read replace search
> do
>       perl -i -p -e "s/\Q$search/$replace/g" TestResult
> done
> 
> 
> 
> cheers
> rickw
> 
> 
> -- 
> _________________________________
> Rick Welykochy || Praxis Services
> 
> I think there is a world market for about five computers.
>       -- Thomas Watson, IBM, 1943


--
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html

Reply via email to