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
# 22  
Old 11-04-2012
I suppose you did copy or move or modify your datatochange.txt file in a way or another, because my code does not modifiy this file, we just read it.
By the way, maybe you did a mix : all the sed -f ... command you passed should be in the "change2run" file, and not in the "datatochange.txt" file

By running :

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

This will generate all <file>.sed containing all the substitution to perform for each <file> specified in the "datatochange.txt"

This will also generate a "change2run" that contains the command to process all the substitution (by using the <file>.sed) for each <file> and store the result in the corresponding generated <file>.new

This script should be run in a simple shell like :
Code:
sh change2run

So all the command i provided just generate new files, they do not modify, rename, or move your initial files. If your files have changed it is because of some other external actions, but not because of my code.

---------- Post updated at 03:59 PM ---------- Previous update was at 03:44 PM ----------

After every run, you should of course then make some additionnal cleanup like (for all <file> mentionned in your "datatochange.txt") :
Code:
mv <file> <file>.old
mv <file>.new <file>
rm <file>.sed
rm change2run

... or whatever actions are needed to fit with your requirements

Last edited by ctsgnb; 11-04-2012 at 01:20 PM..
This User Gave Thanks to ctsgnb For This Post:
# 23  
Old 11-04-2012
Thanks a lot

---------- Post updated at 01:16 PM ---------- Previous update was at 10:01 AM ----------

Dear

Now everythign is Ok,

Only, please can you make a code how to do it
Code:
mv <file> <file>.old
mv <file>.new <file> 
rm <file>.sed
rm change2run

I was trying like this but it doesn't work
I put this at the end of my script ,,

Code:
mv "i" "i".old 
mv "i".new "i" 
rm -f "i".sed 
rm -f change2run

Thanks for your help... and time

---------- Post updated at 02:01 PM ---------- Previous update was at 01:16 PM ----------

Dear ctsgnb

I got it like this.. Smilie

Code:
rm -f change2run
awk '{print $1}' datatochange.txt | uniq | awk '{print("mv "$1 " "$1"_old")}' | sh
awk '{print $1}' datatochange.txt | uniq | awk '{print("mv "$1".new" " "$1)}' | sh
awk '{print $1}' datatochange.txt | uniq | awk '{print("rm -f "$1".sed")}' | sh

Do you have other short way?

Regards

Last edited by jiam912; 11-04-2012 at 02:19 PM.. Reason: code
# 24  
Old 11-04-2012
I meant : you should of course replace <file> with the names of the files you used in you datatochange.txt file.
You could gather all the steps in a single script which could for example be something like :

Code:
#!/usr/bin/ksh

# Scan the file datatochange.txt AND generate the necessary files
# to perform the corresponding substitutions
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

# Run the substitutions and store the result in *.new file
ksh change2run

# Rename current file into *.old
# Rename the result *.new with the current file name (only if 
# the previous renaming was successful)
# Remove the *.sed files that were used during the substitution
awk '!a[$1]++{print $1}' datatochange.txt | while read i
do
mv "$i" "$i.old" && mv "$i.new" "$i"
rm "$i.sed"
done

# Remove the main script that were generated to process the substitution
rm change2run

Of course all this could be enhanced in many ways...just feel free to improve & adapt it to your own environment & requirements.

Last edited by ctsgnb; 11-04-2012 at 03:53 PM..
This User Gave Thanks to ctsgnb For This Post:
# 25  
Old 11-05-2012
Dear ctsgnb

Thanks That's perfect!

Your help is greatly appreciated.

---------- Post updated 11-05-12 at 04:21 AM ---------- Previous update was 11-04-12 at 03:04 PM ----------

dear ctsgnb

could you please check where there is a error

Code:
awk '{a[$1]++; print $1}' datatochange.txt | `while read i` 
do 
mv "$i" "$i.old" && mv "$i.new" "$i"
rm "$i.sed" done

I got a error while: Expression syntax

Thanks for your help

Last edited by jiam912; 11-05-2012 at 05:22 AM.. Reason: code
# 26  
Old 11-05-2012
1) remove the back quote around the while read i
2) Add a new line or a semicolon before the done

Code:
awk '{a[$1]++; print $1}' datatochange.txt | while read i  
do  
mv "$i" "$i.old" && mv "$i.new" "$i"
rm "$i.sed"
done

# 27  
Old 11-05-2012
dear ctsgnb
Thanks
My problem was becasue I was using
Code:
#!bin/csh

I should use ?
Code:
#!/bin/bash

---------- Post updated at 05:47 AM ---------- Previous update was at 05:36 AM ----------

it is working fine.. now.

Thanks a lot for your help and time
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