Using variables in a sed loop...

Hi all,

I'm trying to search for the first field in a file called vintella.sudoers_1 and then use the result to grep for its match in vintella.sudoers_useralias file but assigning the second field of what it finds to another variable called to_replace.

I then want to use that to_replace variable in a sed command to replace that first field in the vintella.sudoers_1 into a new file called vintella.sudoers_2

Everything works fine right up until the sed part.. It keeps the original info and then the sed'd info where as I just want the sed'd info only in the vintella.sudoers_2 file... If that makes sense.??..

for i in `cat vintella.sudoers_1 | awk '{print $1}'`
do
to_replace=$(grep $i vintella.sudoers_useralias | awk -F= '{print $2}')
echo $i $to_replace
sed "s/${i}/${to_replace}/" vintella.sudoers_1 >> vintella.sudoers_2
done

Try replacing

sed "s/${i}/${to_replace}/" vintella.sudoers_1 >> vintella.sudoers_2

with

sed -n "s/${i}/${to_replace}/p" vintella.sudoers_1 >> vintella.sudoers_2

Your script could be re-written like this towards some tuning.

while read i junk
do
 to_replace=$(awk -v r=$i -F= '/r/ {print $2}' vintella.sudoers_useralias)
 echo $i $to_replace
 sed "s/${i}/${to_replace}/" vintella.sudoers_1 >> vintella.sudoers_2
done < vintella.sudoers_1

Please test it before using since I might miss something related to file formatting which you didn't post.

Thanks for the reply clx..

I tried your sed method and it kind of works.. It's replacing what I need but I think because its in the for loop it keeps spitting out the other information too once it performs the action on each line..

I also tried the other method you mentioned but it didn't work at all.. It just left a blank space at the start of each line.. Awk commands don't seem to translate as well to Solaris...

So you are also having other lines apart from "to be replaced" lines.
You might need to take the result file for each iteration to stop duplication.

something like..

cp vintella.sudoers_1 tmp

and inside loop,

sed "s/${i}/${to_replace}/" tmp > tmp2
mv tmp2 tmp

It would be better if you could post the sample data from the files so that we can suggest better way arround.

Hi,
Best way to show I guess is to give a few lines of each file.. I want to replace field 1 of vintella.sudoers_1 with field 2 of what it matches in vintella.sudoers_useralias

vintella.sudoers_1

vintella.sudoers_useralias

vintella.sudoers_1

You might try..

$ cat update.sh
#! /usr/bin/ksh

# source the file
. vintella.sudoers_useralias

# do change
while read v1 v2 v3
do
 eval echo \$$v1 $v2 $v3
done < vintella.sudoers_1
$
$
$
$ ./update.sh
anap02 KVHPOJANAPC C_POJ_NOTES_START_ANAPC
anap02 KVHPOJANAPC C_POJ_NOTES_STOP_ANAPC
anap04 KVHPOJANAPD C_POJ_NOTES_START_ANAPD
$
$

It seems to me reasonable to assume that neither spaces nor equals signs can appear in either the original value nor the alias which replaces it. The following code makes that assumption. If the assumption is incorrect, the code would require a little more work.

awk -F'[ =]' 'FNR==NR {a[$1]=$2; next} {$1=a[$1]} 1' vintella.sudoers_useralias vintella.sudoers_1

Regards,
Alister

Tried the above but got the following error:-

syntax error at line 1 : `(' unexpected

EDIT**

Hi alister..
I just tried your command this time but added /usr/xpg4/bin/ to the begin of it and it works this way..

/usr/xpg4/bin/awk -F'[ =]' 'FNR==NR {a[$1]=$2; next} {$1=a[$1]} 1' vintella.sudoers_useralias vintella.sudoers_1

Is there any way of adding a condition that says if $1 = root to skip it and move on to the next line?

I did not test it at all, but this should do it:

/usr/xpg4/bin/awk -F'[ =]' 'FNR==NR {a[$1]=$2; next} $1!="root" {$1=a[$1]; print}' vintella.sudoers_useralias vintella.sudoers_1

Regards,
Alister

Hmmm, tried that but it doesn't copy the line that begins with root to the vintella.sudoers_1 file..

Thanks for replying though..

EDIT**

Hi,
Is there anyway to alter this command so that if it does not find a match it will just leave the line alone instead of replacing it with a blank space?

awk -F'[ =]' 'FNR==NR {a[$1]=$2; next} {$1=a[$1]} 1' $useralias ${entries} >> ${entries}_2