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