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 |
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. |
thx reply ,
I just tried the command "comm" , it seems not work , can advise how to make it ? thx |
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) |
Hi ust,
Give this a try: Code:
grep -f file1 file2 Code:
$ cat file1 Hope this helps. |
Code:
#!/bin/bash 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> |
Quote:
|
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 07:26 AM. |