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

CoryC oakleeman at yahoo.com
Sun Mar 25 00:20:48 CDT 2012


> 
> 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




More information about the Discuss mailing list