Please Help to Check script Search and Replace

Please Help to Check script Search and Replace
Ex. Search 0001 and Replete un_0001

---script
Code:

nawk -F\" 'NR==FNR{a[$0];next}$2 in a{sub($2,"un_"$2)}1' input.txt file*.txt > resoult.txt

script is work to one result but
if i have file1.txt, file2.txt, file3.txt i want to Replace Result in old file
please help again
thank you

---file1.txt

Event: "0001","0002","0001","0003"
Event: "0002","0002","0001","0003"
Event: "0003","0002","0001","0003"
Event: "0003","0002","0001","0003"

---file2.txt

Event: "0003","0002","0001","0003"
Event: "0002","0002","0001","0003"
Event: "0001","0002","0001","0003"
Event: "0001","0002","0001","0003"

--- input.txt

0001
0003

---Result
---file1.txt

Event: "un_0001","0002","0001","0003"
Event: "0002","0002","0001","0003"
Event: "un_0003","0002","0001","0003"
Event: "un_0003","0002","0001","0003"

---file2.txt

Event: "un_0003","0002","0001","0003"
Event: "0002","0002","0001","0003"
Event: "un_0001","0002","0001","0003"
Event: "un_0001","0002","0001","0003"

You want to repalce 0001 in the begging or anywhere. Please be specific

you can use sed or tr command to achive this. Otherwise you will write a perl program for the same

1 Like

Hi,

Try this...

#!/bin/sh

file_to_search=file.txt
original_file=$file_to_search

cp $original_file newfile
for da_b in $(cat /test/input.txt)
do
sed "s/Event: \"${da_b}\"/Event: \"un_${da_b}\"/g" $file_to_search > test_$da_b
rm $file_to_search
file_to_search=test_$da_b
done

cat $file_to_search
rm $file_to_search
cp newfile $original_file
rm newfile
1 Like
awk -F\" 'NR==FNR{a[$0];next}$2 in a{sub($2,"un_"$2)}1' input.txt file.txt
1 Like

thank you For Script :smiley: pravin27 and danmero

nawk -F\" 'NR==FNR{a[$0];next}$2 in a{sub($2,"un_"$2)}1' input.txt file*.txt > resoult.txt

if i have file1.txt, file2.txt, file3.txt
script is work to one result but i want to Replace Result in old file
please help again
thank you

---------- Post updated at 01:29 PM ---------- Previous update was at 09:02 AM ----------

thank you for all answer

---------- Post updated at 01:30 PM ---------- Previous update was at 01:29 PM ----------

thank for all answer

Is not safe to edit-in-place, use a temporary file for output and move the temporary file over old file.

1 Like