Replacing string values from a File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing string values from a File
# 1  
Old 11-16-2011
Replacing string values from a File

I want to replace string values from a file to a file

For eg : File1 has 30 lines of string with values, those specific values needs to be changed in file2 and remaining values in file2 should be as it is.



For example:

From file (File1)
cluster.name=secondaryCluster

To replace File (File2)
cluster.name=CoreSetupCluster

(in the above CoreSetupCluster in file2 should be changed to secondaryCluster as in file1
# 2  
Old 11-16-2011
Code:
awk 'BEGIN{FS=OFS="="}NR==FNR{a[NR]=$NF;next}{$NF=a[FNR]?a[FNR]:$NF}1' file1 file2

# 3  
Old 11-17-2011
Value not changed :
Code:
sriram@ubuntu:~/perl/Awk$ awk 'BEGIN{FS=OFS="="}NR==FNR{a[NR]=$NF;next}{$NF=a[FNR]?a[FNR]:$NF}1' file1 file2
value=value1
value2=value3

sriram@ubuntu:~/perl/Awk$ cat file1 
value=value1
value2=value3

sriram@ubuntu:~/perl/Awk$ cat file2
value=value1
value2=value2

Moderator's Comments:
Mod Comment Please use code tags!

Last edited by zaxxon; 11-17-2011 at 04:21 AM.. Reason: code tags
# 4  
Old 11-17-2011
Code:
awk -F= 'NR==FNR{a[$1]=$2;next}a[$1]{$2=a[$1]}1' OFS="=" file1 file2 > newfile

# 5  
Old 11-22-2011
No , Smilie ,, Let me be more clear and explain you... file1 contains 10 entries , all entries are seperated with = sign, file2 contains 100 entries... all i want is if any lines starting before = sign matches file1 in file2 , then replace that particular line in file2 with file1 values ... hope i am more clear this time...



Thanks,
# 6  
Old 11-22-2011
The question was quite clear. What doesn't work? What output did you get?
# 7  
Old 11-24-2011
yes looks good ... how do i replace it in actual file rather than a new file >> does not work
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replacing values inside a file.

Good day guys, I'm having trouble in creating a logic when it comes to replacing the values inside a file. I tried using sed command but it just doesn't work the way I want it to be. Here is what I'm trying to achieve. If my input file contains the values below. NAME++GUEST1 ++GUESS2++... (3 Replies)
Discussion started by: asdfghjkl
3 Replies

2. Shell Programming and Scripting

awk file to read values from Db2 table replacing hard coded values

Hi, I want to replace a chain of if-else statement in an old AWK file with values from Db2 table or CSV file. The part of code is below... if (start_new_rec=="true"){ exclude_user="false"; user=toupper($6); match(user, "XXXXX."); if (RSTART ==2 ) { ... (9 Replies)
Discussion started by: asandy1234
9 Replies

3. Programming

Need help for replacing a string in a text file at runtime !

Hi All, I am facing an issue... I need to replace some string in a text file while the same file is read by some other user at the same time. The other user is using it in the Read only mode. So I can't create a temporary file and write the content first and then write it back into the original... (2 Replies)
Discussion started by: agupta2
2 Replies

4. Shell Programming and Scripting

Replacing column 1 in one file with values in other file

Please help me with an shell / awk script to achieve following; File-1: ABCDW01 12322 23322 BDADW01 22232 24453 EDFAW00 32232 23422 and so on, notice that the first coloumn is a code and the another file contains the real value of each entry in the first colum above but not in a... (4 Replies)
Discussion started by: digipak
4 Replies

5. UNIX for Dummies Questions & Answers

replacing a string with another string in a txt file

Dear all, I have a file like below. I want to replace all the '.' in the 3rd column with 'NA'. I don't know how to do that. Anyone has an iead? Thanks a lot! 8 70003200 21.6206 9 70005700 17.5064 10 70002200 . 11 70005100 19.1001 17 70008000 16.1970 32 70012400 26.3465 33... (9 Replies)
Discussion started by: forevertl
9 Replies

6. Shell Programming and Scripting

Replacing values in a file based on values in another file

Hi I have 2 files:- 1. List of files which consists of names of some output files. 2. A delimited file; delimted by "|" I want to replace the value of the $23 (23rd column) in the delimited file with name in the first file. It is always position to position. Meaning first row of the first... (5 Replies)
Discussion started by: pparthiv
5 Replies

7. Shell Programming and Scripting

Replacing File values

Currently I am using the tr command in 3 scenarios for a ksh script. 1) Replacing any spaces in the file with a ~ tr ' ' '~' <$orignalFile> $newFile 2) After certain processing is done at the end of the scirpt i convert the Tilde back to spaces tr ' ' '~' <$newFile> $newFile2 3) Last... (4 Replies)
Discussion started by: hgjdv
4 Replies

8. Shell Programming and Scripting

Replacing a character string in a file

Hi there, I have a paramater file that looks like this :- IRL|07122005|27389|VTIEpay|email address|5|200 When my program finishes I want to replace the seventh field. the existing code is like this cat <<-EOF | ed -s $PARFILE 1,$ g/^$ICO/s/$prvdate/$TODAY/ 1,$... (13 Replies)
Discussion started by: rjsha1
13 Replies

9. Shell Programming and Scripting

replacing string in an XML file

Hi all, I need to replace string in XML file..XML file like <dependency> <groupId>fr.orange.portail.ear</groupId> <artifactId>_AdminServicesEAR</artifactId> <version>1.0.0-20080521.085352-1</version> <type>ear</type> </dependency> <dependency> ... (2 Replies)
Discussion started by: subin_bala
2 Replies
Login or Register to Ask a Question