sed doubt - search and substitute string from variable.

hi,
trying to learn more abt sed :frowning:

i want to substitute a variable(a) with other variable(b) appended.

Read.txt contains:

home/test2/abc
home/test/root1

input.txt contains:

make test "home/test1/none"version="1.3"
wt's wrong test "home/test2/abc"version="1.0"
make save "home/test/root1"version="1.2"
make try "home/test3/user1"version="1.4"

code tried:

a=`cat read.txt`
for i in $a
do
b=_EDITED
sed -e "/$a/s///$a/$b/" input.txt
done

expected output:

make test "home/test1/none"version="1.3"
wt's wrong test "home/test2/abc_EDITED"version="1.0"
make save "home/test/root1_EDITED"version="1.2"
make try "home/test3/user1"version="1.4"

Thanks in advance :b:

#!/usr/bin/env ruby 
a=File.read("read.txt").split("\n")
File.open("input.txt").each do |x|
  a.each{|i| x.gsub!(/#{i}/,"#{i}_EDITED") }
  puts x
end

hi kurumi,
Thanks for ur solution.
I would like to go with sed only, i noticed typo error .sed address pattern shd be $i in my previous post .
still i didnt get the expected output.

try this,

#!/bin/sh

cp input.txt temp_forum.txt
a=`cat read.txt`

for i in $a
do
if [ -f temp1_forum.txt ]
then
cp temp1_forum.txt temp_forum.txt
fi
b=_EDITED
c=$(echo $i | sed 's/\//\\\//g')
sed  '/'$c'/s//'$c$b'/' temp_forum.txt > temp1_forum.txt
done
cat temp1_forum.txt
rm temp_forum.txt temp1_forum.txt

1 Like

Or:

b=_EDITED

while read i
do
  sed "s!$i!$i$b!" input.txt > temp
  mv temp input.txt
done < read.txt

If your sed version supports the -i option:

b=_EDITED

while read i
do
  sed -i "s!$i!$i$b!" input.txt
done < read.txt
1 Like
awk 'NR==FNR{a[$1];next} {for (i in a) {if ($0~i) sub(i,i "_EDITED")}}1' Read.txt input.txt
1 Like

hi,
thanks to everyone.
i have tested pravin & franklin solution.It worked . i hope other two solutions also works.

I will be more happy if you could explain how it works ?

sed -e "/$a/s///$a/$b/"

Am i not saying address & search pattern are same and substitute where $a is found with $a$b.

Thanks a lot in advance for explaining your sed code logic :b:

b=_EDITED

while read i
do
  sed "s!$i!$i$b!" input.txt > temp
  mv temp input.txt
done < read.txt

The sed code is straightforward...

sed "s!$i!$i$b!"

replaces the variable $i with $i$b.

hi pravin ,franklin &rdcwayx,

i noticed one error when i tried the solution for the following input:
read.txt contains:

home/test/root1
home/test/root1_try
home/test/root1_ATB

Actual output:

make save "home/test/root1_EDITED"version="1.2"
test repo "home/test/root1_EDITED_try"version="1.2.1"
make prod "home/test/root1_EDITED_ATB"version="1.2.2"

Expected output:

make save "home/test/root1_EDITED"version="1.2"
test repo "home/test/root1_try_EDITED"version="1.2.1"
make prod "home/test/root1_ATB_EDITED"version="1.2.2"

with rdcwayx solution,actual ouput i got as follows:

make save "home/test/root1_EDITED"version="1.2"
test repo "home/test/root1_EDITED_try_EDITED"version="1.2.1"
make prod "home/test/root1_EDITED_ATB_EDITED"version="1.2.2"

i understood pravin27 logic. i didn't get the logic of using negation!Is it not something similar to grep -v?

---------- Post updated at 07:03 AM ---------- Previous update was at 06:11 AM ----------

i think i fixed it as follows:

sed  '/'$c\"'/s//'$c$b\"'/' temp_forum.txt > temp1_forum.txt
# ./justdoit file
make test "home/test1/none"version="1.3"
wt's wrong test "home/test2/abc_EDITED"version="1.0"
make save "home/test/root1_EDITED"version="1.2"
make try "home/test3/user1"version="1.4"
# cat justdoit
#!/bin/bash
sed "" read.txt | while read -r l;
 do
newl=$(echo "$l"| sed 's@/@\\/@g')
sed "s/\(.*\)\($newl\)\(.*\)/\1\2_EDITED\3/" $1 > filetmp
mv filetmp $1
 done
more $1