LinuxQuestions.org
Help answer threads with 0 replies.
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 12-19-2011, 06:12 AM   #1
rahularjun86
LQ Newbie
 
Registered: Apr 2011
Location: Frankfurt Am Main, Germany
Distribution: FC14
Posts: 9

Rep: Reputation: 0
Unhappy fgrep pipes


Hi,
I have six files and I need to fetch the lines that are common among all of these files. Lets say I have 1.txt, 2.txt, 3.txt....6.txt. I was trying the following command.


$ fgrep -f 1.txt 2.txt | fgrep 3.txt | fgrep 4.txt .... | fgrep 6.txt > Common_among_all.txt


But this is not working Do anyone have any idea how to do this,, I will really appreciate your comments....Many thanks in advance

I did this using cat and uniq:
$ cat *.txt | sort | uniq -c | awk '{if ($1 ==6 ) print $0}'

Last edited by rahularjun86; 12-19-2011 at 06:28 AM.
 
Old 12-19-2011, 07:57 AM   #2
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Note that it works if identical lines are present only one time for each file
(eg it might fail if in one file, same line is present 6 times)

Based on this assumption, this perl line might also work:
Code:
perl -ne '{print if ++$s{$_}==6}' *.txt
 
1 members found this post helpful.
Old 12-19-2011, 04:42 PM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,462

Rep: Reputation: 1517Reputation: 1517Reputation: 1517Reputation: 1517Reputation: 1517Reputation: 1517Reputation: 1517Reputation: 1517Reputation: 1517Reputation: 1517Reputation: 1517
Quote:
Originally Posted by rahularjun86 View Post
Hi,
I have six files and I need to fetch the lines that are common among all of these files. Lets say I have 1.txt, 2.txt, 3.txt....6.txt. I was trying the following command.


$ fgrep -f 1.txt 2.txt | fgrep 3.txt | fgrep 4.txt .... | fgrep 6.txt > Common_among_all.txt
You're almost there. You just need to tell each subsequent fgrep to use stdin as the file containing patterns to be matched. You should also use the "-x" option to insist that the whole line match.
Code:
fgrep -xf 1.txt 2.txt | fgrep -xf - 3.txt | fgrep -xf - 4.txt ... | fgrep -xf - 6.txt
 
1 members found this post helpful.
Old 12-19-2011, 05:52 PM   #4
singhharmeet
LQ Newbie
 
Registered: Dec 2011
Posts: 21

Rep: Reputation: Disabled
u can use this....
grep -hf 1.txt 2.txt | grep -hf 3.txt | grep -hf 4.txt | grep -hf 5.txt | grep -hf 6.txt> common.txt

-f, --file=FILE obtain PATTERN from FILE
-h, --no-filename suppress the prefixing filename on output

also you can use if needed :

-w, --word-regexp force PATTERN to match only whole words
-x, --line-regexp force PATTERN to match only whole lines

hope this helps !!
 
1 members found this post helpful.
Old 12-20-2011, 04:26 PM   #5
rahularjun86
LQ Newbie
 
Registered: Apr 2011
Location: Frankfurt Am Main, Germany
Distribution: FC14
Posts: 9

Original Poster
Rep: Reputation: 0
Thank you all,, your suggestions are really helpful..many thanks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Bash script to fgrep a large file. With list as source for searching. the_file Linux - Newbie 14 01-24-2011 03:42 PM
[SOLVED] Using sed to remove text in fgrep string manwithaplan Linux - General 6 10-11-2009 01:00 AM
fgrep error pmelnikov General 1 04-16-2009 10:17 AM
fgrep or find command question. swamprat Linux - Newbie 2 02-13-2008 01:26 PM

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

All times are GMT -5. The time now is 09:37 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration