LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   fgrep help please (https://www.linuxquestions.org/questions/linux-newbie-8/fgrep-help-please-4175458752/)

bloodstreetboy 04-19-2013 05:51 AM

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?

druuna 04-19-2013 06:06 AM

Quote:

Originally Posted by bloodstreetboy (Post 4934662)
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.

bloodstreetboy 04-19-2013 08:35 AM

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.

TB0ne 04-19-2013 09:16 AM

Quote:

Originally Posted by bloodstreetboy (Post 4934736)
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/^-//"

shivaa 04-19-2013 10:08 AM

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.


bloodstreetboy 04-22-2013 12:08 AM

@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.


All times are GMT -5. The time now is 10:19 AM.