Finding/replacing strings in some files based on a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding/replacing strings in some files based on a file
# 1  
Old 07-11-2012
Question Finding/replacing strings in some files based on a file

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

So for all .sql files, i want to replace the string 'old_one' with 'new_one'.
Is it possible this? If so, how?

Thanks!
# 2  
Old 07-11-2012
Try this...

Code:
ls -l *.csv | awk '{print $9}' > csvfiles.txt
cat csvfiles.txt  | while read LINE
do
   sed 's/oldstring/newstring/g' $LINE > $LINE.txt
   mv $LINE.txt $LINE
done


Last edited by Scrutinizer; 07-11-2012 at 07:53 AM.. Reason: code tags
# 3  
Old 07-12-2012
But what's that $LINE.TXT? I did not want to create another file, but in the original file to replace what's in the first column, with the content of the second one
I have only one csv, so what this does?
ls -l *.csv
My csv file is examples.csv

---------- Post updated at 11:46 PM ---------- Previous update was at 02:17 PM ----------

any help, please? regarding to previous post

---------- Post updated 12-07-12 at 07:46 AM ---------- Previous update was 11-07-12 at 11:46 PM ----------

Also, who is oldstring / newstring?
# 4  
Old 07-12-2012
What does ur examples.csv contain?

You just want to replace "old_one" with "new_one" in that csv file alone or all of the sql files?

oldstring/newstring means oldstring could be anything that you want to replace. And newstring is the string that you want the older one to be replaced with Smilie
# 5  
Old 07-12-2012
My csv looks like in attachament. I want to replace what i found in the first column with the second one.. that's why i asked how should be specified this in that script, the content of the first column, and the one of the second one.
# 6  
Old 07-12-2012
So, you want to replace
Code:
raise_application_error(-20100, 'An error!'); => new_string1
raise_application_error(-20100, 'Another error!'); => new_string2

right?
# 7  
Old 07-12-2012
Exactly, in all .sql files within the current directory (and subdirectories), where i find
raise_application_error(-20100, 'An error!');it should be replaced with new_string1,

similar with the second line , and so on... i can have many lines in that file.
Thanks!

---------- Post updated at 02:03 PM ---------- Previous update was at 09:00 AM ----------

any help, please?

Last edited by Talkabout; 07-12-2012 at 04:50 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Finding similar strings between two files

Hi, I have a file1 like this: ABAT ABCA1 ABCC1 ABCC5 ABCC8 ABCE1 ABHD2 ABL1 CAMTA1 ACBD3 ACCN1 And I have a second file like this: chr19 46118590 46119564 MACS_peak_1499 3100.00 chr19 46122009 46148405 CYP2B7P1 -2445 chr1 7430312 7430990... (7 Replies)
Discussion started by: a_bahreini
7 Replies

2. Shell Programming and Scripting

Replacing strings in various files

i'm trying to figure out the easiest way to replace a string: pineapple pineapple-reg basketball basketball-reg football foot-reg-ball i'm storing the above in a file called wordstoreplace.txt for each line above, the word in the first column is to be replaced by the word in the second... (4 Replies)
Discussion started by: SkySmart
4 Replies

3. Shell Programming and Scripting

Finding a text in files & replacing it with unique strings

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> ... (5 Replies)
Discussion started by: gordom
5 Replies

4. Shell Programming and Scripting

Finding a format in a file and replacing it

Hi, I need help in the following: I have a file in directory with mutiple comma seperated values. One of the value is a date and time format like 2012-04-10 xx:yy:zz I need to find that time format in the file and then replace it with xx:yy+1:zz and then save it as a new file and copy it to a... (3 Replies)
Discussion started by: rabh
3 Replies

5. Shell Programming and Scripting

Extended replacing of nonspecific strings in text files [beware complicated !]

Well, to make another post at this helpful forum :b::D: I recently tried something like this, I want to replace all those numberings/letters that are located between <string>file://localhost/var/mobile/Applications/ and /Documents/</string> numberings =---- replace with: first... (6 Replies)
Discussion started by: pasc
6 Replies

6. Shell Programming and Scripting

Help with Awk finding and replacing a field based on a condition

Hi everybody, I'm trying to replace the $98 field with "T" if the last field (108th) is T I've tried awk 'BEGIN{OFS=FS="|"} {if ($108=="T")sub($98,"T"); print}' test.txt but that doesn't do anything also tried awk 'BEGIN{OFS=FS="|"}{ /*T.$/ sub($98,"T")} { print}' test.txt but... (2 Replies)
Discussion started by: jghi123
2 Replies

7. Shell Programming and Scripting

Replacing Strings in a File

I have a input file which looks like this: Value1="" Value2="" Value3="" ListOfValues=" $Value1 $Value2 $Value3" I have another program which computes the values ($val1, $val2, $val3). So if $val1 is 'A', $val2 is 'B' and $val3 is 'C', I should edit the input file so it will look like:... (6 Replies)
Discussion started by: laiko
6 Replies

8. Shell Programming and Scripting

Replacing Strings in a File

I have a input file which looks like this: Value1="" Value2="" Value3="" ListOfValues=" $Value1 $Value2 $Value3" I have another program which computes the values ($val1, $val2, $val3). So if $val1 is 'A', $val2 is 'B' and $val3 is 'C', I should edit the input file so it will look like:... (0 Replies)
Discussion started by: laiko
0 Replies

9. Shell Programming and Scripting

Replacing strings in csv file.

Hi, I have a problem.. 1) I have a file that contains the lines as below : VRF-TM_DummyLab/mse02.lab,mse02.lab,ge-2/0/7.222 VRF-EMS_HUAWEI_MSAN_208/mse01.lab,mse01.lab,xe-1/0/0.208 2) I need a method to read this file, line by line from :... (5 Replies)
Discussion started by: msafwan82
5 Replies

10. Shell Programming and Scripting

replacing strings with text from other file

Hi, Im trying to update some properties files with text from another file: file1 user=xyz file2 user= after script file2 user=xyz Im using this reading the $QUARTZURL,ETC... from quartz.properties: echo... (1 Reply)
Discussion started by: mc1392
1 Replies
Login or Register to Ask a Question