LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   fgrep pipes (https://www.linuxquestions.org/questions/linux-newbie-8/fgrep-pipes-919459/)

rahularjun86 12-19-2011 06:12 AM

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}'

Cedrik 12-19-2011 07:57 AM

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

rknichols 12-19-2011 04:42 PM

Quote:

Originally Posted by rahularjun86 (Post 4553566)
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

singhharmeet 12-19-2011 05:52 PM

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 !!

rahularjun86 12-20-2011 04:26 PM

Thank you all,, your suggestions are really helpful..many thanks :)


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