开发者

selecting only required lines from unix shell prompt

开发者 https://www.devze.com 2023-02-10 18:16 出处:网络
Lets say I am running $: p开发者_开发问答s au in a shell prompt and want to select 2nd field of 5th entry in that, no matter which process it is. How do I do that ?With awk.

Lets say I am running

$: p开发者_开发问答s au

in a shell prompt and want to select 2nd field of 5th entry in that, no matter which process it is. How do I do that ?


With awk.

awk 'NR==6 { print $2 }'

The 6th record because you need to skip the header.


If you don't want to use awk or the equivalent perl or ruby commands, you can also use more low-level tools:

ps au | head -6 | tail -1 | cut -d ' ' -f 2


In "ps au" output, second field is the process ID; you can extract it directly by telling to ps what you need:

ps a -o pid=

Then you just need to output the fifth line:

ps a -o pid= | sed '5!d'
0

精彩评论

暂无评论...
验证码 换一张
取 消