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
# 8  
Old 11-03-2012
Quote:
Originally Posted by jiam912
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



Just to illustrate that for me it worked :

Code:
$ rm *.new
$ ls -rt | tail -3
sw1135g.sps
sw1135g.xps
datatochange.txt
$ cat datatochange.txt
sw1135g.sps 2657.00 7305.00 2 1
sw1135g.xps 2657.00 7305.00 2 1
$ cat sw1135g.sps
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
$ cat sw1135g.xps
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
$ awk '{F=$1;e=(F~/sps$/)?" ":z;s=$2" "$3;p=$4;r=$5;
while(getline <F){
sub(s e p,s e r)
print $0 > F".new"
}
close (F)
}' datatochange.txt
$ ls -rt | tail -2
sw1135g.xps.new
sw1135g.sps.new
$ cat sw1135g.sps.new
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
$ cat sw1135g.xps.new
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
$

# 9  
Old 11-03-2012
Hi ctsgnb
Thanks for your help..

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

Thanks and regards
# 10  
Old 11-03-2012
So you mean that in the file datatochange.txt there will be *OLD.[sx]ps filenames right ?
This User Gave Thanks to ctsgnb For This Post:
# 11  
Old 11-03-2012
Hi ctsgnb

Thansk again the code works perfect... was my mistake

---------- Post updated at 12:25 PM ---------- Previous update was at 12:17 PM ----------

Hi ctsgnb

I want to keep a copy of the original input file sw1135g.sps to rename as sw1135gOLD.sps, and the the new one with the changes done will be called sw1135g.sps.

It shoulbe done for both files.

I appreciate your help
# 12  
Old 11-03-2012
If the filenames in "datatochange.txt" are like *OLD* and you want the Target files have the same name but without the "OLD" you can go with :

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


Last edited by ctsgnb; 11-03-2012 at 02:46 PM..
# 13  
Old 11-03-2012
Hi ctsgnb

I think that I can't there is a little misunderstanding.

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

Hope it is more clear..

I appreciate your help Image
# 14  
Old 11-03-2012
ok, then you can keep the first code i proposed
Note :
1) The file "datatochange.txt" is not limited to 2 lines , you can add more lines if needed.
2) Make sur you understand the code so you can adapt it to your need.
This User Gave Thanks to ctsgnb For This Post:
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