Hi,
We have a file (e.g. a .csv file, but could be any other format), with 2 columns: the old value and the new value. We need to modify all the files within the current directory (including subdirectories), so find and replace the contents found in the first column within the file, with the contents of the 2nd column

e.g. the file can contain:

COL1 -- COL2
old_one new_one

That 'new_one' string can contain '!' or single quotation marks, which i should escape them. Is it possible this? To get the value in shell script, from the first column (so, read the csv line by line), and the new value from the second column.
If so, how?

Step 1: form a sed script based on the csv file
Step 2: apply sed to all the files you need to modify

Assuming csv (that is, columns are separated with comma, and comma doesn't appear anywhere neither in old nor in new values), the sed script can be trivially formed with a following sed command:

sed -e 's/\(.*\)/s,\1,g/' file.csv script.sed

Now you may apply it as

for file in list_of_files; do sed -i -f script.sed $file; done

I didn't get the escaping part (what and how should be escaped), but it is surely possible.

I didn't et the escaping part (what and how should be escaped), but it is surely possible.

OK, let's say in the first column i can have "It's too late for this action!", and in the second (replacing string), "Some text here".

Now, when replacing, how can i automatically add the escape "\" character, in front of special characters, when parsing "It's too late for this action!", to find this string in files, and then replace it?

What does exactly this?

sed -e 's/(.*)/s,\1,g/' file.csv script.sed

Thanks

Literally it is an instruction to sed to prepend a line with s, and append ,g to it.
Say, some line in the replacement file reads
foo,bar
Then, after the sed application it'll become
s,foo,bar,g
which in turn is a sed instruction to replace (s) all (globally) appearances of foo to bar.

To escape bangs you may also use sed, this time with s/!/\\!/g instruction.

Thanks for reply.
I decided to write sepparate commands for each file that will be corrected.
For example i have this command:

find ./server -type f -name 'CERINTE_BU.sql' -exec sed -i "s/raise_application_error(-20998, 'Error text/please recheck!');/os_err.raise_err('ERR_1472');--'Error text/please recheck!'/g" {} \;

Well, so raise_application_error(-20998, 'Error text/please recheck!'); should be replaced with os_err.raise_err('ERR_1472');--'Error text/please recheck!'

In this command, what option can i specify and where, to automatically add "\" in front of specal characters?

I tried in this way, but it's not working:

find ./server -type f -name 'CERINTE_BU.sql' -exec sed -i "s/!//\\!/graise_application_error(-20998, 'Error text/please recheck!!');/os_err.raise_err('ERR_1472');--'Error text/please recheck!'/g" {} \;

So in this case, for example, escape characters should be added in front of the every "!", and other special characters

Thanks

Edited 4 Years Ago by sorin.oltean87

This article has been dead for over six months. Start a new discussion instead.