我想首先 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,但沒有奏效
uj5u.com熱心網友回復:
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我們應該用管道而不是使用它
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/317378.html
下一篇:僅獲取最新檔案的全域路徑
