grep command for a text file in multiple directories

27,810

Solution 1

To recursively search using grep, use the -R option.

To search for an exact string, use -F, so that 2* isn't treated as a regular expression.

To search only on specific filenames, use the --include option. Combined:

grep -FR --include=DATA.txt '2* x' main_directory > another_text_file

Solution 2

Since you know the name of the target files, you can also do

grep '^2\* x' */DATA.txt > newfile

Or, with awk:

awk '^/2\* x/' */DATA.txt > newfile

And Perl:

perl -ne 'print if /2\* x/' */DATA.txt > newfile

Solution 3

General approach:

grep -r <pattern>

or

specific approach:

find -name <file patterns which you want to find> | xargs grep <pattern you looking for>
Share:
27,810

Related videos on Youtube

deepblue_86
Author by

deepblue_86

Updated on September 18, 2022

Comments

  • deepblue_86
    deepblue_86 over 1 year

    I have 24 directories in a specific directory (main_directory). Each 24 directory have a text file whose name is DATA.txt.

    I need to use grep command to extract below specific pattern for each text file;

    2* x  = 3800689.6402     y  = 882077.3636     z  = 5028791.2953
    

    2* x = part is constant for all DATA.txt. The other numeric numbers are variable. I need to extract above line for each DATA.txt and save them into another text file. Which script I can use for this process?

    • Admin
      Admin about 8 years
      Just grep -FR '2* x' main_directory > another_text_file?
    • Admin
      Admin about 8 years
      @muru this code search all text files in directories so it works very slow. How can I restrict the search only DATA.txt file.
    • Admin
      Admin about 8 years
      Add --include=DATA.txt?
  • deepblue_86
    deepblue_86 about 8 years
    @guru each directory name consist of numbers like, 0001 0102 0203 ...2324. Is there a way to greping these directories w.r.t. the number orders? (i.e, grep 0001, 0102, 0203, 0304........2324 and append the results into text file with that order). Above command greping random.
  • muru
    muru about 8 years
    @deepblue_86 not for grep itself. The above command would have the file paths in the output, so sort it: grep … | sort -f: -k1,1 > another_text_file
  • deepblue_86
    deepblue_86 about 8 years
    alright @muru, it is oked.
  • 404pio
    404pio about 8 years
    who use newlines in file names? My find search for filenames with spaces. It's problem with xargs - it needs additional argument for files with blank spaces.
  • terdon
    terdon about 8 years
    Some people choose to, other times it happens by accident. The main point is that newlines are allowed in file names so it is good practice to consider them. And yes, find can deal with both spaces and newlines and anything else. Which is why find -name foo -exec grep bar {} + is better than xargs but, if you want to use xargs, you need to do find -name foo -print0 | xargs -0 grep bar.