如何将 xargs 提供给管道 grep 以获取管道 cat 命令
How to feed xargs to a piped grep for a piped cat command
如何将 xargs 提供给 piped grep 以获得 piped cat 命令。
命令 1:
(为特定日期时间生成具有唯一 PID 的 grep 模式,从 runtime.log 读取)
cat runtime.log | grep -e '2018/09/13 14:50' | awk -F'[ ]' '{print }' | awk -F'PID=' '{print }' | sort -u | xargs -I % echo '2018/09/13 14:50.*PID='%
以上命令的输出是(自定义 grep 模式):
2018/09/13 14:50.*PID=13109
2018/09/13 14:50.*PID=14575
2018/09/13 14:50.*PID=15741
命令 2:
(读取 runtime.log 并根据 grep 模式获取适当的行(理想情况下 grep 模式应来自命令 1))
cat runtime.log | grep '2018/09/13 14:50.*PID=13109'
问题是如何结合命令 1 和命令 2
以下组合版本的命令没有给出预期的输出(生成的输出中有日期不是“2018/09/13 14:50”的行)
cat runtime.log | grep -e '2018/09/13 14:50' | awk -F'[ ]' '{print }' | awk -F'PID=' '{print }' | sort -u | xargs -I % echo '2018/09/13 14:50.*PID='% | cat runtime.log xargs grep
grep 有一个选项 -f
。来自 man grep
:
-f FILE, --file=FILE
Obtain patterns from FILE, one per line. The empty file contains zero patterns, and therefore matches nothing. (-f is specified by POSIX .)
所以你可以使用
cat runtime.log | grep -e '2018/09/13 14:50' | awk -F'[ ]' '{print }' | awk -F'PID=' '{print }' | sort -u | xargs -I % echo '2018/09/13 14:50.*PID='% > a_temp_file
cat runtime.log | grep -f a_temp_file
shell 的语法可以避免创建临时文件。 <()
。来自 man bash
:
Process Substitution
Process substitution is supported on systems that support named pipes
(FIFOs) or the /dev/fd method of naming open files. It takes the form
of <(list) or >(list). The process list is run with its input or
output connected to a FIFO or some file in /dev/fd. The name of this
file is passed as an argument to the current command as the result of
the expansion. If the >(list) form is used, writing to the file will
provide input for list. If the <(list) form is used, the file passed
as an argument should be read to obtain the output of list.
因此您可以将其合并为:
cat runtime.log | grep -f <(cat runtime.log | grep -e '2018/09/13 14:50' | awk -F'[ ]' '{print }' | awk -F'PID=' '{print }' | sort -u | xargs -I % echo '2018/09/13 14:50.*PID='%)
如何将 xargs 提供给 piped grep 以获得 piped cat 命令。
命令 1: (为特定日期时间生成具有唯一 PID 的 grep 模式,从 runtime.log 读取)
cat runtime.log | grep -e '2018/09/13 14:50' | awk -F'[ ]' '{print }' | awk -F'PID=' '{print }' | sort -u | xargs -I % echo '2018/09/13 14:50.*PID='%
以上命令的输出是(自定义 grep 模式):
2018/09/13 14:50.*PID=13109
2018/09/13 14:50.*PID=14575
2018/09/13 14:50.*PID=15741
命令 2: (读取 runtime.log 并根据 grep 模式获取适当的行(理想情况下 grep 模式应来自命令 1))
cat runtime.log | grep '2018/09/13 14:50.*PID=13109'
问题是如何结合命令 1 和命令 2
以下组合版本的命令没有给出预期的输出(生成的输出中有日期不是“2018/09/13 14:50”的行)
cat runtime.log | grep -e '2018/09/13 14:50' | awk -F'[ ]' '{print }' | awk -F'PID=' '{print }' | sort -u | xargs -I % echo '2018/09/13 14:50.*PID='% | cat runtime.log xargs grep
grep 有一个选项 -f
。来自 man grep
:
-f FILE, --file=FILE
Obtain patterns from FILE, one per line. The empty file contains zero patterns, and therefore matches nothing. (-f is specified by POSIX .)
所以你可以使用
cat runtime.log | grep -e '2018/09/13 14:50' | awk -F'[ ]' '{print }' | awk -F'PID=' '{print }' | sort -u | xargs -I % echo '2018/09/13 14:50.*PID='% > a_temp_file
cat runtime.log | grep -f a_temp_file
shell 的语法可以避免创建临时文件。 <()
。来自 man bash
:
Process Substitution
Process substitution is supported on systems that support named pipes (FIFOs) or the /dev/fd method of naming open files. It takes the form of <(list) or >(list). The process list is run with its input or output connected to a FIFO or some file in /dev/fd. The name of this file is passed as an argument to the current command as the result of the expansion. If the >(list) form is used, writing to the file will provide input for list. If the <(list) form is used, the file passed as an argument should be read to obtain the output of list.
因此您可以将其合并为:
cat runtime.log | grep -f <(cat runtime.log | grep -e '2018/09/13 14:50' | awk -F'[ ]' '{print }' | awk -F'PID=' '{print }' | sort -u | xargs -I % echo '2018/09/13 14:50.*PID='%)