将文件中的输入行用于 unix grep

Use input lines from file for unix grep

我的文件系统上有一个文件,里面有很多 pids。每个 pid 都在一个新行上。例如...

26011
26013
26016
26017

我想使用单个命令为文件中的每个 pid 执行 'ps -eaf | grep pid'。

有人可以帮助我如何使用文件中的输入 pids 以及文件中的每一行。

谢谢

你可以这样做:

for pid in `cat temp.pids`; do ps -eaf | grep $pid; done

其中 temp.pids 是包含每行一个 PID 的文件

如果文件中有所有 pid,则可以使用 grep -f 将其用作模式本身:

ps -eaf | grep -f file

来自man grep

-f FILE, --file=FILE

Obtain patterns from FILE, one per line. The empty file contains zero patterns, and therefore matches nothing.

也许试试:

ps -ef | grep -E "$(cat pids.txt | tr '\n' '|' | head -c -1)"

这会将所有 pid 与管道连接在一起,并将其用作 grep

的正则表达式