LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 04-19-2013, 06:51 AM   #1
bloodstreetboy
Member
 
Registered: May 2012
Posts: 195
Blog Entries: 3

Rep: Reputation: 37
fgrep help please


Can you please give me information about fgrep?
What does the following command do?
Code:
grep -f file1 file2
What is the difference between fgrep and grep -f as well as Fgrep and grep -F?

If I want to print common lines between two files, how can I do this with grep?
 
Old 04-19-2013, 07:06 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2376Reputation: 2376Reputation: 2376Reputation: 2376Reputation: 2376Reputation: 2376Reputation: 2376Reputation: 2376Reputation: 2376Reputation: 2376Reputation: 2376
Quote:
Originally Posted by bloodstreetboy View Post
What does the following command do?
Code:
grep -f file1 file2
If I want to print common lines between two files, how can I do this with grep?
Have you tried the command you mentioned? It does exactly what you ask for.

The grep manual page answers all your (other) questions.
 
Old 04-19-2013, 09:35 AM   #3
bloodstreetboy
Member
 
Registered: May 2012
Posts: 195
Blog Entries: 3

Original Poster
Rep: Reputation: 37
Yes I have tried this command.
I have two files with following contents.
The file process.txt have
Code:
 12:24:37 up 59 min,  6 users,  load average: 0.58, 1.64, 1.89      thunderbird back-up
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty7     :0               11:25   59:16   1:14   0.21s gnome-session      jmeter test plans
root     pts/2    :0.0             11:30   19:54   0.01s  0.01s bash
root     pts/3    :0.0             11:30   52:36   0.16s  0.16s bash
root     pts/4    :0.0             11:32   51:14  28:42  28:42  ffmpeg -i /medi
root     pts/5    :0.0             11:33   50:28  29:05  29:05  ffmpeg -i /medi
root     pts/6    :0.0             11:56    0.00s  0.01s  0.00s w      Test material

May 11 15:31
============
/root - 6.3G - 1,06,342/ 4.4G - 98277
 .cpan - 331.9MB - 50056
 .cache- 495.5MB - 555
/usr  - 5.7G
/var - 1.2G
/tmp - 68.7K
/sys - 545.9M
/selinux - nothing
/sbin - 7.4M
/proc - 1016.8M
/opt - 1.3G /1.3G-81243
/lost+found - nothing
/lib - 634.9M
/home - 51.6M
/etc - 26.4M
/dev - 960M
/boot - 97.1M
/bin - 6.2M
& the file space.txt have
Code:
May 11 15:31
============
/root - 6.3G - 1,06,342/ 4.4G - 98277
 .cpan - 331.9MB - 50056
 .cache- 495.5MB - 555
/usr  - 5.7G
/var - 1.2G
/tmp - 68.7K
/sys - 545.9M
/selinux - nothing
/sbin - 7.4M
/proc - 1016.8M
/opt - 1.3G /1.3G-81243
/lost+found - nothing
/lib - 634.9M
/home - 51.6M
/etc - 26.4M
/dev - 960M
/boot - 97.1M
/bin - 6.2M
======================================
Jul 14 19:43
============
File system
   Desktop
   Documents
   /opt - htdocs + database
   thunderbird back-up
   
     Demo Projects
     htdocs
     jmeter test plans
     Management
     thunderbird back-up
     Test cases for projects
     Test material

16 GB
   Empty
73 GB
   Empty
==================================================
Remove
------
Eclipse
Netbeans
Seamonkey
The out put of
$ grep -f process.txt space.txt
is same as
cat space.txt

I am not able to understand why does not it print common lines.
 
Old 04-19-2013, 10:16 AM   #4
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 15,532

Rep: Reputation: 2811Reputation: 2811Reputation: 2811Reputation: 2811Reputation: 2811Reputation: 2811Reputation: 2811Reputation: 2811Reputation: 2811Reputation: 2811Reputation: 2811
Quote:
Originally Posted by bloodstreetboy View Post
Yes I have tried this command.
I have two files with following contents.

The output of
$ grep -f process.txt space.txt
is same as
cat space.txt

I am not able to understand why does not it print common lines.
The grep command posted is the 'typical' solution. However, it does produce the same results for me on openSUSE (and a diff confirms it...it's essentially cat'ing a file).

So...options:
  • The diff command:
    Code:
    diff --changed-group-format='%=' process.txt space.txt
  • Using sort and comm:
    Code:
    sort -o tmp1 process.txt 
    sort -o tmp2 space.txt
    comm -12 tmp1 tmp2
  • A Perl one-liner:
    Code:
     perl -ne"print if s/^-//"
 
1 members found this post helpful.
Old 04-19-2013, 11:08 AM   #5
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
fgrep is equivalent to grep -F, not grep -f

Code:
~$ fgrep <keyword> file1 file2         ## Same as grep -F
~$ grep -F <keyword> file1 file2       ## Same as fgrep
Whereas grep -f is used to specify file as argument. From manual of grep (see here):
Code:
-f FILE, --file=FILE
      Obtain patterns from FILE, one per line. The empty file con-
      tains zero patterns, and therefore matches nothing.

Last edited by shivaa; 04-22-2013 at 08:57 AM.
 
1 members found this post helpful.
Old 04-22-2013, 01:08 AM   #6
bloodstreetboy
Member
 
Registered: May 2012
Posts: 195
Blog Entries: 3

Original Poster
Rep: Reputation: 37
@TB0ne & @shivaa
Thanks for your explanation. I got it.
I think I should use diff or comm to print common lines, that would be much better.
Thanks again.
 
  


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
[SOLVED] fgrep pipes rahularjun86 Linux - Newbie 4 12-20-2011 05:26 PM
[SOLVED] Using sed to remove text in fgrep string manwithaplan Linux - General 6 10-11-2009 02:00 AM
fgrep error pmelnikov General 1 04-16-2009 11:17 AM
fgrep or find command question. swamprat Linux - Newbie 2 02-13-2008 02:26 PM


All times are GMT -5. The time now is 02:45 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration