Hallo Everyone.
I have to admit I'm shell scripting illiterate . I need to find certain strings in several text files and replace each of the string by unique & corresponding text.
I prepared a csv file with 3 columns: <filename>;<old_pattern>;<new_pattern>
dominik@dominik-VirtualBox:~/Pulpit/test/1$ cat file.csv
file1.txt;abc;123
file2.txt;XYZ;6789
A very kind guy (much more skilled than me) helped me with the script:
dominik@dominik-VirtualBox:~/Pulpit/test/1$ cat script.sh
for i in `cat file.csv`; do
file=`echo $i | cut -d ";" -f1`;
pattern1=`echo $i | cut -d ";" -f2`;
pattern2=`echo $i | cut -d ";" -f3`;
sed -i "s/$pattern1/$pattern2/" $file;
done
To find & replace text I run the script and grep:
dominik@dominik-VirtualBox:~/Pulpit/test/1$ bash script.sh
dominik@dominik-VirtualBox:~/Pulpit/test/1$ grep . file{1,2}*
With the above file.csv example the script works fine and do what's intended. The problem starts if <old_pattern> and <new_pattern> text have white spaces:
dominik@dominik-VirtualBox:~/Pulpit/test/1$ cat file.csv
file1.txt;abc jkl nm;1 2 3
file2.txt;XYZ rt;67 89
In that case script returns errors.I tried to modify file.csv by putting text between quotation marks but it didn't help. How the script should be adjusted to work with text consisting of white spaces? I would appreciate any help from you. Thank you very much in advance. Regards,
gordom