Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
10-17-2007, 05:53 AM
|
#1
|
LQ Newbie
Registered: Oct 2007
Posts: 6
Rep:
|
Script to compare numbers inside two text files
Hai all,
I have two txt files.
I want to read a number(s) in a file and check for same number(s) in another file.
If match is found then write out the mathched number(s) to another new file
eg:
test1.txt
10,12,1984,45678952
test2.txt
09,12,1984,45789632
10,12,1984,45869874
I want to read the first three comma separated vales from test1.txt(10,12,1984) and check it in test2.txt. In this case one match is found. So I need the O/p file to be as
10,12,1984,45678952,45869874
Can any one help me please....
Last edited by bugg_deccan; 10-17-2007 at 05:54 AM.
|
|
|
10-17-2007, 06:41 AM
|
#2
|
Member
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212
Rep:
|
Code:
awk 'NR==FNR{p=$1$2$3;r=$0;next}
$1$2$3==p&&$0=r","$NF' FS="," test1.txt test2.txt
|
|
|
10-17-2007, 01:37 PM
|
#3
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Code:
awk 'BEGIN{OFS=FS=","}
FNR==NR{ u=$1","$2","$3;x[u]=$NF;next }
{
k=$1","$2","$3
print k,x[k]
}
' "file2" "file"
|
|
|
10-17-2007, 10:53 PM
|
#4
|
Member
Registered: Oct 2003
Distribution: Archlinux
Posts: 147
Rep:
|
Quote:
Originally Posted by bugg_deccan
I want to read a number(s) in a file and check for same number(s) in another file.
|
Not sure if you have multiple lines in the first file or not. If you do, here's a small python solution for it.
Code:
import sys
file1 = dict(line.strip().split(',',1) for line in open(sys.argv[1]))
file2 = dict(line.strip().split(',',1) for line in open(sys.argv[2]))
for x in set(file1) & set(file2):
print ','.join([x, file1[x], file2[x]])
Usage:
python compare.py test1.txt test2.txt
This will work if you have multiple lines in both files and you need to compare them all.
This program makes two assumptions (cause I'm lazy):
1. The files aren't some sort of huge files that will fill up all your ram. (stores files in memory)
2. There are 4 numbers on each line, not more.
Both are easy fixes, so if you need them just ask.
|
|
|
All times are GMT -5. The time now is 06:58 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|