我有看起来像的文件:
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
Run Code Online (Sandbox Code Playgroud)
我只想在一个命令中打印.txt文件名。
我可以:
grep -e '^-' outfile.log > outfile.log2
Run Code Online (Sandbox Code Playgroud)
..仅给出以“-”开头的行。
-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
Run Code Online (Sandbox Code Playgroud)
然后:
awk '{print $9}' outfile.log2 > outfile.log3
Run Code Online (Sandbox Code Playgroud)
..给出所需的输出:
61N0584832_EDIP000749728818_MFC_20190612203409.txt
613577165_EDIP000750181517_MFC_20190613055207.txt
Run Code Online (Sandbox Code Playgroud)
因此,问题是,这两个命令可以合并为1吗?
您可以使用一个awk:
awk '/^-/{ print $9 }' file > outputfile
Run Code Online (Sandbox Code Playgroud)
要么
awk '/^-/{ print $9 }' file > tmp && mv tmp file
Run Code Online (Sandbox Code Playgroud)
它是这样的:
/^-/ -查找以开头的每一行 -{ print $9 } -仅打印匹配行的字段9。