LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   match the file content (https://www.linuxquestions.org/questions/linux-newbie-8/match-the-file-content-939010/)

ust 04-09-2012 11:47 PM

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

Zssfssz 04-10-2012 12:19 AM

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.

ust 04-10-2012 12:46 AM

thx reply ,

I just tried the command "comm" , it seems not work , can advise how to make it ? thx

Zssfssz 04-10-2012 12:53 AM

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)

druuna 04-10-2012 01:36 AM

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.

yoK0 04-10-2012 02:00 AM

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>

ust 04-10-2012 02:15 AM

Quote:

Originally Posted by yoK0 (Post 4648976)
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.

David the H. 04-10-2012 10:18 AM

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.


All times are GMT -5. The time now is 08:59 PM.