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