Harry Smith
Harry Smith

Reputation: 278

Perform awk command on same file in many folders and append output

I have 40 folders (COPD1...COPD40) all that have a file called summary.txt. I want to extract the last element of the last line (e.g. "90%") in all of these folders. I know how to do this once :

awk '/Overall alignment rate:/{print $NF}' summary.txt 

I am not sure how to do this for all 40 folders and append that extracted element to a new .txt file so the final product looks like:

COPD1 98%
COPD2 96%
...
COPD40 97.5%

Any assistance would be appreciated.

Upvotes: 1

Views: 164

Answers (1)

John1024
John1024

Reputation: 113984

$ awk '/Overall alignment rate:/{print FILENAME,$NF}' COPD*/summary.txt
COPD1/summary.txt 90%
COPD2/summary.txt 90%
COPD3/summary.txt 90%
[...snip...]

Notes:

  1. Awk has a variable, FILENAME, that is the current file name.

  2. The shell glob COPD*/summary.txt will expand to all files named summary.txt that are in a directory that starts with COPD.

Documentation

From the POSIX spec for awk:

FILENAME
A pathname of the current input file. Inside a BEGIN action the value is undefined. Inside an END action the value shall be the name of the last input file processed.

Upvotes: 2

Related Questions