1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Grep help needed (syntax)

Discussion in 'Linux and Unix' started by raeba, Feb 15, 2005.

Thread Status:
Not open for further replies.
Advertisement
  1. raeba

    raeba Thread Starter

    Joined:
    Feb 6, 2005
    Messages:
    13
    Hi.

    I am trying to get my command line string correct to find a url (string of text) in all the files below a certain directory on the server.

    Here's what I've been typing...

    fgrep -i -r 'foobar' /home/* > found.txt

    However, I want to skip log files (that end in log and processed) and I only want output of the filenames that the string (foobar) appears in, I don't want the output file to have the actual example of the appearance (just the dir/filenames).

    So, how can I do this so that I'll find 'foobar' in all the files, except logs, processed logs, and perhaps exclude *.gz too, with the output showing only the names of files that foobar appears in (one per line)?

    Thanks a bunch.

    Hugs,

    Raeba
     
  2. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    Pipe it to another grep command.
     
  3. raeba

    raeba Thread Starter

    Joined:
    Feb 6, 2005
    Messages:
    13
    I'm new to this command, please type me out a command line that will do what I need.

    Thanks a lot for your help.

    Raeba
     
  4. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    A pipe just outputs the output of one command to another command. So all you have to do is use the pipe | to output your current grep statement to another grep statement to filter out what you dont want.

    grep | grep
     
  5. raeba

    raeba Thread Starter

    Joined:
    Feb 6, 2005
    Messages:
    13
    I understand the pipe indicator (|). I don't know how to exclude et cetera. If someone would could type out a command line string for me, based upon the above, I'd appreciate it.

    Raeba ;)
     
  6. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    Do you know what the manual pages are? At a shell prompt type: man grep

    Look at grep -v and grep -f.
     
  7. raeba

    raeba Thread Starter

    Joined:
    Feb 6, 2005
    Messages:
    13
    I'm already calling -f by using fgrep. The -v swtich is for selecting an inversion, which has nothing to do with what I am trying to do.

    Thanks for trying. Any one esle care to give an entire command syntax statement for me (based upon my needs shown above)?

    Raeba
     
  8. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    Use the -v on the second grep statement that you are piping your output to.
     
  9. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    fgrep is the same as grep -F not grep -f.
     
  10. raeba

    raeba Thread Starter

    Joined:
    Feb 6, 2005
    Messages:
    13
    LwdSquashman, I think I know what you meant now about the -v ... Anyway, I am still trying to get a command line going here...

    :)
     
  11. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    Ok, I was trying to help you by making you think but some people just want the answer. When I learned Linux several years go, my class was taught by this little Italian guy who would never give us the answers to anything. He would sit down with you and help you find the answer but would never give you the answer directly.

    So I think this will do what you want to do.

    Create a file with the file extensions you dont want included. One file extension on each line. Use that filename in your second grep statement.
    .gz
    .log
    .whatever

    fgrep -l -i -r 'foobar' /home/* | grep -f filename -v
     
  12. raeba

    raeba Thread Starter

    Joined:
    Feb 6, 2005
    Messages:
    13

    I am not trying to "learn" (as in a classroom) anything in Linux or it's command per se. I just need to get past this one thing, to get something much more important done.

    It will probably be the last time I will ever grep for anything...in my case.

    Thanks for the time and help. By the way, it saves bandwidth to just to ignore the message or actually answer it -- so that a thread doen't linger on - serving no purpose for the one who posted it (or future readers with same issue). Enough said.

    Thanks again.

    Raeba :p
     
  13. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    Trivial!
     
  14. deuce868

    deuce868

    Joined:
    Nov 2, 2000
    Messages:
    637
    Anyone not using grep more than once on a linux machine is doing a lot of things the hard way. :D


    oops, wasted bandwith...
     
  15. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/330901

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice