[英]Find, grep and then wc in Linux
我想首先 grep 在日期时间范围内创建的日志文件中的一些文本,然后 output 该文本的出现次数。 我已经设法找出 Unix 命令来查找日期范围内的文件,并找出 grep 来查找这些文件中的文本。 但是,我无法找到一种方法来查找该文本的出现次数
find . -maxdepth 1 -newermt "2021-10-06 02:06:00" ! -newermt "2021-10-06 06:30:00" | xargs grep "text"
最后尝试使用xargs wc -l
但它没有用
如果你想计算出现次数,你不需要最后使用xargs
,只需将它 pipe 到wc -l
。
root@HOST:/var/log/apache2# find . -maxdepth 1 -type f -newermt "2021-10-06 02:06:00" ! -newermt "2021-10-06 06:30:00"
./error.log
./error.log.1
root@HOST:/var/log/apache2# find . -maxdepth 1 -type f -newermt "2021-10-06 02:06:00" ! -newermt "2021-10-06 06:30:00" | xargs grep "Apache"
./error.log:[Wed Oct 06 06:25:11.574179 2021] [mpm_prefork:notice] [pid 4138] AH00163: Apache/2.4.29 (Ubuntu) mod_fcgid/2.3.9 OpenSSL/1.1.1j configured -- resuming normal operations
./error.log.1:[Tue Oct 05 06:25:09.859586 2021] [mpm_prefork:notice] [pid 4138] AH00163: Apache/2.4.29 (Ubuntu) mod_fcgid/2.3.9 OpenSSL/1.1.1j configured -- resuming normal operations
root@HOST:/var/log/apache2# find . -maxdepth 1 -type f -newermt "2021-10-06 02:06:00" ! -newermt "2021-10-06 06:30:00" | xargs grep "Apache" | wc -l
2
root@HOST:/var/log/apache2#
这是你的出现次数: 2
解释:
./error.log:[Wed Oct 06 06:25:11.574179 2021] [mpm_prefork:notice] [pid 4138] AH00163: Apache/2.4.29 (Ubuntu) mod_fcgid/2.3.9 OpenSSL/1.1.1j configured -- resuming normal operations
./error.log.1:[Tue Oct 05 06:25:09.859586 2021] [mpm_prefork:notice] [pid 4138] AH00163: Apache/2.4.29 (Ubuntu) mod_fcgid/2.3.9 OpenSSL/1.1.1j configured -- resuming normal operations
find. -maxdepth 1 -type f -newermt "2021-10-06 02:06:00": -newermt "2021-10-06 06:30:00" | xargs grep "Apache" | xargs wc -l
find. -maxdepth 1 -type f -newermt "2021-10-06 02:06:00": -newermt "2021-10-06 06:30:00" | xargs grep "Apache" | xargs wc -l
find. -maxdepth 1 -type f -newermt "2021-10-06 02:06:00": -newermt "2021-10-06 06:30:00" | xargs grep "Apache" | xargs wc -l
就像调用: wc -l (output of previous command here)
wc -l < (output of previous command here)
,即:我们将 output of previous command 传递给 wc 的 STDINxargs
,我们应该只使用 pipe 它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.