1

I have directories named from 3 to 8. In each one there is a file called OUTCAR. I need to grep a number from this file as:

grep Elapsed OUTCAR|egrep -o "[0-9]+\.[0-9]+"

The lines in question look like that:

Elapsed time (sec): 45123.137

So, at the end I want to print two columns as the directory name and egrep output like:

3 80650.077
..
8 42757.057

For example when I elapse in folder 3 with grep Elapsed OUTCAR|egrep -o "[0-9]+\.[0-9]+" I get 80650.077 Many thanks for help!

1

With grep and sed:

grep -oP 'Elapsed time \(sec\):\K.*' {3..8}/OUTCAR | sed 's_/OUTCAR:__'

This uses the brace expansion {3..8}/OUTCAR to construct this list of arguments to the grep process:

3/OUTCAR 4/OUTCAR 5/OUTCAR …

grep searches these files for matching lines and outputs only the part after the colon with the relative file path and name. sed removes the unwanted file name part from every line, leaving just the directory name and the match (which already contains a space).

| improve this answer | |

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.