Replace string in a file with some content indexed from another file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace string in a file with some content indexed from another file.
# 1  
Old 06-21-2014
Replace string in a file with some content indexed from another file.

We have two files

file 1: (usually small, ~100 lines), each line contains a : separated index, value e.g

Code:
2: Apple
1: Banana
5: Pear
7: Orange

File 2: (usually large, 10 million lines or more), each line contains a single string value. e.g
Code:
xyz1
xyz2
xyz3
xyz4
xyz5
xyz6
xyz7

Now the goal is to replace the "index" in the first file (in place) with the value from the second file. i.e. the index in the first file is the line number to be used to get the value from the second file.

e.g the contents for file 1 should be
Code:
xyz2:Apple
xyz1: Banana
xyz5: Pear
xyz7: Orange


Last edited by Don Cragun; 06-21-2014 at 06:22 PM.. Reason: Add CODE tags.
# 2  
Old 06-21-2014
Try something like this:
Code:
awk 'NR==FNR{A[$1]=$2; next} FNR in A{print $1, A[FNR]}' FS=": *" file1 FS=" " OFS=: file2 > newfile

If succesful replace file1 with newfile.

Output:
Code:
xyz1:Banana
xyz2:Apple
xyz5:Pear
xyz7:Orange

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 3  
Old 06-21-2014
If preserving the order of lines in File 1 is important, this comes close to producing the output you requested:
Code:
awk '
BEGIN {	# Set input and output field separators
	FS = OFS = ":"
}
NR == 1 {
	# Save 1st input file pathname (since we will overwrite it later)
	f1name = FILENAME
}
NR == FNR {
	# Gather data from 1st input file
	d[++f1c] = $2	# Data to be saved.
	l[$1] = f1c	# Line # from 2nd file to be printed on Line # from
			# 1st file.
	next
}
FNR in l {
	# Gather data from 2nd input file for any line specifeid in the 1st
	# field in the 1st file.
	o[l[FNR]] = $1
}
END {	# Overwrite the 1st input file with data gathered from both input files.
	for(i = 1; i <= f1c; i++)
		print o[i], d[i] > f1name
}' "File "[12]

which, with the sample input files you provided, changes the contents of File 1to be:
Code:
xyz2: Apple
xyz1: Banana
xyz5: Pear
xyz7: Orange

Be sure that you have a backup copy of File 1 before trying this since this script overwrites it.

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
This User Gave Thanks to Don Cragun 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 string of a file with a string of another file for matches using grep,sed,awk

I have a file comp.pkglist which mention package version and release . In 'version change' and 'release change' line there are two versions 'old' and 'new' Version Change: --> Release Change: --> cat comp.pkglist Package list: nss-util-devel-3.28.4-1.el6_9.x86_64 Version Change: 3.28.4 -->... (1 Reply)
Discussion started by: Paras Pandey
1 Replies

2. Shell Programming and Scripting

File and file content string rename

We have around 1000 scripts in a specific folder like mcx001.txt, mcx002.txt, mcx999.txt, mce001.txt etc We need to rename those files where 3rd character is "x" and should be replaced by "y". So files should be renamed to mcy001.txt, mcy002.txt, mcy999.txt Also, mcx001.txt have content... (5 Replies)
Discussion started by: vedanta
5 Replies

3. Shell Programming and Scripting

How to replace the filed at file 1 by looking the content at file 2?

Suppose I have two file which content like this: File 1.txt Cetner 1, machine A Center 2, machine B Center 3, machine A Center 4, machine C ............................ File 2.txt machine A, 10.10.10.1 machine B,... (4 Replies)
Discussion started by: Alex Li
4 Replies

4. Shell Programming and Scripting

Sed: replace content from file with the content from file

Hi, I am having trouble while using 'sed' with reading files. Please help. I have 3 files. File A, file B and file C. I want to find content of file B in file A and replace it by content in file C. Thanks a lot!! Here is a sample of my question. e.g. (file A: a.txt; file B: b.txt; file... (3 Replies)
Discussion started by: dirkaulo
3 Replies

5. Shell Programming and Scripting

Replace string by file content

hi I have template file my.tpl: bla-bla-bla <link href="style.css" type="text/css"> bla-bla-bla and style.css : body{margin: 0px;} I want to get in result one file: bla-bla-bla <script>body{margin: 0px;}</script> bla-bla-bla I tryed to used SED: sed '/<link .*href=\"(*)*\"... (6 Replies)
Discussion started by: dim_nsk
6 Replies

6. Shell Programming and Scripting

replace blank field in file 2 with content of file 1

Something like vlookup in excel, column 2 in file 2 is blank and should be replaced by column 2 in file 1 based on comparing column 1 in both files. file1 Code: 1234~abc~b~c~d~e~f~g~h~09/10/09 5678~def~b~c~d~e~f~g~h~12/06/10 8910~hij~b~c~d~e~f~g~h~03/28/13... (1 Reply)
Discussion started by: sigh2010
1 Replies

7. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

8. Shell Programming and Scripting

SED to replace file content

Hi, I want to replace _F* by _F in a xml file. what is the sed command. I have tried sed "s/_F$/_F/g" or sed "s/_F*/_F/g" , but it does not work. thx file content <TAG>KC_FOU</TAG> <TAG>KC_FABC</TAG> <TAG>KC_FABCDG</TAG> desire output <TAG>KC_F</TAG> <TAG>KC_F</TAG> <TAG>KC_F</TAG> (6 Replies)
Discussion started by: godfreyyip
6 Replies

9. Shell Programming and Scripting

How To Replace A String In File With A String Containing Windows File Path

Hi, I have a file with the following contents # Lines that start with a # are comments. # # Calling TOAD like this will perform a comparison from command line : # # "C:\Program Files\Quest Software\Toad for Oracle 9.6\toad.exe" -c... (2 Replies)
Discussion started by: rajan_san
2 Replies

10. Shell Programming and Scripting

replace a string with content from another file

Hi, I'm a newbi in shell script. Here what I want to do: FileA: bor bor bor xxxx bib bib bi FileB: something something something I want to replace string "xxxx" in FileA with contents of FileB. i tried with sed: fileb=`cat FileB` reg=xxxx file=FileA (4 Replies)
Discussion started by: afatguy
4 Replies
Login or Register to Ask a Question