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"
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.
#!/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
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