Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 10-17-2007, 04:53 AM   #1
LQ Newbie
Registered: Oct 2007
Posts: 6

Rep: Reputation: 0
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



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


Can any one help me please....

Last edited by bugg_deccan; 10-17-2007 at 04:54 AM.
Old 10-17-2007, 05:41 AM   #2
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 36
awk 'NR==FNR{p=$1$2$3;r=$0;next}
$1$2$3==p&&$0=r","$NF' FS="," test1.txt test2.txt
Old 10-17-2007, 12:37 PM   #3
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
awk 'BEGIN{OFS=FS=","}
 FNR==NR{ u=$1","$2","$3;x[u]=$NF;next }
  print k,x[k]
' "file2" "file"
Old 10-17-2007, 09:53 PM   #4
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Originally Posted by bugg_deccan View Post
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.

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]])
python 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.


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
Bash script to compare two files swatward Linux - Newbie 17 04-18-2012 06:14 PM
Search for text inside files alaios Linux - Newbie 7 03-12-2006 09:20 AM
searching inside text files minm Linux - Newbie 2 01-08-2005 11:56 PM
how to compare 2 text files by using php code antony_csf Programming 3 10-14-2004 05:52 AM
Need text pattern compare script kscott121 Linux - Software 4 05-10-2004 01:13 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:54 PM.

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