Kalin Borisov
Kalin Borisov

Reputation: 1120

Remove everything from output till to defined var

From ps command I want to have some var in that case "PS1" to be defined and after that everything till that var to be deleted.

ex.:

ps -ef | grep -i PS1
cbsuser 1138700       1   0   Sep 15      -  0:10 ./PS1 PS01 3

expected output:

./PS1 PS01 3

In my mind is something like which is wrong:

sed 's/^$/ "PS1"/g'

or related I'm not sure how to finish it.

Upvotes: 1

Views: 90

Answers (7)

terdon
terdon

Reputation: 3380

A few options. These all parse your command. A better approach is to use pgrep or the o option for ps but if you really want to parse:

  1. grep itself

    ps -ef | grep -Po 'PS1.*'
    
  2. perl

    ps -ef | grep -i PS1 | perl -pe 's/.*(?=PS1)//' 
    
  3. sed

    ps -ef | sed 's/.*\(PS1.*\)/\1/' 
    

Upvotes: 2

mpapec
mpapec

Reputation: 50667

ps -ef | perl -lne 'print $1 if /(\S* PS1 .*)/xi'

Upvotes: 3

sat
sat

Reputation: 14949

You can use this,

ps -eo cmd | grep 'PS1'

Upvotes: 1

fejese
fejese

Reputation: 4628

You can use custom format for ps to output only what you need so you only need to filter for the required command in the list:

ps e --format %a | grep PS1

Or even better use pgrep that is designed to do this if it's available:

pgrep -fl PS1 | cut -f2- -d' '

Upvotes: 2

anubhava
anubhava

Reputation: 785691

You can simplify with using pgrep:

 pgrep -fl PS1 | cut -d ' ' -f2-

Upvotes: 0

han058
han058

Reputation: 908

ps -ef | grep -i PS1| grep -v grep| awk '{print $(NF-2)" "$(NF-1)" "$NF}'

Upvotes: 0

Ram
Ram

Reputation: 1115

ps -ef | grep -i PS1| awk '{ for(i=9;i<=NF;++i)printf "%s ",$i }

Upvotes: 1

Related Questions