Find and replace value using a key from other file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find and replace value using a key from other file
# 1  
Old 11-03-2012
Find and replace value using a key from other file

Dear Friends,

I am looking for a way how to find and replace a value 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 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:
sw1135g.sps 2657.00   7305.00 2 1
sw1135g.xps 2657.00   7305.00 2 1

Then the desired output

file name = sw1135gnew.sps
Code:
S   2657.00   7305.00  1V1     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

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 = sw1135gnew.xps
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.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

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 output name for the new file need to be different. (sw1135gnew.xps example)

I have many files to change it is only a example for one case.

Please let me know if there is other way to do it,

Any help on this will be strongly appreciated.

Thanks in advance Smilie

Last edited by Scott; 11-03-2012 at 07:42 AM.. Reason: Code tags
# 2  
Old 11-03-2012
With some assumptions:
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

I accept that this is not a good solution (you might end up replacing wrong values in some cases). But then your lookup file provides no indication about the position/s to attempt search/substitution. In one file, the search is supposed to start at the 24th column and at the 38th column in the other.

Last edited by elixir_sinari; 11-03-2012 at 05:36 AM..
# 3  
Old 11-03-2012
ELIXIER,

THE POINT IS TO FIND THE VALUE 2657.00 7305.00 IN THE FILE sw1135g.sps AND REPLACE in colum 24, value 2 WITH value 1, acoording to file datatochange.txt TAKING THE field $5 AS THE VALUE TO CHANGE AND THE field $2 field $3 AS reference 2657.00 7305.00

filename = datatochange.txt ( reference file )
sw1135g.sps 2657.00 7305.00 2 1

Regards

---------- Post updated at 03:25 AM ---------- Previous update was at 03:00 AM ----------

ELIXIER,

how I can send the files to you, because ,when I post the files here , does not keep the good format and this cause the confusion regarding to columns..

Thansk for your hel Smilie
# 4  
Old 11-03-2012
Quote:
Originally Posted by jiam912
ELIXIER,

THE POINT IS TO FIND THE VALUE 2657.00 7305.00 IN THE FILE sw1135g.sps AND REPLACE in colum 24, value 2 WITH value 1, acoording to file datatochange.txt TAKING THE field $5 AS THE VALUE TO CHANGE AND THE field $2 field $3 AS reference 2657.00 7305.00

filename = datatochange.txt ( reference file )
sw1135g.sps 2657.00 7305.00 2 1

Regards

---------- Post updated at 03:25 AM ---------- Previous update was at 03:00 AM ----------

ELIXIER,

how I can send the files to you, because ,when I post the files here , does not keep the good format and this cause the confusion regarding to columns..

Thansk for your hel Smilie
Please go back and look at your initial posting to this thread. You said that the datatochange.txt line:
Code:
sw1135g.sps 2657.00 7305.00 2 1

was supposed to change the line:
Code:
S 2657.00 7305.00 2V1 0.0 0 0.0 420124.9 2571324.9 153.7297152126

to:
Code:
S 2657.00 7305.00 1V1 0.0 0 0.0 420124.9 2571324.9 153.7297152126

which is a change in output column 19 (not 24).
And you said that the datatochange.txt line:
Code:
sw1135g.xps 2657.00 7305.00 2 1

was supposed to change the line:
Code:
X 5822 241L 2657.00 7305.002 1 2401 2586.00 7186.00 7425.001

and several similar lines to:
Code:
X 5822 241L 2657.00 7305.001 1 2401 2586.00 7186.00 7425.001

and several similar lines all with changes in output column 28 (not 38).

Did you look at the header above the text box where you entered your question? Among other things it says:
Quote:
Do you have any code fragments or data samples in your post? If so wrap them in code tags using the code tag button in the editor below (hint: it looks like this -->Image)
If you mean that your input had tabs or multiple spaces in it that you didn't protect with code tags; that might explain why elixir_sinari gave you changes to the wrong output column for the changes to these files. If you want a general solution, you'll have to specify the output column to be changed in the datatochange.txt file and use code tags when showing us sample input and output files.

Last edited by Don Cragun; 11-03-2012 at 06:10 AM..
# 5  
Old 11-03-2012
INPUT 1
file name = sw1135g.sps
Code:
S   2657.00   7290.00  2V1     0.0   0     0.0 419375.1 2571324.6 155.1297150343

---------- Post updated at 05:10 AM ---------- Previous update was at 05:08 AM ----------

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

---------- Post updated at 05:21 AM ---------- Previous update was at 05:10 AM ----------

REFERENCE
filename = datatochange.txt ( reference file )
Code:
sw1135g.sps 2657.00 7290.00 2 1

---------- Post updated at 05:21 AM ---------- Previous update was at 05:21 AM ----------

REFERENCE
filename = datatochange.txt ( reference file )
Code:
sw1135g.sps 2657.00 7290.00 2 1

---------- Post updated at 05:21 AM ---------- Previous update was at 05:21 AM ----------

OUTPUT 1
file name = sw1135gnew.sps

Code:
H26      1         2         3         4         5         6         7         8
S   2657.00   7290.00  1V1     0.0   0     0.0 419375.1 2571324.6 155.1297150343

---------- Post updated at 05:22 AM ---------- Previous update was at 05:21 AM ----------

OUTPUT 2
file name = sw1135gnew.xps

Code:
H26 5678901234567890123456789012345678901234567890123456789012345678901234567890
H26      1         2         3         4         5         6         7         8
X  5822       91L   2657.00   7290.001    1  2401   2586.00   7171.00   7410.001
X  5822       91L   2657.00   7290.001  241  4801   2596.00   7171.00   7410.001
X  5822       91L   2657.00   7290.001  481  7201   2606.00   7171.00   7410.001
X  5822       91L   2657.00   7290.001  721  9601   2616.00   7171.00   7410.001
X  5822       91L   2657.00   7290.001  961 12001   2626.00   7171.00   7410.001
X  5822       91L   2657.00   7290.001 1201 14401   2636.00   7171.00   7410.001
X  5822       91L   2657.00   7290.001 1441 16801   2646.00   7171.00   7410.001
X  5822       91L   2657.00   7290.001 1681 19201   2656.00   7171.00   7410.001
X  5822       91L   2657.00   7290.001 1921 21601   2666.00   7171.00   7410.001
X  5822       91L   2657.00   7290.001 2161 24001   2676.00   7171.00   7410.001
X  5822       91L   2657.00   7290.001 2401 26401   2686.00   7171.00   7410.001
X  5822       91L   2657.00   7290.001 2641 28801   2696.00   7171.00   7410.001
X  5822       91L   2657.00   7290.001 2881 31201   2706.00   7171.00   7410.001
X  5822       91L   2657.00   7290.001 3121 33601   2716.00   7171.00   7410.001
X  5822       91L   2657.00   7290.001 3361 36001   2726.00   7171.00   7410.001
X  5822       91L   2657.00   7290.001 3601 38401   2736.00   7171.00   7410.001

---------- Post updated at 05:24 AM ---------- Previous update was at 05:22 AM ----------

Thanks Don,, I have post again the data whit the correct format..

---------- Post updated at 05:25 AM ---------- Previous update was at 05:24 AM ----------

THE POINT IS TO FIND THE VALUE 2657.00 7290.00 IN THE FILE sw1135g.sps AND REPLACE in colum 24, value 2 WITH value 1, acoording to file datatochange.txt TAKING THE field $5 AS THE VALUE TO CHANGE AND THE field $2 field $3 AS reference 2657.00 7290.00

---------- Post updated at 05:35 AM ---------- Previous update was at 05:25 AM ----------

REFERENCE
filename = datatochange.txt ( reference file )
Code:
sw1135g.sps 2657.00   7290.00 2 1
sw1135g.xps 2657.00   7290.00 2 1

sorry I forgot to put the file sw1135g.xps inside of file datatochange.txt
Regards
# 6  
Old 11-03-2012
Just another way to do it. (if multiple space are shrinked into a single one)

Code:
awk '{F=$1;e=(F~/sps$/)?" ":z;s=$2" "$3;p=$4;r=$5;
while(getline <F){sub(s e p,s e r,$0);print $0 > F".new"};close (F)
}' datatochange.txt

# 7  
Old 11-03-2012
it does not work....Smilie

---------- Post updated at 10:17 AM ---------- Previous update was at 08:02 AM ----------

Hi the code from Elixier is. Working fine .

Please how keep the name of the input file and output file as following

Example
Imput file= sw1135g.sps to change to sw1135gOLD.sps

Output file=sw1135g.sps


Many thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need to find and replace in a file

Hi All, I am having below sample data in a file. I need to find all the line form this file with word ABC and i need to replace the characters at position 120 which is "CO:BOGFDUI"(30chars) in the lines with blank space. I have tried using grep to find the word with ABC (grep ABC filename),... (3 Replies)
Discussion started by: abhi_123
3 Replies

2. Shell Programming and Scripting

Find Key and replace value

Hi, I am new to shell scripting. I have a config file where key and value is stored as below. In my shell script, I want to look for Test ID in the config file and replace the value 1 with another value stored in a variable. How would I do that ? <Config Key="Test ID" Value="1"/> I... (6 Replies)
Discussion started by: Vishnuvardhanh
6 Replies

3. Shell Programming and Scripting

Perl script to read string from file#1 and find/replace in file#2

Hello Forum. I have a file called abc.sed with the following commands; s/1/one/g s/2/two/g ... I also have a second file called abc.dat and would like to substitute all occurrences of "1 with one", "2 with two", etc and create a new file called abc_new.dat sed -f abc.sed abc.dat >... (10 Replies)
Discussion started by: pchang
10 Replies

4. Shell Programming and Scripting

Shell Script @ Find a key word and If the key word matches then replace next 7 lines only

Hi All, I have a XML file which is looks like as below. <<please see the attachment >> <?xml version="1.0" encoding="UTF-8"?> <esites> <esite> <name>XXX.com</name> <storeId>10001</storeId> <module> ... (4 Replies)
Discussion started by: Rajeev_hbk
4 Replies

5. Shell Programming and Scripting

Find and Replace in File

Legends, I have a file /tmp/list.txt I want to find "/bin/" and replace it with "/log/" I tried the follwoing but no luck Sandy: /tmp> perl -pi -e 's/\/bin\/\/log\/' /tmp/list.txt >> /tmp/try Substitution pattern not terminated at -e line 1. AND, Sandy: /tmp> perl -pi -e... (2 Replies)
Discussion started by: sdosanjh
2 Replies

6. Shell Programming and Scripting

C Shell problem: using a key from one file to find data in another

I've never written scripts (just switched from Ada to C++). I have a book that's over my head and a few examples, other then that I'm floundering. Everything here at work is being done in C Shell. None of the C++ programmers are experienced in shell scripting. I have a data file with the... (2 Replies)
Discussion started by: bassmaster
2 Replies

7. Shell Programming and Scripting

Find and replace in a gz file

Is there a way to do a find and replace in a .gz file in a single script ? I can always unzip, find and replace and then zip it again but would hate to do this everytime. Thanks ! Vivek (1 Reply)
Discussion started by: vashah
1 Replies

8. Shell Programming and Scripting

Find and replace in a file

Hi everyone, I am new to the world of shell script programming. I have a file named Fnd1.txt which has the contents as below. I need to replace the \t with the tab space. Can any one help me to write a perl scipt for this. USA45V1\tG\t341029 USAV1T1\tG\t450545 USAREJ1\tG\t572645... (5 Replies)
Discussion started by: vinay123
5 Replies

9. Shell Programming and Scripting

how to replace a text inside a file based on a xml key

<c-param> <param-name>Number</param-name> <param-value>22</param-value> <description>my house number</description> </c-param> <c-param> <param-name>Address</param-name> ... (4 Replies)
Discussion started by: reldb
4 Replies

10. UNIX for Dummies Questions & Answers

Find replace within a file?

I build several files by using the cut command to grab select fields(columns) from a really bid csv file. Each file is one column of data. I then put them together using paste command. Here is the code built in tcsh: cut -d , -f 1 some.csv > 1.csv cut -d , -f 10 some.csv > 10.csv paste 1.csv... (2 Replies)
Discussion started by: yankee428
2 Replies
Login or Register to Ask a Question