methuselah
methuselah

Reputation: 13206

Parse file to get contents in text file and output to new file

I have multiple text files in a directory with the following contents and I want to loop through them, extract the items in bold and output them into a text file:

...

Copy File ${RESOURCES}/aXpKcHLe2f.DAT ${NEWDIR}/File2.DAT

Compare File Actions ${TEMPLATES}/KGuovBrMwK ${NEWDIR}/DT

...

Copy File ${RESOURCES}/9ZzUpgmTy0.DAT ${NEWDIR}/File2.DAT

Compare File Actions ${TEMPLATES}/qpk3BiCvRG ${NEWDIR}/DT

..

How would I do this in a bash script? I understand that it requires some regex, but I am not sure where or how to start.

Upvotes: 1

Views: 73

Answers (1)

anubhava
anubhava

Reputation: 785128

You may use this find + awk solution:

find . -name '*.txt' -exec awk -F '/' 'NF>1{sub(/ .*/, "", $2); print $2}' {} +

This will print desired output from 2nd column delimited by / in each *.txt file. If your file extensions are different then you can change this -name pattern accordingly.

Upvotes: 2

Related Questions