Ubuntu – grep command for a text file in multiple directories

command linegreptext processing

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?

Best Answer

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
Related Question