Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
Distribution: Ubuntu n' Flavors, ReactOS, MINIX3, FreeDOS, Arch
Posts: 339
Rep:
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.
Distribution: Ubuntu n' Flavors, ReactOS, MINIX3, FreeDOS, Arch
Posts: 339
Rep:
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)
#!/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.
#!/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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.