Split output of command by columns using Bash?

前端 未结 10 871
遥遥无期
遥遥无期 2020-12-23 02:46

I want to do this:

  1. run a command
  2. capture the output
  3. select a line
  4. select a column of that line

Just as an example, l

10条回答
  •  不思量自难忘°
    2020-12-23 03:31

    Your command

    ps | egrep 11383 | cut -d" " -f 4
    

    misses a tr -s to squeeze spaces, as unwind explains in his answer.

    However, you maybe want to use awk, since it handles all of these actions in a single command:

    ps | awk '/11383/ {print $4}'
    

    This prints the 4th column in those lines containing 11383. If you want this to match 11383 if it appears in the beginning of the line, then you can say ps | awk '/^11383/ {print $4}'.

提交回复
热议问题