Results 1 to 5 of 5

Thread: Search all documents ending in .log for a string of text?

  1. #1
    Join Date
    Oct 2010
    Beans
    186
    Distro
    Ubuntu 15.10 Wily Werewolf

    Question Search all documents ending in .log for a string of text?

    I'm trying to search all .log files in ~/.irssi/irclogs/ and it's sub directories for the string 'irssi' and I had though the command I'd used for something similar before was:

    Code:
    $HOME -name 'documentname*' -exec grep "string" /dev/null '{}' +
    Evidently this is not the case as bash outputs:

    Code:
    bash: /home/dusf: is a directory
    How should I edit the command, and is it possible to output every line found containing the string to file?
    Last edited by howefield; January 17th, 2016 at 01:20 PM. Reason: reset font properties.

  2. #2
    Join Date
    Jan 2008
    Location
    Auckland, New Zealand
    Beans
    3,132
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Search all documents ending in .log for a string of text?

    Close, it should be:
    Code:
    find ~/.irssi/irclogs -name "*.log" -exec grep -H "irssi" {} \;
    $HOME just means your home directory, you would usually start a command with the name of an executable to run.

    The "-H" means grep will print out the file name if it finds a match. I usually want this option enabled when using grep with find but you might not. You can use the "-i" option for grep if you want the grep to be case insensitive too.

    If you want to output the result of a command to a file then just add " > filename.txt" at the end.
    Last edited by ad_267; October 27th, 2010 at 08:54 AM.

  3. #3
    Join Date
    Oct 2010
    Beans
    186
    Distro
    Ubuntu 15.10 Wily Werewolf

    Re: Search all documents ending in .log for a string of text?

    Quote Originally Posted by ad_267 View Post
    Close, it should be:
    Code:
    find ~/.irssi/irclogs -name "*.log" -exec grep -H "irssi" {} \;
    $HOME just means your home directory, you would usually start a command with the name of an executable to run.

    The "-H" means grep will print out the file name if it finds a match. I usually want this option enabled when using grep with find but you might not. You can use the "-i" option for grep if you want the grep to be case insensitive too.

    If you want to output the result of a command to a file then just add " > filename.txt" at the end.
    Works great, thanks!

    Could we an an argument to search for lines with the string "irssi", but don't include those that also contain the string "quit"?

  4. #4
    Join Date
    Jan 2008
    Location
    Auckland, New Zealand
    Beans
    3,132
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Search all documents ending in .log for a string of text?

    Yeah one way would be to pipe the output of that command into another grep that removes any lines containing quit.

    So the full command with the output going to a file would be:

    Code:
    find ~/.irssi/irclogs -name "*.log" -exec grep -H "irssi" {} \; | grep -v "quit" > filename.txt
    The "|" sends the output from the first find command into the second grep and the "-v" option means invert, so it outputs any lines that don't contain quit.

    Another way might be to use a regular expression with just the one grep that matched lines containing irssi but not quit.

  5. #5
    Join Date
    Oct 2010
    Beans
    186
    Distro
    Ubuntu 15.10 Wily Werewolf

    Arrow Re: Search all documents ending in .log for a string of text?

    Quote Originally Posted by ad_267 View Post
    Yeah one way would be to pipe the output of that command into another grep that removes any lines containing quit.

    So the full command with the output going to a file would be:

    Code:
    find ~/.irssi/irclogs -name "*.log" -exec grep -H "irssi" {} \; | grep -v "quit" > filename.txt
    The "|" sends the output from the first find command into the second grep and the "-v" option means invert, so it outputs any lines that don't contain quit.

    Another way might be to use a regular expression with just the one grep that matched lines containing irssi but not quit.
    Thanks

    Grep seems like a very cool and powerful tool which I look forward to learning to use.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •