[NTLUG:Discuss] Discuss Digest, Vol 111, Issue 2

Chris Cox cjcox at acm.org
Sun Mar 25 15:49:22 CDT 2012


On 03/25/2012 12:20 AM, CoryC wrote:
>
>>
>> All is there any have a good suggestion on change words on
>> every line in a file. ex:original: dog
>>     1234cat
>>     1234mouse  1234houesr 1234 after
>> changed dog 9899cat 9899mouse 9899house 9899 I have this,
>> but seems to not woking: #!/bin/sh
>> v='9899'
>> cat file | while read s1 s2
>> do
>> if [ "$s2" != "$v" ]; then
>>      echo $s1 '--' $s2
>>     cat  "$s1" ' ' "$IPADDR">>
>> file
>>     sed -i '/$s2/ d' file
>> fi
>> done
>> exit 0 thank for your
>> help.
>>
>>
>>
>>
>
> If you are simply wanting to replace "1234" with "9899" then I think the following should work:
>
> cp /path/to/filename /tmp/filename.tmp
> sed s/"1234"/"9899"/g<  /tmp/filename.tmp>  /path/to/filename
> rm -rf /tmp/filename.tmp
>
> if you are wanting to write it into a script that you can pass two variables to then I think that it will look like this:
>
> cp /path/to/filename /tmp/filename.tmp
> sed s/"$1"/"$2"/g<  /tmp/filename.tmp>  /path/to/filename
> rm -rf /tmp/filename.tmp
>
>

perl -pi -e "s,${original},${replace},g" /path/to/filename

assumes original contains the original pattern and replace contains what 
to replace it with.

The commas are delimiters... choose a character not used in original or 
replace.

You may need to escape characters in original or replace that means 
something to perl regular expressions.... for example, if the idea is 
really to find and replace IP addresses, I assume that periods are used, 
which translates to "any character" when used as a regular expression... 
so you can precede the character in original with backslash.... etc... 
do that for any special pattern match char inside of original.



More information about the Discuss mailing list