4

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?

  • Just grep -FR '2* x' main_directory > another_text_file? – muru Apr 1 '16 at 6:58
  • @muru this code search all text files in directories so it works very slow. How can I restrict the search only DATA.txt file. – deepblue_86 Apr 1 '16 at 7:07
  • Add --include=DATA.txt? – muru Apr 1 '16 at 7:08
9

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
  • @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. – deepblue_86 Apr 1 '16 at 7:32
  • @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 – muru Apr 1 '16 at 7:33
  • alright @muru, it is oked. – deepblue_86 Apr 1 '16 at 7:37
5

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
1

General approach:

grep -r <pattern>

or

specific approach:

find -name <file patterns which you want to find> | xargs grep <pattern you looking for>
  • 1
    The find approach will fail if any file names or their paths contain spaces or newlines . – terdon Apr 1 '16 at 10:10
  • 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. – 404pio Apr 1 '16 at 12:45
  • 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. – terdon Apr 1 '16 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.