Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unix grep和awk,合并命令?_Unix_Awk_Grep - Fatal编程技术网

Unix grep和awk,合并命令?

Unix grep和awk,合并命令?,unix,awk,grep,Unix,Awk,Grep,我的文件看起来像: 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_EDIP00074

我的文件看起来像:

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文件名,最好在一个命令中打印

我可以做到:

grep -e '^-' outfile.log > outfile.log2
…只给出以“-”开头的行

-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
..提供所需输出:

61N0584832_EDIP000749728818_MFC_20190612203409.txt
613577165_EDIP000750181517_MFC_20190613055207.txt

因此问题是,这两个命令是否可以组合成1个?

使用
grep
PCRE
启用(
-p
)标志:


'^-.*\K.*
:从
-
开始直到最后一个空格被匹配但忽略的行(任何
\K
的左边都将被匹配并忽略),并且将打印
\K
右边的匹配部分

您可以使用单个
awk

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

它的工作原理如下:

  • /^-/
    -查找以
    -
  • {print$9}
    -仅打印匹配行的字段9

似乎与领先的
-
匹配并不是您真正想要的。如果只想将
.txt
文件作为输出,请筛选文件名:

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

因为他清楚地写了
,我只想打印.txt文件名
,所以我们应该测试txt文件,因为文件名总是最新的列,所以我们只需测试最新的字段行,就可以更方便地进行移植,如下所示:

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

dos2unix
您的文件名是否曾经包含空格,例如
61N0584832 MFC_20190612203409.txt
?因为您能够读取请求,所以加1<代码>我只想打印.txt文件名
awk '$9 ~ /\.txt$/{print $9}' input-file
awk '$NF ~ /\.txt$/{print $NF}' outfile.log > outfile.log2
61N0584832_EDIP000749728818_MFC_20190612203409.txt
613577165_EDIP000750181517_MFC_20190613055207.txt