alexherm
alexherm

Reputation: 1362

grep and awk, combine commands?

I have file that looks like:

This is a RESTRICTED site.
All connections are monitored and recorded.
Disconnect IMMEDIATELY if you are not an authorized user!
sftp> cd outbox
sftp> ls -ltr
-rw-------   1 0        0            1911 Jun 12 20:40 61N0584832_EDIP000749728818_MFC_20190612203409.txt
-rw-------   1 0        0            1878 Jun 13 06:01 613577165_EDIP000750181517_MFC_20190613055207.txt

I want to print only the .txt file names, ideally in one command.

I can do:

grep -e '^-' outfile.log > outfile.log2

..which gives only the lines that start with '-'.

-rw-------   1 0        0            1911 Jun 12 20:40 61N0584832_EDIP000749728818_MFC_20190612203409.txt
-rw-------   1 0        0            1878 Jun 13 06:01 613577165_EDIP000750181517_MFC_20190613055207.txt

And then:

awk '{print $9}' outfile.log2 > outfile.log3

..which gives the desired output:

61N0584832_EDIP000749728818_MFC_20190612203409.txt
613577165_EDIP000750181517_MFC_20190613055207.txt

And so the question is, can these 2 commands be combined into 1?

Upvotes: 3

Views: 2650

Answers (4)

Jotne
Jotne

Reputation: 41460

Since he clearly writes I want to print only the .txt file names, we should test for txt file and since file name are always the latest column we make it more portable by only test the latest filed line this:

awk '$NF ~ /\.txt$/{print $NF}' outfile.log > outfile.log2
61N0584832_EDIP000749728818_MFC_20190612203409.txt
613577165_EDIP000750181517_MFC_20190613055207.txt

Upvotes: 1

William Pursell
William Pursell

Reputation: 212674

Seems like matching the leading - is not really want you want. If you want to just get the .txt files as output, filter on the file name:

awk '$9 ~ /\.txt$/{print $9}' input-file

Upvotes: 3

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627535

You may use a single awk:

awk '/^-/{ print $9 }' file > outputfile

Or

awk '/^-/{ print $9 }' file > tmp && mv tmp file

It works like this:

  • /^-/ - finds each line starting with -
  • { print $9 } - prints Field 9 of the matching lines only.

Upvotes: 8

P....
P....

Reputation: 18411

Using grep with PCRE enabled (-P) flag:

grep -oP '^-.* \K.*' outfile.log 
61N0584832_EDIP000749728818_MFC_20190612203409.txt
613577165_EDIP000750181517_MFC_20190613055207.txt

'^-.* \K.*' : Line starting with - till last white space are matched but ignored (anything left of \K will be matched and ignored) and matched part right of \K will be printed.

Upvotes: 1

Related Questions