Search code examples
regexbashsedps

Sed removes matches from output


I'm trying to extract the process numbers starting with the number 3.

When using ps | sed "/^\s\+3/", I get an error message : sed: -e expression #1, char 8: missing command

I then added a global flag : ps | sed "/^\s+3/g" which succeeds but instead of showing me all matches, it removes all the matches found.

This is the unchanged output :

  PID TTY          TIME CMD
 3128 pts/8    00:00:00 bash
 5279 pts/8    00:00:00 ps
 5280 pts/8    00:00:00 sed

In the end, this is the output I get : PID TTY TIME CMD

 5219 pts/8    00:00:00 ps
 5220 pts/8    00:00:00 sed

Solution

  • You need to add -n parameter to print the lines which matches a particular pattern. And note that, basic sed won't support the pattern \s (which is used for matching whitespaces).

    ps | sed -n '/^ *3/p'
    

    OR

    ps | sed -n '/^[[:blank:]]*3/p'