Find the answer to your Linux question:
Results 1 to 8 of 8
Hey guys, I'm trying to search for text within a bunch of numerically-consecutively-named text files, and would like to return a list of files contain a certain phrase. e.g. The ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2012
    Posts
    3

    Need a push in the right direction for scripting a little something...


    Hey guys,

    I'm trying to search for text within a bunch of numerically-consecutively-named text files, and would like to return a list of files contain a certain phrase.

    e.g.

    The following files exist:

    1000.txt
    1001.txt
    ..
    9998.txt
    9999.txt

    Using the "cat" command to print the text to the screen, I want to return a list of files that contain the exact phrase "rocker verb".

    Can anyone help out?

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    sounds home-worky.

    use grep to search.

  3. #3
    Just Joined!
    Join Date
    Sep 2012
    Posts
    3
    grep! Awesome..

    As for checking files recursively, how can I use the "cat" command to print files 0001.txt-9999.txt recursively? If I do that, I think I can just use a pipe (|) to mesh the cat command with grep..

    As for grep, is it possible to keep track of a positive search hit in grep (for example, in a file results.txt)? It would be fantastic if I could take the filename-variable (0001.txt..9999.txt) from the actual command line and write that to a file that is a positive hit in the grep search..

    Thanks for your help. Learning lots as I go..

  4. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    you can get the name of the file with -H. for example:

    Code:
    grep -H --color whatever 000[1-3].txt
    not sure why you need to use cat, though.

  5. #5
    Just Joined!
    Join Date
    Sep 2012
    Posts
    3
    Quote Originally Posted by atreyu View Post
    you can get the name of the file with -H. for example:

    Code:
    grep -H --color whatever 000[1-3].txt
    not sure why you need to use cat, though.
    I'm also curious about how to do this with data captured from things other than text files as well.

    e.g. using "ifconfig" to find out which network devices are on a certain lan, and saving the device name (.e.g. en1, en2) to a file. Or using "ls" to see which folders in the current directory contain a certain chunk of text and keeping track of that in a file..

  6. #6
    Just Joined!
    Join Date
    May 2012
    Posts
    43
    Quote Originally Posted by akickintheteeth View Post
    I'm also curious about how to do this with data captured from things other than text files as well.

    e.g. using "ifconfig" to find out which network devices are on a certain lan, and saving the device name (.e.g. en1, en2) to a file. Or using "ls" to see which folders in the current directory contain a certain chunk of text and keeping track of that in a file..
    Use pipes to parse output to another command as input, and '>' to write to files (> will overwrite, >> will append)

    Code:
    ls -l | grep textToSearch >> file.txt
    Last edited by Cancerous; 09-19-2012 at 11:30 PM.

  7. #7
    Just Joined!
    Join Date
    Feb 2009
    Posts
    14
    Hi,

    you can try do a little "for loop"

    ex:

    for i in [1..1000];do grep "rocker verb" file$i.txt && cat file$i.txt ;done

  8. #8
    Linux Newbie
    Join Date
    Nov 2009
    Posts
    223
    Teeth,

    Look into awk. I have used it countless times to isolate certain items from the output of commands etc.

    For example: ifconfig|egrep inet |egrep addr|awk -F: '{print ($2)}'|awk '{print ($1)}'

    Cheers VP

Posting Permissions

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