grep和awk,结合命令吗? [英] grep and awk, combine commands?
本文介绍了grep和awk,结合命令吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的文件看起来像:
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
我只想用一个命令打印.txt文件名.
I want to print only the .txt file names, ideally in one command.
我可以做到:
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
然后:
awk '{print $9}' outfile.log2 > outfile.log3
..给出所需的输出:
..which gives the desired output:
61N0584832_EDIP000749728818_MFC_20190612203409.txt
613577165_EDIP000750181517_MFC_20190613055207.txt
所以问题是,这2个命令可以合并为1个吗?
And so the question is, can these 2 commands be combined into 1?
推荐答案
您可以使用一个awk
:
awk '/^-/{ print $9 }' file > outputfile
或
awk '/^-/{ print $9 }' file > tmp && mv tmp file
它是这样的:
-
/^-/
-查找以-
开头的每一行
-
{ print $9 }
-仅显示匹配行的字段9.
/^-/
- finds each line starting with-
{ print $9 }
- prints Field 9 of the matching lines only.
这篇关于grep和awk,结合命令吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文