Replace text from one file in another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace text from one file in another file
# 1  
Old 07-25-2014
Replace text from one file in another file

I have 2 files. Here is a sample from file 1.

Code:
NETIK0102_UCS_Boot 20000025b510105d
NETIK0102_UCS_Boot 20000025b510104d
NETIK0102_UCS_HBA0 20000025b510113e
NETIK0102_UCS_HBA1 20000025b510112e
NETIK0102_UCS_HBA2 20000025b51010de
NETIK0102_UCS_HBA3 20000025b51010fe
SEIADWFMPRD1 10000000c962521c
SEIADWFMPRD1 10000000c96253ab
SEIDEVDB61 10000000c9ae12ec
SEIDEVDB61 10000000c9a8c842

And here is a sample from file 2

Code:
NETIK0102 195702363 19BB
NETIK0102 195702363 19BC
NETIK0102 195702363 1AA3
NETIK0102 195702363 1A94
NETIK0102 195702363 1A88
SEIADWFMPRD1 195702363 1992
SEIADWFMPRD1 195702363 1993
SEIADWFMPRD1 195702363 19B1
SEIADWFMPRD1 195702363 19C4
SEIDEVDB61 195702363 1726
SEIDEVDB61 195702363 1727
SEIDEVDB61 195702363 1728
SEIDEVDB61 195702363 1729
SEIDEVDB61 195702363 172A



Where ever the first column in file2 is like the first column in file1 I need to replace replace the replace the first column in file2 with the column 2 of file 1. So my output would look like

Code:
 
20000025b510105d 195702363 19BB
20000025b510105d  195702363 19BC
20000025b510105d  195702363 1AA3
20000025b510105d  195702363 1A94
20000025b510105d  195702363 1A88
20000025b510104d  195702363 19BB
20000025b510104d 195702363 19BC
20000025b510104d 195702363 1AA3
20000025b510104d 195702363 1A94
20000025b510104d 195702363 1A88
....
....
....
10000000c9ae12ec 195702363 1726
10000000c9ae12ec 195702363 1727
10000000c9ae12ec 195702363 1728
10000000c9ae12ec 195702363 1729
10000000c9ae12ec 195702363 172A
10000000c9a8c842 195702363 1726
10000000c9a8c842 195702363 1727
10000000c9a8c842 195702363 1728
10000000c9a8c842 195702363 1729
10000000c9a8c842 195702363 172A


Any tips on how to do this using awk, grep, sed of nested while looks would be much appreciated.
# 2  
Old 07-25-2014
Try,

Code:
awk 'NR==FNR {a[$1]=$2;next} ($1 in a) {print a[$1] FS $2}' file1 file2

Not tested though.
# 3  
Old 07-25-2014
Code:
awk 'NR==FNR{arr[$1]=$2;next}($1 in arr){$1=arr[$1]}1' file1 file2

# 4  
Old 07-25-2014
Thanks it almost works. But is there a way to use ~ instead of = ? So this way it will swap all the occurences of NETIK0102_UCS_BOOT, and NETIK0102_UCS_HBA0 etc? If it matches any of the word NETIK0102 in column 1 I would want to replace it with the each corresponding alphanumeric string in column 2 that has NETIK0102 in file2?

Thanks!
# 5  
Old 07-25-2014
Try:
Code:
awk 'NR==FNR{arr[$1]=$2;next}{for(ele in arr) {if ($1 ~ ele) {$1=arr[ele]}}}1' file1 file2

This User Gave Thanks to chacko193 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace text in a file

I wrote a program using Perl to find and replace a text within a file. The text that needs to be replaced in the file is 'sql7.0.1' with 'sqls715'. When I execute my program I get an error message: Here is my code: #!/usr/bin/perl use strict; use warnings; my $filename =... (6 Replies)
Discussion started by: dellanicholson
6 Replies

2. Shell Programming and Scripting

Bash script to replace text file from a lookup file

Hi. I need assistance with the replacing of text into a specific file via a bash script. My bash script, once run, currently provides a menu of computer names to choose.The script copies onto my system various files, depending what computer was selected in the menu.This is working OK. Now, I... (1 Reply)
Discussion started by: jonesn2000
1 Replies

3. Windows & DOS: Issues & Discussions

2 Questions: replace text in txt file, add text to end of txt file

so... Lets assume I have a text file. The text file contains multiple "#" symbols. I want to replace all thos "#"s with a STRING using DOS/Batch I want to add a certain TEXT to the end of each line. How can I do this WITHOUT aid of sed, grep or anything linux related ? (1 Reply)
Discussion started by: pasc
1 Replies

4. Shell Programming and Scripting

Search and replace from file in awk using a 16 bit text file

Hello, Some time ago a helpful awk file was provided on the forum which I give below: NR==FNR{A=$0;next}{for(j in A){split(A,P,"=");for(i=1;i<=NF;i++){if($i==P){$i=P}}}}1 While it works beautifully on English and Latin characters i.e. within the ASCII range of 127, the moment a character beyond... (6 Replies)
Discussion started by: gimley
6 Replies

5. Shell Programming and Scripting

Replace text in column1 of a file matching columns of another file

Hi all, I have 2 files: species-names.txt Abaca-bunchy-top-virus ((((Abaca-bunchy-top-virus((Babuvirus((Unassigned((Nanoviridae((Unassigned)))) Abutilon-mosaic-virus ((((Abutilon-mosaic-virus((Begomovirus((Unassigned((Geminiviridae((Unassigned))))... (2 Replies)
Discussion started by: thienxho
2 Replies

6. Shell Programming and Scripting

how to replace a text in a file from second file in shell

Hi, I have file 1 say abc abcd abc abcd <some tag> xyx abcd xyz abc abcd <some tag> xyx xyz abc And i have another file say file 2 - replaced tag1 replaced tag2 Now i want to put value of file2 in file1 whereever it finds <some tag>. for e.g. first <some tag> should be replaced... (4 Replies)
Discussion started by: abhitanshu
4 Replies

7. Shell Programming and Scripting

How to replace text in a file with text entered

I am trying to write a shell script that will allow the typing of a value, then using that value to replace data in a text file. I suspect I need sed. The format of the file is: Variable1:Value1 Variable2:Value2 The interaction would be something like: Shell Prompt: "Please enter the... (9 Replies)
Discussion started by: cleanden
9 Replies

8. UNIX for Dummies Questions & Answers

how can search a String in one text file and replace the whole line in another file

i am very new to UNIX plz help me in this scenario i have two text files as below file1.txt name=Rajakumar. Discipline=Electronics and communication. Designation=software Engineer. file2.txt name=Kannan. Discipline=Mechanical. Designation=CADD Design Engineer. ... (6 Replies)
Discussion started by: kkraja
6 Replies

9. UNIX for Dummies Questions & Answers

search and replace a specific text in text file?

I have a text file with following content (3 lines) filename : output.txt first line:12/12/2008 second line:12/12/2008 third line:Y I would like to know how we can replace 'Y' with 'N' in the 3rd line keeping 1st and 2nd lines same as what it was before. I tried using cat output.txt... (4 Replies)
Discussion started by: santosham
4 Replies

10. Shell Programming and Scripting

Replace text in input file

I wish to replace values of specific parameters in an input file for batch runs of a java code. It's essentially a nested for-loop sorta like this: valuearray1 contains values for param1 valuearray2 contains values for param2 for (all values in valuearray1) go into specific position in... (2 Replies)
Discussion started by: daphantomica
2 Replies
Login or Register to Ask a Question