Using variables in a sed loop...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using variables in a sed loop...
# 1  
Old 06-13-2012
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.??..

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

# 2  
Old 06-13-2012
Try replacing

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

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


Your script could be re-written like this towards some tuning.
Code:
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.
# 3  
Old 06-13-2012
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...
# 4  
Old 06-13-2012
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..
Code:
cp vintella.sudoers_1 tmp

and inside loop,

Code:
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.
# 5  
Old 06-13-2012
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

Quote:
KVHNOTESIDANAPC KVHPOJANAPC C_POJ_NOTES_START_ANAPC
KVHNOTESIDANAPC KVHPOJANAPC C_POJ_NOTES_STOP_ANAPC
KVHNOTESIDANAPD KVHPOJANAPD C_POJ_NOTES_START_ANAPD

vintella.sudoers_useralias
Quote:
KVHNOTESIDAHMLB=ahml02
KVHNOTESIDAHUBA=ahub01
KVHNOTESIDALMLD=alml02
KVHNOTESIDANAPC=anap02
KVHNOTESIDANAPD=anap04
KVHNOTESIDALMLF=alml06

vintella.sudoers_1
Quote:
anap02 KVHPOJANAPC C_POJ_NOTES_START_ANAPC
anap02 KVHPOJANAPC C_POJ_NOTES_STOP_ANAPC
anap04 KVHPOJANAPD C_POJ_NOTES_START_ANAPD
# 6  
Old 06-13-2012
You might try..

Code:
$ 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
$
$

# 7  
Old 06-13-2012
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.

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

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with variables in loop

Hello, please assist: users="test1 test2" keytest1="abcd" keytest2="dbcd" for i in $users do echo "$key${i}" > fileout done So, my objective is to take the current user (ie test1) in loop and echo its associated keyname (ie keytest1) variable to a file. The echo... (2 Replies)
Discussion started by: motdman
2 Replies

2. Shell Programming and Scripting

Loop through variables

I am pretty new to Unix. Trying to pick up some slack while a coworker is out on vacation. Basically the script is working fine however when I go through the testing phase and have to make mods it is a pita. Here is an example of what I have #!/bin/ksh if then echo... (8 Replies)
Discussion started by: biobill
8 Replies

3. Shell Programming and Scripting

Need to loop three variables

Hi, I have a out from a command i need to grep a report. For that i need loop 3 variable for that. How i can loop need help. Symmetrix ID : 123456 Masking View Name : Host16 Last updated at : 04:13:06 PM on Thu Mar 17,2011 Initiator Group Name : Host16 Host... (3 Replies)
Discussion started by: ranjancom2000
3 Replies

4. Shell Programming and Scripting

for loop with 2 variables

i am having a file contants as below my requirement is for file in `awk -F "," '{print $8,$9}'` <temp.txt echo "$file" echo "$file">test.txt a=`awk -F "," '{print $1}' `<test.txt b=`awk -F "," '{print $2}' `<test.txt but script reads , i want both the vales for further... (5 Replies)
Discussion started by: sagar_1986
5 Replies

5. Shell Programming and Scripting

Help with a For loop and variables

Greetings. I'm completely new to shell scripting and quickly trying to catch on. Here's my scenario: I have a text file, named ip.txt, containing IP addresses. I want to automatically perform a whois query on each address in the file, search the output for the country, and then put both the IP... (4 Replies)
Discussion started by: molnir
4 Replies

6. Shell Programming and Scripting

Need help in for loop with 2 variables

Hi, I need help on for loop need to add domain and IP In domain list 1.com 2.com 3.com In Ip list 1.1.0.1 1.2.0.1 1.3.0.1 1.com 1.1.0.1 2.com 1.2.0.1 3.com 1.3.0.1 I need to excute this command (4 Replies)
Discussion started by: ranjancom2000
4 Replies

7. Shell Programming and Scripting

SH: two variables in for loop

Hi, say I have a simple sh script like this: for i in a b c d do for j in 1 2 3 4 do echo "$i $j" done done and the output is a 1 a 2 a 3 a 4 b 1 (20 Replies)
Discussion started by: marcpascual
20 Replies

8. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

9. Shell Programming and Scripting

Is there a better way I could have run this loop. (For loop with two variables)

Sorry for such a dreadful title, but I'm not sure how to be more descriptive. I'm hoping some of the more gurutastic out there can take a look at a solution I came up with to a problem, and advice if there are better ways to have gone about it. To make a long story short around 20K pieces of... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

10. Shell Programming and Scripting

using variables outside a while loop

Hi Guys, I have a scripts that uses a while loop to read a file and set 2 variables. How can I do this so the variables can be used outside the while loop ? Below is an example....# ./junk2 -m -e user EXE=user master=TRUE DB_TAG=PRODUCT In loop MST=MST=testsvr1:3110 In loop ARGS=... (2 Replies)
Discussion started by: Tornado
2 Replies
Login or Register to Ask a Question