LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 02-22-2012, 04:26 AM   #1
dann_radkov
Member
 
Registered: Sep 2011
Posts: 52

Rep: Reputation: Disabled
grep for common patterns in 2 files


Hey Guys,
I have two files with items.
A-txt file /is the output of yum listavaiable/ has extracted all the files from a repo of an RHEL6 . I modified them so now i have only the rpm names. I cut lose all the version/release/platform part.
B-txt file /is again the output of yum listavailable/ but for RHEL5.6. I modified the list so now i have only the rpm names. I cut lose all the version/release/platform part.

I need to use grep to produce a list & make sure all items present in B file(or the old image we used to have) and also present in A file get outputted to a C file.
This way I can basically define what I need in the new image and cut loose the rest of the stuff.
Please let me know if you have suggestions.
 
Old 02-22-2012, 04:49 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
"man comm"
 
Old 02-22-2012, 05:19 AM   #3
dann_radkov
Member
 
Registered: Sep 2011
Posts: 52

Original Poster
Rep: Reputation: Disabled
Ok looks justified. I sorted them beforehand and did a
comm -1 -2 Afile Bfile(as fas as i understand it doesnt matter which you will put first) > Cfile.txt
Sound about right ?
 
Old 03-05-2012, 12:57 AM   #4
dann_radkov
Member
 
Registered: Sep 2011
Posts: 52

Original Poster
Rep: Reputation: Disabled
Ok Just confirmed it doesnt work in my case.Some files from list A are not necessarily present in list B.I cannot do a comm -1 -2 .Please elaborate.
 
Old 03-05-2012, 01:19 AM   #5
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Quote:
Originally Posted by dann_radkov
Some files from list A are not necessarily present in list B.
That contradicts your earlier statement:

Quote:
Originally Posted by dann_radkov
I need to use grep to produce a list & make sure all items present in B file(or the old image we used to have) and also present in A file get outputted to a C file.
If the file is not listed in A, then you don't want it according to the original post.

If the requirements have changed, that's fine. But, you haven't explained how the requirements have changed. We can't help with a solution if you don't provide those requirements.
 
Old 03-05-2012, 02:42 AM   #6
singhjc
Member
 
Registered: Nov 2010
Location: Moradabad
Distribution: Redhat,Centos
Posts: 31

Rep: Reputation: 1
Hi,

First get determine what do you want actually.
 
Old 03-07-2012, 01:52 AM   #7
dann_radkov
Member
 
Registered: Sep 2011
Posts: 52

Original Poster
Rep: Reputation: Disabled
I need to use grep to produce a list & make sure all items present in B file(or the old image we used to have) and also present in A file get outputted to a C file.
This way I can basically define what I need in the new image and cut loose the rest of the stuff.
Please let me know if you have suggestions.

I also realized i was using comm with a wrong syntax
i was doing
#comm -1 -2 Afile Bfile > Cfile
whereas I should be doing
#comm -12 Afile Bfile > Cfile
 
Old 03-07-2012, 02:51 AM   #8
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Ok, something is not "translating well."

You want a list of lines listed in both fileA and fileB.

The command you gave in your last reply will do that.

For example:
Code:
user@localhost$ cat fileA
package 1
package 2
package 3
package 4
package 5
package 6
user@localhost$ cat fileB
package 1
package 4
package 6
package 7
package 8
user@localhost$ comm -1 -2 fileA fileB
package 1
package 4
package 6
user@localhost$ comm -12 fileA fileB
package 1
package 4
package 6
user@localhost$ cat fileA fileB | sort | uniq -d
package 1
package 4
package 6
The last command is an alternative to comm.

To me, that satisfies your requirements. If the command is not producing the desired output on your end, then (1) the description of the requirements needs to be refined or (2) your data files are not "sanitized" (e.g. dos2unix, extra whitespace, mismatched lower/uppercase letters, etc.)

We would need to see real, actual sample data and the output you're getting.
 
  


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
get files that match ALL patterns (using grep?) vigilandy Linux - General 5 06-09-2010 04:18 AM
grep patterns tekmann33 Linux - Newbie 2 07-14-2008 01:25 PM
how to grep 5k patterns at a time? xiawinter Linux - Software 9 12-29-2007 01:18 AM
LXer: Five common PHP design patterns LXer Syndicated Linux News 0 07-21-2006 01:24 AM
Exclude certain patterns from grep? kinetik Linux - General 4 04-24-2006 05:37 AM

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

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