在 Linux 中查找、grep 然后 wc
Find, grep and then wc in Linux
我想先 grep 在 DateTime 范围内创建的日志文件中的一些文本,然后输出该文本的出现次数。
我已经设法找出 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
,只需将其传递给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
解释:
- xargs 读取先前命令的输出,然后用它构建下一个命令
- 上一个命令的输出是:
./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
的结果就像调用:wc -l (output of previous command here)
- 但这不是我们想要的(尝试手动调用它),我们想要的是
wc -l < (output of previous command here)
,即:我们将先前命令的输出传递给 wc 的 STDIN
- 所以,我们不应该使用
xargs
,而是直接通过管道
我想先 grep 在 DateTime 范围内创建的日志文件中的一些文本,然后输出该文本的出现次数。 我已经设法找出 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
,只需将其传递给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
解释:
- xargs 读取先前命令的输出,然后用它构建下一个命令
- 上一个命令的输出是:
./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
的结果就像调用:wc -l (output of previous command here)
- 但这不是我们想要的(尝试手动调用它),我们想要的是
wc -l < (output of previous command here)
,即:我们将先前命令的输出传递给 wc 的 STDIN - 所以,我们不应该使用
xargs
,而是直接通过管道