LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-01-2012, 08:52 PM   #1
sttumped
LQ Newbie
 
Registered: Nov 2012
Posts: 2

Rep: Reputation: Disabled
Find & replace text from file 1 with text from file 2?


Hi everyone,

I'm new to Linux and manipulating large text files in Linux.

I am familiar with search and replace within a single text file, but I'm faced with a situation where I suspect coding is necessary and would like to seek your help.

File 1 looks like this (all files are tab delimited):

Quote:
ID AGE SALARY
Name1 34 30000
Name2 23 24000
Name3 35 40000
...
etc.
File 2 looks like this (not in the same order as File 1):

Quote:
ID NAME
Name2 John
Name1 Jane
Name3 Joseph
...
etc.
Basically, I want to search File 2 for the NAME corresponding to the ID in File 1 and then output a new file, File 3, which now has the IDs replaced with NAMEs, but it is crucial that I maintain the same order for File 3 as in File 1 (as I'll be using File 3 for something else and the order is important) like this:

Quote:
NAME AGE SALARY
Jane 34 30000
John 23 24000
Joseph 35 40000
...
etc.

Is there a straight forward way of doing this without coding? I read about AWK, but I'm not sure if that command can compare with other files (??).

Very stuck at the moment, thanks for your help.
 
Old 11-02-2012, 03:12 AM   #2
cliffordw
Member
 
Registered: Jan 2012
Location: South Africa
Posts: 499

Rep: Reputation: 188Reputation: 188
Hi there,

Can't think of an efficient way of doing this without coding, but a short Perl script can do the job:
Code:
#!/usr/bin/perl -w
use strict;
my $file1 = 'file1.txt';
my $file2 = 'file2.txt';
my $file3 = 'file3.txt';
open(F2,'<',$file2) || die "Could not open $file2: $!\n";
my %names = ();
while (<F2>) {
	chomp;
	my ($id,$name) = split(/\t/o,$_,2);
	$names{$id} = $name;
}
close(F2);
open(F1,'<',$file1) || die "Could not open $file1: $!\n";
open(F3,'>',$file3) || die "Could not open $file3: $!\n";
my $linecnt = 0;
my $errcnt = 0;
while (<F1>) {
	$linecnt++;
	chomp;
	my ($id,$age,$salary) = split(/\t/o,$_,3);
	if (!defined($salary)) {
		printf STDERR "Error: could not parse line %d - skipping\n", $linecnt;
		$errcnt++;
		next;
	}
	if (!defined($names{$id})) {
		printf STDERR "Error: could not name for ID '%s' in line %d - skipping\n", $id, $linecnt;
		$errcnt++;
		next;
	}
	printf F3 "%s\t%s\t%s\n", $names{$id}, $age, $salary;
}
close(F1);
close(F3) || die "Failed to close $file3: $!\n";
exit($errcnt ? 1 : 0);
Regards,

Clifford
 
1 members found this post helpful.
Old 11-02-2012, 03:32 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Hi and welcome to LinuxQuestions! Here is a simple awk solution:
Code:
awk 'FNR == NR { name[$1] = $2 }; FNR < NR { $1 = name[$1]; print } ' file2 file1 > file3
Feel free to ask for explanations about this code.
 
1 members found this post helpful.
Old 11-02-2012, 04:14 AM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 3,136

Rep: Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336
Quote:
Originally Posted by sttumped View Post
but it is crucial that I maintain the same order for File 3 as in File 1 (as I'll be using File 3 for something else and the order is important) like this:
Is File 1 sorted on the first field? If it is, you can use join:

Code:
join --header -o 1.2,2.2,2.3  <(sort -k1,1 names.txt) salaries.txt 
# or without the bash-specific process substitution:
sort -k1,1 names.txt | join --header -o 1.2,2.2,2.3  - salaries.txt
 
1 members found this post helpful.
Old 11-02-2012, 04:36 AM   #5
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
This looks like a task that could be ready for putting into a database such as postgresql.
 
1 members found this post helpful.
Old 11-02-2012, 03:58 PM   #6
sttumped
LQ Newbie
 
Registered: Nov 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thank you everyone for your suggested solutions to this issue! It is much much appreciated. I will study each and will post again if I have any questions.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to do search & replace on a text file--need to extract URLs from a sitemap file Mountain Linux - General 4 08-07-2015 10:52 AM
[SOLVED] Bash command to 'cut' text into another text file & modifying text. velgasius Programming 4 10-17-2011 04:55 AM
[SOLVED] command to find/replace in a text file? sneakyimp Linux - Newbie 17 02-15-2011 12:30 AM
Linux command to find and replace string within text file chips11 Linux - Newbie 5 11-24-2008 02:25 PM
Find and replace text in multiple file Bad_Bob Linux - Software 9 05-08-2008 02:31 AM


All times are GMT -5. The time now is 12:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration