Parsing ps and grep output in shell

bash, cgi, grep, linux, shell

Solution

You can use `awk` for both filtering and parsing:

ps -ef | awk '/[p]ort/ {printf "start time: %s\nsn: %s\nsku: %s\nport: %s\n", $5, $11, $13, $NF}'

As glenn jackman pointed out in the comments the square brackets in the filter string prevent the expression from matching the filter string itself in the process list.

Problem

I get the following message when I do a "ps -ef | grep port" ``` apache 6215 1 0 11:20 ? 00:00:00 perl /scripts/myscript.pl -sn 4123E -sku HSME01-HW -port 8 ``` Is there a way to parse the following: - start time (11:20) - sn (4123E) - sku (HSME01-HW) - port (8)

Original source