【发布时间】:2016-09-12 01:15:32
【问题描述】:
我在文件中有这些行:
postgres 2609 21030 0 12:49 ? 00:00:00 postgres: postgres postgres [local] idle in transaction
postgres 2758 21030 0 12:51 ? 00:00:00 postgres: postgres postgres [local] idle in transaction
postgres 28811 21030 0 09:26 ? 00:00:00 postgres: postgres postgres [local] idle in transaction
postgres 32200 21030 0 11:40 ? 00:00:00 postgres: postgres postgres [local] idle in transaction
postgres 32252 21030 0 11:41 ? 00:00:00 postgres: postgres postgres [local] idle in transaction
我需要分开第二列的值来处理它们。我已经完成了这段代码:
pid=$(cat idle_log.txt | cut -d" " -f2)
echo $pid
但它只在结果中给了我 28811 32200 32252。如您所见,列表中没有 2609 2758 的痕迹,我也想得到它们。 我也想在提取 pid 后对它们进行计数。 我用过:
npid=$(grep -o " " <<< $pid | grep -c .)
它为 28811 32200 32252 的结果返回 2 我需要它返回 3 作为进程计数。 最后我想用while循环处理一些事情,但是命令输出一次返回结果,我不能以循环格式一个一个地处理它们。
谢谢大家的帮助。
【问题讨论】:
标签: linux bash ubuntu grep cut