LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-10-2010, 04:28 AM   #1
ldapguy
LQ Newbie
 
Registered: Nov 2010
Posts: 2

Rep: Reputation: 0
How to compare files and pull out required data?


I have 2 files to compare and then print out information that match a certain pattern. I know basic scripting and was heading down the path of merging the 2 files together but this is the wrong approach. Would really appreciate a script that can do what is required:

file 1 contains dates, times and ID's:
2010-10-28 10:42 5939697357
2010-10-28 11:56 5919543491
2010-10-28 11:58 5919543491
2010-10-28 12:07 5919543491
2010-10-28 12:31 5976848509
2010-10-28 13:01 5995808867
2010-10-28 14:14 5938245291
2010-10-28 14:24 5938245291

File 2 contains just ID's:
5939697357
5919543491
5938245291

I need to compare the 2 files and print out the lines from file1 that contain any ID found in file2. I'm playing with awk and sed but cannot come up with a script.
 
Old 11-10-2010, 04:45 AM   #2
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 542

Rep: Reputation: 79
Try this:
Code:
#!/bin/bash

for i in `cat file2`; do
  awk "/$i/" file1
done
 
Old 11-10-2010, 04:51 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Code:
#!/bin/bash

while read ids
do
  awk '$3=='$ids'' file1
done < file2
BTW: If both files are very big, this (and the previous answer #2) is not the most resource friendly way to do it.

Hope this helps.
 
Old 11-10-2010, 04:51 AM   #4
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
grep should work as well, grep -f file2 file1
 
Old 11-10-2010, 06:14 AM   #5
ldapguy
LQ Newbie
 
Registered: Nov 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks guys for the quick reply. I've tried all 3 ways and they all work great. My files will never be too large in size, maybe 60 to 70 lines at most. Now I'll just do some testing with larger file sizes and see which one works best.

Much appreciated!!
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Compare semicolon seperated data in 2 files using shell script novice82 Linux - Newbie 4 11-17-2009 05:26 PM
script help - pull date format out of file name and compare to today MaureenT Linux - General 5 11-06-2008 11:14 AM
gnuplot, pull X data from file, specify Y data at cmd line? hedpe Programming 5 03-15-2007 11:32 PM
I made a program that can pull data off a large web site 20 times a day halfpower General 12 11-18-2006 08:38 PM
using DD to pull records out of a data file fhinkle Linux - Newbie 6 02-24-2005 04:20 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:56 PM.

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
Open Source Consulting | Domain Registration