LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 10-17-2007, 04:53 AM   #1
bugg_deccan
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

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 04:54 AM.
 
Old 10-17-2007, 05:41 AM   #2
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 35
Code:
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
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
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"
 
Old 10-17-2007, 09:53 PM   #4
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Quote:
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.

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.
 
  


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
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


All times are GMT -5. The time now is 07:42 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration