LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 04-09-2012, 11:47 PM   #1
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130

Rep: Reputation: 31
match the file content


I have two files , the content is as below , I would like to list the lines in file2 , that the lines the file1 have , as the below , both files have aaaa , cccc , eeee , so the output is the line which have aaaa , cccc , eeee , can advise what can i do ? thx

file1
=====
aaaa
bbbb
cccc
dddd
eeee


file2
=====
aaaa 1111
cccc 2222
eeee 3333


my desired result is as below
=============================
aaaa 1111
cccc 2222
eeee 3333
 
Old 04-10-2012, 12:19 AM   #2
Zssfssz
Member
 
Registered: Sep 2011
Location: Las Vegas!
Distribution: Ubuntu n' Flavors, ReactOS, MINIX3, FreeDOS, Arch
Posts: 339

Rep: Reputation: Disabled
This is a job for the programing section (just say you want to use bash and they'll give you the commands)
I'd move this but I'm not an admin, don't repoast, pm an admin.
 
Old 04-10-2012, 12:46 AM   #3
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130

Original Poster
Rep: Reputation: 31
thx reply ,

I just tried the command "comm" , it seems not work , can advise how to make it ? thx
 
Old 04-10-2012, 12:53 AM   #4
Zssfssz
Member
 
Registered: Sep 2011
Location: Las Vegas!
Distribution: Ubuntu n' Flavors, ReactOS, MINIX3, FreeDOS, Arch
Posts: 339

Rep: Reputation: Disabled
Ohh... Um...
There might be an option in grep for this but I don't know!
I have spent hours looking for stuff like his long ago when I needed to make some files........
Find one of those remove duplacate words sites and use that if yer desperate.
(or try sudo apt-get install comm on Debian/Ubuntu)
 
Old 04-10-2012, 01:36 AM   #5
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 ust,

Give this a try:
Code:
grep -f file1 file2
Sample:
Code:
 $ cat file1
aaaa
bbbb
cccc
dddd
eeee
$ cat file2
aaaa 1111
cccc 2222
eeee 3333
$ grep -f file1 file2 
aaaa 1111
cccc 2222
eeee 3333
grep can use a file that holds the patterns (file1 in your case)

Hope this helps.
 
1 members found this post helpful.
Old 04-10-2012, 02:00 AM   #6
yoK0
LQ Newbie
 
Registered: Apr 2012
Distribution: Slackware, CentOS
Posts: 29

Rep: Reputation: 0
Code:
#!/bin/bash

file1=$1
file2=$2
while read LINE
    do
        grep $LINE $file2
done < $file1
save script as check.sh eg. and do chmod u+x check.sh

now you can use it like this

./check.sh <file_with_patherns> <file_to_check>

it will not work with your files unless "aaaaaa" is equal or smaller to "aaaaaa 11111"

For example in first file you can keep all the IP addresses you want to keep an eye on,
the second file is log file. This way you can extract all lines containing IP addresses from
first file.

./check.sh <file_containing_IP_addresses> <log_file>
 
Old 04-10-2012, 02:15 AM   #7
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by yoK0 View Post
Code:
#!/bin/bash

file1=$1
file2=$2
while read LINE
    do
        grep $LINE $file2
done < $file1
save script as check.sh eg. and do chmod u+x check.sh

now you can use it like this

./check.sh <file_with_patherns> <file_to_check>

it will not work with your files unless "aaaaaa" is equal or smaller to "aaaaaa 11111"

For example in first file you can keep all the IP addresses you want to keep an eye on,
the second file is log file. This way you can extract all lines containing IP addresses from
first file.

./check.sh <file_containing_IP_addresses> <log_file>
excellent , thx much.
 
Old 04-10-2012, 10:18 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I highly suggest you take another look at druuna's suggestion. It usually pays to keep it simple. Why run a loop of many separate grep commands when you can do it all at once with a single instance?

The only time I wouldn't use grep -f is if any of the patterns in file1 could also be found in other columns/sections of file2 than the ones you want to output. File1 is treated as a collection of patterns to search for, and since the ones in your file are not anchored to the start of the line, they will match all occurrences of the text that appear in file2.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed match html content (multiple lines) ted_chou12 Linux - Newbie 5 12-08-2011 01:25 AM
[SOLVED] AWK: match multiple strings in the file, print 1 when match and 0 when not cristalp Programming 12 11-15-2011 10:18 AM
[SOLVED] Replace content of one column to another if match found saurabhmehan Linux - Newbie 5 12-22-2010 12:21 PM
delete file that match the content packets Programming 5 04-03-2007 02:47 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:30 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