Summary: Can sed or awk do this?

From: Xu, Ying (Ying.Xu@telecheck.com)
Date: Fri Jun 09 2000 - 09:15:00 CDT


I received more than ten responses. Thank you all for your help.

Both 'sed' and 'awk' can handle the task. 'sed' is easier.

I tried the followings. They all worked.

1. cat FILENAME | sed 's/'"$ID"'/'"$replacement"'/g'
    cat FILENAME | sed 's/'$ID'/'$replacement'/g'
    cat FILENAME | sed "s/$ID/$replacement/g"
    cat FILENAME | sed s/$ID/$replacement/g

2. /usr/xpg4/bin/awk -v v=$ID -v q=$replacement '{if ($1==v) $1=q; {print}}'
FILENAME
    Note, /usr/bin/awk doesnt have -v option.

Ying

> -----Original Message-----
> From: Xu, Ying
> Sent: Thursday, June 08, 2000 4:46 PM
> To: 'sun-managers@sunmanagers.ececs.uc.edu'
> Subject: Can sed or awk do this?
>
> Hi,
>
> I know it's easy to use 'sed' or 'awk' to search specific
> pattern in a file then replace it with another pattern, but if I dont have
> specific pattern, only have a variable which contains the pattern. Is
> there any way to perform the same task?
>
> e.g. in a file
>
> 2111
> 2112
> 2113
> 2114
> 2115
>
> ID=2112;
> I'd like to replace $ID with 9999. $ID is not always 2112,
> it varies. So I have to pass a variable to 'sed' or 'awk'.
>
> If 'sed' or 'awk' cannot do that, is there any other way to
> do it?
>
> Thanks in advance for your help.
>
> Ying



This archive was generated by hypermail 2.1.2 : Fri Sep 28 2001 - 23:14:09 CDT