Find and replace many values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find and replace many values
# 1  
Old 11-03-2012
Find and replace many values

Dear Friends,

I did the same question before in other thread, but I want to explain a little better my request.

I am looking for a way how to find and replace a values in two files using a reference a file where are the key to replace.

Basically, I want to keep a copy of the original file and make a new one in order to compare at the end that the change was done in the correct columns whitout change the rest of file..Smilie

All files are in the same folder.

Then the files are like this

file name = sw1135g.sps
Code:
S   2657.00   7305.00  2V1     0.0   0     0.0 420124.9 2571324.9 153.7297152126
S   2657.00   7285.00  2V1     0.0   0     0.0 419125.0 2571325.2 154.6297152202

file name = sw1135g.xps
Code:
X  5822      241L   2657.00   7305.002    1  2401   2586.00   7186.00   7425.001
X  5822      241L   2657.00   7305.002  241  4801   2596.00   7186.00   7425.001
X  5822      241L   2657.00   7305.002  481  7201   2606.00   7186.00   7425.001
X  5822      241L   2657.00   7305.002  721  9601   2616.00   7186.00   7425.001
X  5822      241L   2657.00   7305.002  961 12001   2626.00   7186.00   7425.001
X  5822      241L   2657.00   7305.002 1201 14401   2636.00   7186.00   7425.001
X  5822      241L   2657.00   7305.002 1441 16801   2646.00   7186.00   7425.001
X  5822      241L   2657.00   7305.002 1681 19201   2656.00   7186.00   7425.001
X  5822      241L   2657.00   7305.002 1921 21601   2666.00   7186.00   7425.001
X  5822      241L   2657.00   7305.002 2161 24001   2676.00   7186.00   7425.001
X  5822      241L   2657.00   7305.002 2401 26401   2686.00   7186.00   7425.001
X  5822      241L   2657.00   7305.002 2641 28801   2696.00   7186.00   7425.001
X  5822      241L   2657.00   7305.002 2881 31201   2706.00   7186.00   7425.001
X  5822      241L   2657.00   7305.002 3121 33601   2716.00   7186.00   7425.001
X  5822      241L   2657.00   7305.002 3361 36001   2726.00   7186.00   7425.001
X  5822      241L   2657.00   7305.002 3601 38401   2736.00   7186.00   7425.001
X  5822      251L   2657.00   7285.002    1  2401   2586.00   7166.00   7405.001
X  5822      251L   2657.00   7285.002  241  4801   2596.00   7166.00   7405.001
X  5822      251L   2657.00   7285.002  481  7201   2606.00   7166.00   7405.001
X  5822      251L   2657.00   7285.002  721  9601   2616.00   7166.00   7405.001
X  5822      251L   2657.00   7285.002  961 12001   2626.00   7166.00   7405.001
X  5822      251L   2657.00   7285.002 1201 14401   2636.00   7166.00   7405.001
X  5822      251L   2657.00   7285.002 1441 16801   2646.00   7166.00   7405.001
X  5822      251L   2657.00   7285.002 1681 19201   2656.00   7166.00   7405.001
X  5822      251L   2657.00   7285.002 1921 21601   2666.00   7166.00   7405.001
X  5822      251L   2657.00   7285.002 2161 24001   2676.00   7166.00   7405.001
X  5822      251L   2657.00   7285.002 2401 26401   2686.00   7166.00   7405.001
X  5822      251L   2657.00   7285.002 2641 28801   2696.00   7166.00   7405.001
X  5822      251L   2657.00   7285.002 2881 31201   2706.00   7166.00   7405.001
X  5822      251L   2657.00   7285.002 3121 33601   2716.00   7166.00   7405.001
X  5822      251L   2657.00   7285.002 3361 36001   2726.00   7166.00   7405.001
X  5822      251L   2657.00   7285.002 3601 38401   2736.00   7166.00   7405.001

filename = datatochange.txt ( reference file )

Code:
Code:
sw1135g.sps 2657.00   7305.00 2 1
sw1135g.sps 2657.00   7305.00 2 1
sw1135g.xps 2657.00   7285.00 2 1
sw1135g.xps 2657.00   7285.00 2 1

Then the desired output

file name = sw1135g.sps

Code:
Code:
S   2657.00   7305.00  1V1     0.0   0     0.0 420124.9 2571324.9 153.7297152126
S   2657.00   7285.00  1V1     0.0   0     0.0 419125.0 2571325.2 154.6297152202

The changed was done in colum 24, value 2 has been replaced by value 1, acoording to file datatochange.txt $5
looking and taking as reference 2657.00 7305.00

file name = sw1135g.xps

Code:
Code:
X  5822      241L   2657.00   7305.001    1  2401   2586.00   7186.00   7425.001
X  5822      241L   2657.00   7305.001  241  4801   2596.00   7186.00   7425.001
X  5822      241L   2657.00   7305.001  481  7201   2606.00   7186.00   7425.001
X  5822      241L   2657.00   7305.001  721  9601   2616.00   7186.00   7425.001
X  5822      241L   2657.00   7305.001  961 12001   2626.00   7186.00   7425.001
X  5822      241L   2657.00   7305.001 1201 14401   2636.00   7186.00   7425.001
X  5822      241L   2657.00   7305.001 1441 16801   2646.00   7186.00   7425.001
X  5822      241L   2657.00   7305.001 1681 19201   2656.00   7186.00   7425.001
X  5822      241L   2657.00   7305.001 1921 21601   2666.00   7186.00   7425.001
X  5822      241L   2657.00   7305.001 2161 24001   2676.00   7186.00   7425.001
X  5822      241L   2657.00   7305.001 2401 26401   2686.00   7186.00   7425.001
X  5822      241L   2657.00   7305.001 2641 28801   2696.00   7186.00   7425.001
X  5822      241L   2657.00   7305.001 2881 31201   2706.00   7186.00   7425.001
X  5822      241L   2657.00   7305.001 3121 33601   2716.00   7186.00   7425.001
X  5822      241L   2657.00   7305.001 3361 36001   2726.00   7186.00   7425.001
X  5822      241L   2657.00   7305.001 3601 38401   2736.00   7186.00   7425.001
X  5822      251L   2657.00   7285.001    1  2401   2586.00   7166.00   7405.001
X  5822      251L   2657.00   7285.001  241  4801   2596.00   7166.00   7405.001 
X  5822      251L   2657.00   7285.001  481  7201   2606.00   7166.00   7405.001
X  5822      251L   2657.00   7285.001  721  9601   2616.00   7166.00   7405.001
X  5822      251L   2657.00   7285.001  961 12001   2626.00   7166.00   7405.001 
X  5822      251L   2657.00   7285.001 1201 14401   2636.00   7166.00   7405.001
X  5822      251L   2657.00   7285.001 1441 16801   2646.00   7166.00   7405.001
X  5822      251L   2657.00   7285.001 1681 19201   2656.00   7166.00   7405.001
X  5822      251L   2657.00   7285.001 1921 21601   2666.00   7166.00   7405.001 
X  5822      251L   2657.00   7285.001 2161 24001   2676.00   7166.00   7405.001 
X  5822      251L   2657.00   7285.001 2401 26401   2686.00   7166.00   7405.001 
X  5822      251L   2657.00   7285.001 2641 28801   2696.00   7166.00   7405.001
X  5822      251L   2657.00   7285.001 2881 31201   2706.00   7166.00   7405.001
X  5822      251L   2657.00   7285.001 3121 33601   2716.00   7166.00   7405.001 
X  5822      251L   2657.00   7285.001 3361 36001   2726.00   7166.00   7405.001
X  5822      251L   2657.00   7285.001 3601 38401   2736.00   7166.00   7405.001

The changed was done in colum 38, value 2 has been replaced by value 1, acoording to file datatochange.txt $5
looking and taking as reference 2657.00 7305.00

In the reference file datatochange.txt, I have wrote the name of the file where the changes need to be done.

the filenames in "datatochange.txt" are like
sw1135g.sps
sw1135g.xps
which are my inputs...
Then, I would like to keep a copy of these files changing the name to
sw1135gOLD.sps
sw1135gOLD.xps
And to get the new ones as: ( where we did the changes --our output files ) should be:
sw1135g.sps
sw1135g.xps

I have many files to change it is only a example for one case. previously I didn't explain that I have many values to change in the same input file,, And looks like using the command get line we can replace only one line of the file ?

I got this from Mr elixier;

With some assumptions:

Code:
Code:
awk '{file=$1 searchstr=$2 " " $3;sstrlen=length(searchstr) fromthis=$4;
tothat=$5 split(file,a,/[.]/);newfile=a[1] "new." a[2] while(getline < file)
 {  if((strt=index($0,searchstr)))  {   p1=substr($0,1,strt+sstrlen-1) 
  p2=substr($0,strt+sstrlen)   sub(fromthis,tothat,p2)   $0=p1 p2  }
  print > newfile }}' datatochange.txt

Works perfect,, but only for one line...

Please try to help me

Any help on this will be strongly appreciated.

Thanks in advance Smilie

Last edited by jiam912; 11-03-2012 at 05:21 PM.. Reason: format of column
# 2  
Old 11-03-2012
please provide a full and representative example of the contend of "datatochange.txt" file

Are the entries of "datatochange.txt" sorted ?
I mean, are all the replacement rules gathered by filename ?
do you only have 2 types of files (*.xps and *.sps)?

---------- Post updated 2012-11-04 at 01:09 AM ---------- Previous update was 2012-11-03 at 09:14 PM ----------

Please avoid duplicate thread.

You could give a try to this quick solution:

Code:
awk '{F=$1;a[F];e=(F~/sps$/)?" ":z;s=$2" "$3;p=$4;r=$5 print "s/" s e p "/" s e r "/" >> F".sed" }END{ for(i in a){print "sed -f "i".sed "i " >>"i".new">>"change2run"} }' datatochange.txt

1) run the code above
2) then run the generated script "change2run"

This should generate *.new files with all the substitutions done in it.
and "datatochange.txt" can have multiple line with a same filenames, this code should be able to handle it properly

Last edited by ctsgnb; 11-03-2012 at 06:01 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find duplicate values in specific column and delete all the duplicate values

Dear folks I have a map file of around 54K lines and some of the values in the second column have the same value and I want to find them and delete all of the same values. I looked over duplicate commands but my case is not to keep one of the duplicate values. I want to remove all of the same... (4 Replies)
Discussion started by: sajmar
4 Replies

2. Shell Programming and Scripting

How to find the X highest values in a list depending on the values of another list with bash/awk?

Hi everyone, This is an exemple of inpout.txt file (a "," delimited text file which can be open as csv file): ID, Code, Value, Store SP|01, AABBCDE, 15, 3 SP|01, AABBCDE, 14, 2 SP|01, AABBCDF, 13, 2 SP|01, AABBCDE, 16, 3 SP|02, AABBCED, 15, 2 SP|01, AABBCDF, 12, 3 SP|01, AABBCDD,... (1 Reply)
Discussion started by: jeremy589
1 Replies

3. Shell Programming and Scripting

Find and replace values using a list from a file

Hello Guys. I have a very big file where I need to change some values according a list on other file. The following file is were I have the values to be changed. The field where I need to replace the values is File_Nb : File1.txt Obs_Report_Result : # ===== (5)... (19 Replies)
Discussion started by: jiam912
19 Replies

4. Shell Programming and Scripting

Find a blank field and replace values to NA

Hi All, i have a file like col1 col2 col3 13 24 NA 12 13 14 11 12 13 14 22 NA 18 26 NA in this file if i found "NA" other values in the line are also replace by NA Could you help me! (7 Replies)
Discussion started by: Shenbaga.d
7 Replies

5. Shell Programming and Scripting

find values between values in two different fields

Hi, I need help to find values between two different fields based on $6 (NUM) AND $1 (CD), within the same ID. The result should show the values between the NUMs which will be extracted from within $3 and $2 in data.txt file below. data.txt ex 139 142 Sc_1000004 ID 4 CD ... (2 Replies)
Discussion started by: redse171
2 Replies

6. UNIX for Dummies Questions & Answers

Find and Replace based on values in an file

I have a file in which I want to do multiple find and replace of strings. For a single replace I can implement: sed -i 's/old/new/' <input_file> I have a second file that contains the old and the new values like the arbitrary example below: old new xyz pqr ab 756 rst pqr... (3 Replies)
Discussion started by: Gussifinknottle
3 Replies

7. Shell Programming and Scripting

Find and replace duplicate column values in a row

I have file which as 12 columns and values like this 1,2,3,4,5 a,b,c,d,e b,c,a,e,f a,b,e,a,h if you see the first column has duplicate values, I need to identify (print it to console) the duplicate value (which is 'a') and also remove duplicate values like below. I could be in two... (5 Replies)
Discussion started by: nuthalapati
5 Replies
Login or Register to Ask a Question