![如何在 Linux 中仅使用匹配的单词来 grep 行](https://linux22.com/image/751222/%E5%A6%82%E4%BD%95%E5%9C%A8%20Linux%20%E4%B8%AD%E4%BB%85%E4%BD%BF%E7%94%A8%E5%8C%B9%E9%85%8D%E7%9A%84%E5%8D%95%E8%AF%8D%E6%9D%A5%20grep%20%E8%A1%8C%20.png)
我有一个小问题。我有一个进程列表。执行此命令后
ps aux | grep postgres
我懂了
postgres 1178 0.0 0.2 320064 27060 ? S Apr12 0:05 /usr/lib/postgresql/10/bin/postgres -D /var/lib/postgresql/10/main -c config_file=/etc/postgresql/10/main/postgresql.conf
postgres 1314 0.0 0.0 320176 7052 ? Ss Apr12 0:00 postgres: 10/main: checkpointer process
postgres 1315 0.0 0.0 320064 4060 ? Ss Apr12 0:04 postgres: 10/main: writer process
postgres 1316 0.0 0.0 320064 9020 ? Ss Apr12 0:04 postgres: 10/main: wal writer process
postgres 1317 0.0 0.0 320464 6760 ? Ss Apr12 0:03 postgres: 10/main: autovacuum launcher process
postgres 1319 0.0 0.0 174988 3376 ? Ss Apr12 0:03 postgres: 10/main: stats collector process
postgres 1321 0.0 0.0 320372 4972 ? Ss Apr12 0:00 postgres: 10/main: bgworker: logical replication launcher
999 2477 0.0 0.1 273936 22668 ? Ss Apr12 0:07 postgres
999 2486 0.0 0.1 288608 24256 ? Ss Apr12 0:34 postgres
999 2519 0.0 0.1 287476 23424 ? Ss Apr12 0:08 postgres
999 2560 0.0 0.1 274540 23076 ? Ss Apr12 0:07 postgres
70 2584 0.0 0.1 189856 19624 ? Ss Apr12 0:05 postgres
999 2704 0.0 0.3 484860 40132 ? Ss Apr12 0:16 postgres
999 3173 0.0 0.2 213860 26000 ? Ss Apr12 0:13 postgres
999 3361 0.0 0.5 746652 73252 ? Ss Apr12 0:17 postgres
999 3401 0.0 0.1 288252 24500 ? Ss Apr12 0:09 postgres
999 4743 0.0 0.1 213992 19184 ? Ss Apr12 0:00 postgres: checkpointer
999 4744 0.0 0.0 213860 5760 ? Ss Apr12 0:05 postgres: background writer
999 4745 0.0 0.0 213860 10044 ? Ss Apr12 0:05 postgres: walwriter
999 4746 0.0 0.0 214536 8332 ? Ss Apr12 0:08 postgres: autovacuum launcher
999 4747 0.0 0.0 68552 5508 ? Ss Apr12 1:30 postgres: stats collector
999 4748 0.0 0.0 214284 6612 ? Ss Apr12 0:00 postgres: logical replication launcher
999 4749 0.0 0.0 287596 7804 ? Ss Apr12 0:00 postgres: checkpointer process
999 4750 0.0 0.0 287476 3756 ? Ss Apr12 0:04 postgres: writer process
999 4751 0.0 0.0 287476 8232 ? Ss Apr12 0:04 postgres: wal writer process
999 4752 0.0 0.0 287884 6480 ? Ss Apr12 0:05 postgres: autovacuum launcher process
999 4753 0.0 0.0 142608 2872 ? Ss Apr12 0:14 postgres: stats collector process
999 4762 0.0 0.1 288368 13500 ? Ss Apr12 0:00 postgres: checkpointer process
999 4763 0.0 0.0 288252 5760 ? Ss Apr12 0:05 postgres: writer process
999 4764 0.0 0.0 288252 8420 ? Ss Apr12 0:05 postgres: wal writer process
999 4765 0.0 0.0 288664 6396 ? Ss Apr12 0:06 postgres: autovacuum launcher process
999 4766 0.0 0.0 143300 3704 ? Ss Apr12 0:15 postgres: stats collector process
999 4790 0.0 0.0 288544 4520 ? Ss Apr12 0:00 postgres: bgworker: logical replication launcher
999 4793 0.0 0.0 274640 6384 ? Ss Apr12 0:00 postgres: checkpointer process
999 4794 0.0 0.0 274540 5228 ? Ss Apr12 0:02 postgres: writer process
999 4795 0.0 0.0 274540 8436 ? Ss Apr12 0:02 postgres: wal writer process
999 4796 0.0 0.0 274928 6296 ? Ss Apr12 0:05 postgres: autovacuum launcher process
999 4797 0.0 0.0 129656 2816 ? Ss Apr12 0:14 postgres: stats collector process
999 4801 0.0 0.0 274064 6248 ? Ss Apr12 0:00 postgres: checkpointer process
999 4802 0.0 0.0 273936 5064 ? Ss Apr12 0:02 postgres: writer process
999 4803 0.0 0.0 273936 8140 ? Ss Apr12 0:02 postgres: wal writer process
999 4804 0.0 0.0 274352 5776 ? Ss Apr12 0:05 postgres: autovacuum launcher process
999 4805 0.0 0.0 129216 2700 ? Ss Apr12 0:13 postgres: stats collector process
70 4816 0.0 0.0 189856 4940 ? Ss Apr12 0:00 postgres: checkpointer process
70 4817 0.0 0.0 189856 2640 ? Ss Apr12 0:05 postgres: writer process
70 4818 0.0 0.0 189856 6820 ? Ss Apr12 0:05 postgres: wal writer process
70 4819 0.0 0.0 190184 4940 ? Ss Apr12 0:05 postgres: autovacuum launcher process
70 4820 0.0 0.0 44788 2364 ? Ss Apr12 0:15 postgres: stats collector process
70 4858 0.0 0.0 190068 3644 ? Ss Apr12 0:00 postgres: bgworker: logical replication launcher
999 4872 0.0 0.4 288816 56476 ? Ss Apr12 0:02 postgres: checkpointer
999 4873 0.0 0.0 288608 8836 ? Ss Apr12 0:05 postgres: background writer
999 4874 0.0 0.0 288608 8148 ? Ss Apr12 0:08 postgres: walwriter
999 4875 0.0 0.0 289172 6880 ? Ss Apr12 0:21 postgres: autovacuum launcher
999 4876 0.0 0.0 144064 4104 ? Ss Apr12 2:01 postgres: stats collector
999 4881 0.0 0.0 288896 4252 ? Ss Apr12 0:00 postgres: logical replication launcher
999 4972 0.0 0.0 485020 5948 ? Ss Apr12 0:00 postgres: checkpointer
999 4973 0.0 0.0 485004 6320 ? Ss Apr12 0:05 postgres: background writer
999 4974 0.0 0.1 484860 13844 ? Ss Apr12 0:04 postgres: walwriter
999 4975 0.0 0.0 485464 6252 ? Ss Apr12 0:06 postgres: autovacuum launcher
999 4976 0.0 0.0 143660 2948 ? Ss Apr12 0:18 postgres: stats collector
999 4977 0.0 0.0 485276 4400 ? Ss Apr12 0:00 postgres: logical replication launcher
999 4979 0.0 0.0 747036 10456 ? Ss Apr12 0:00 postgres: checkpointer
999 4980 0.0 0.0 746912 6384 ? Ss Apr12 0:05 postgres: background writer
999 4981 0.0 0.1 746652 20856 ? Ss Apr12 0:04 postgres: walwriter
999 4982 0.0 0.0 747748 6188 ? Ss Apr12 0:03 postgres: autovacuum launcher
999 4983 0.0 0.0 143524 3020 ? Ss Apr12 0:10 postgres: stats collector
999 4984 0.0 0.0 747592 4492 ? Ss Apr12 0:00 postgres: logical replication launcher
root 54090 0.0 0.0 14428 1080 pts/0 S+ 20:11 0:00 grep --color=auto postgres
我只需要看到以下几行:
999 2477 0.0 0.1 273936 22668 ? Ss Apr12 0:07 postgres
999 2486 0.0 0.1 288608 24256 ? Ss Apr12 0:34 postgres
999 2519 0.0 0.1 287476 23424 ? Ss Apr12 0:08 postgres
999 2560 0.0 0.1 274540 23076 ? Ss Apr12 0:07 postgres
70 2584 0.0 0.1 189856 19624 ? Ss Apr12 0:05 postgres
999 2704 0.0 0.3 484860 40132 ? Ss Apr12 0:16 postgres
999 3173 0.0 0.2 213860 26000 ? Ss Apr12 0:13 postgres
999 3361 0.0 0.5 746652 73252 ? Ss Apr12 0:17 postgres
999 3401 0.0 0.1 288252 24500 ? Ss Apr12 0:09 postgres
我该怎么做?你能帮忙吗?
答案1
使用行尾锚点:
ps aux | grep 'postgres$'
此外,grepping 进程最好用 来完成pgrep
。