How to find a certain string in a file and replace it with a value from another file using sed/awk?

Hi Everyone,
I am new to this forum and new to sed/awk programming too !!
I need to find particular string in file1(text file) and replace it with a value from another text file(file2) the file2 has only one line and the value to be replaced with is in the second column.

file 1:
(assert (= c OUT))

file 2:
OUT 0

NOW, I HAVE TO REPLACE 'OUT' WITH THE VALUE 0.
please pleeaseee tell me how to do this using sed/awk or any other way!!! am in distress!! HELP

 
key=$(awk '{print $1}' file2)
value=$(awk '{print $2}' file2)
sed -i "s/$key/$value/g" file1
1 Like

maybe something like this ?

while read a b
do
    sed "s/$a/$b/g" file1 >output
    cat output >file1
done <file2

or

while read a b
do
    sed -i "s/$a/$b/g" file1
done <file2
1 Like

Guys thanx for replying!!:smiley:
But will these solve my problem? since where exactly are we searching the string 'OUT' here? I actually have similar other strings to search and replace with other values from other files.. actually this is the case:

file 1:

(define argc :: int)
(assert (> argc 1))
(assert (= argc ARG))
<check>

(define argc :: float)
(assert (> c 0))
(assert (= c OUT))
<check>

.
.
.

file2:
main 1

file 3:
main 0
.
.
.
Now, I have to find the string 'ARG' from file 1 and replace it with the value '1' from file 2. And, find 'OUT' and replace it with the value '0' from file 3.

Now can you put some more light on it? :o:confused:

If you have too many files like file2, file3,....file50
Then all above files can be concatenated into one say a temp file..
then use temp file and change values in file1.
Then go with ctsgnb in post #3

1 Like

I had a file which had all this data... I had separated it only :stuck_out_tongue:
actually the data in file 2, 3, 4 etc are divided into 2 files... i have to merge them. okay i will see this and get back to you. thanx a ton!! :))

---------- Post updated at 11:50 PM ---------- Previous update was at 11:48 PM ----------

does the link u gave show how to concatenate files into a single?

---------- Post updated 12-17-10 at 12:06 AM ---------- Previous update was 12-16-10 at 11:50 PM ----------

ques---->
does a and b signify the strings i want to find?

In

 
while read a b

a is first string (i.e. OUT) and b is 2nd string (i.e. 0)
and in

 
sed -i "s/$a/$b/g" file1

$a will be replaced by $b

If you don't have file2, file3, ..., file50 concatenate into one already then you need few lines of script to do that 1st. If you are facing problem in this, pls tell all such files and there names (name format)

1 Like

Hi Anurag,
I have two files:

Stack.txt:

main: 1
function1: 1
function2: 1

and

Output.txt:

main: 0
function1: 1
function2: 0

I want to concatenate them into a single file say:

Data.txt:

main: 1
function1: 1
function2: 1
main: 0
function1: 1
function2: 0

Now what exact command to use? :o I am a total novice !

Just append output.txt file to Stack.txt file.

cat Stack.txt > Data.txt
cat Output.txt >> Data.txt

I read all the posts over here, but I still don't understand what exactly u want

Hi.. thx so much for the quick reply!! I just want to separate the blocks and send it as an argument to a tool(yices). a shell script for that i want.. i am new to linux environ..
basically i am developing a diagnostic tool..(a debugging project)
i need to do some text editing and send the text to yices(whole file). if the result is 'unsat' i need to separate the blocks and send each to diagnose where the problem lies.. Now you got it? does it make sense? i have other problems too.. will post another thread for that :stuck_out_tongue:

First column value in both Stack.txt and Output.txt are same. I hope this is not the case. If same, then value in 1st file (Stack.txt) will be in final output.

 
cat Stack.txt Output.txt > Data.txt
while read a b
do
    sed -i "s/$a/$b/g" file1
done <Data.txt

If -i switch in sed doesn't wrok, then you need to you a temp file as below:

sed "s/$a/$b/g" file1 >temp; mv temp file1

hi anurag,
u r right... it will be like this:
data.txt:

main: ARG 1
function1: F1 1
function2: F1' 1
main: OUT 0
function1: F2 1
function2: F2' 0

now it becomes little difficult actually:
i have to replace ARG in the first file(see main post) with the corresponding value for ARG ie '1' . similarly for F1, value 1 etc. now ill the previous code work? i have to match the string and get the corresponding value .. how will i do that?

Assuming you want to replace
ARG with 1
F1 with 1
F1' with 1
OUT with 0
etc..

 
cat Stack.txt Output.txt > Data.txt
while read a b c
do
    sed -i "s/$b[']\{0,1\}/$c/g" file1
done <Data.txt

I do not want to replace with fixed values. I have to search for the string OUT in the new file(data.txt) and replace the OUT in file1 with the corresponding value(it may be 0/1/2/3 , anything)
that is have to grep for OUT in the file and find the value that is beside it!!:confused:

Right. That's what above script is doing.
It's reading Data.txt and putting 1st column value in a , 2nd column value in b and 3rd column value in c.
Then all occurences of b in file1 is being replaced by c.
I hope this is what you want.

1 Like

yes i think this should work!! thanks so muchh!! i will write back if i am stuck anywhere else... cheerzzz:D

Anurag,
in the code u hgave used {0,1}, but if my value is anything, what will be in the generic case?

cat Stack.txt Output.txt > Data.txt
while read a b
do
    sed -i "s/$a[']\{0,1\}/$b/g" file1
done <Data.txt

Yes. a, b ,c are variables.
If only two columns, then remove 3rd variable c.
Now all occurences of a in file1 is being replaced by b.

As poer your earlier post #13.. some of values are having single quote.
If no single quote in column1, then sed will be simpler

 
sed -i "s/$a/$b/g" file1
1 Like

yes it is working... but if the values are anything... not only 0 and 1 (given in ur code) what will i write?

Below Regular expresion tells that single quote may appear ZERO (means not there at all) OR ONE time.

[']\{0,1\}

This is to read F1 OR F1' in variable a

F1 1
F1' 1
1 Like