1747971 Members
3325 Online
108756 Solutions
New Discussion

Re: sed filtering

 
krissh89
Occasional Advisor

sed keyword

 

HI 

 

I need the below output 

 

ps -ef | grep ctm


cimsrvr-ctm  4575 4574 0 Oct 30 ? 473:33 cimservermain --executor-socket 4

 

 

Above ps output is an example.

 

I need to filter the ctm keyword alone.

 

details=`ps -ef | grep ctm | awk '{print $1}'

 

echo $details

cimsrvr-ctm

 

 

From the above output I need to filter only ctm and I don't want cimsrvr.

 

I am trying to use sed to filter the keyword but I am not able to find the correct sysntax to sed.

 

 

 

8 REPLIES 8
Steven Schweda
Honored Contributor

Re: sed keyword

   You don't really need "awk".  For example:

mba$ echo "cimsrvr-ctm  4575 4574 0 Oct 30 ? 473:33 cimservermain\
 --executor-socket 4" | sed -e 's/[^-]*-\([^ ]*\).*/\1/'
ctm

   This "sed" command looks for any number of non-hyphens followed by a
hyphen ("[^-]*-"), any number of non-spaces ("[^ ]*"), and any number of
any characters (".*").  For all that, it substitutes ("s/x/y/") the
non-spaces ("\1", the stuff between "\(" and "\)").

> I need to filter the ctm keyword alone.

   That could mean many things.  I removed the stuff up to the first
hyphen from the first space-separated token.  If you want to use "awk"
to separate the first token, then "sed" can remove everything up through
the first hyphen:

mba$ echo 'cimsrvr-ctm' | sed -e 's/[^-]*-//'
ctm

 

   This "sed" command looks for any number of non-hyphens followed by a
hyphen ("[^-]*-"), and substitutes nothing for them.

krissh89
Occasional Advisor

Re: sed keyword

Thanks for the information.

 

But I need to get the output only as ctm  in first column instead of cimsrvr-ctm when i run ps -ef | grep ctm  so that is the reason I have put awk to it.

 

 

 

 

Steven Schweda
Honored Contributor

Re: sed keyword

> [...] so that is the reason I have put awk to it.

 

   Did you look at my first example?  Did it work?  Did it use "awk"?
You can use "awk" if you wish, but I claim that it's not needed here.

Dennis Handly
Acclaimed Contributor

Re: awk filtering

>I need to get the output only as ctm  in first column instead of cimsrvr-ctm when I run ps -ef | grep ctm  so that is the reason I have put awk to it.

 

Please explain EXACTLY what you want and not how you think it should be done.  Do you want the user that has embedded "ctm" or the user exactly "ctm"?

Do you want the rest of the ps(1) output?  ps(1) has its own filtering options.

 

>   You don't really need "awk".

 

And alternate statement is that you don't need sed nor grep if you have awk.  :-)

 

If you want the list of all users that have "ctm" as the last 3 chars of the name, running processes:

UNIX95=EXTENDED_PS ps -e -ouser= | awk '/ctm/ {l = length($1); print subst($1, l-3)}' | sort -u

krissh89
Occasional Advisor

Re: sed filtering

I am trying to write a script to start a process if that process is stopped automatically.

 

Here is my firrst level script.

 

#!/bin/ksh
#
#
#
pdweb status |grep -w no > /dev/null 2>&1

if [ $? -eq 0 ];then

FailedComponents=`pdweb status |grep -w no |awk '{print $1}'`


for i in $FailedComponents; do

/etc/rc.d/init.d/ahpdweb_start.sh start $i


done

else
echo "All components are up"
fi

 

-----------------------------

When I run the FailedComponents=`pdweb status |grep -w no |awk '{print $1}'` ,  I am  getting the below ouptiut.

 

 

webseald-plaza_portal     yes       no

webseald-webseald         yes       yes

webseald-webseald1        yes       yes

webseald-webseald2        yes       yes

 

but to start the web seal process I need to input only plaza_portal and not webseald-plaza to /etc/rc.d/init.d/ahpdweb_start.sh start $i    

 

So I am trying to ignore all the keyword before "-"

Steven Schweda
Honored Contributor

Re: sed filtering

> So I am trying to ignore all the keyword before "-"

   Ok.  Again, did you look at my "sed" examples?  Did they work?

Dennis Handly
Acclaimed Contributor

Re: awk filtering

>Here is my first level script.

 

Assuming the length of the prefix is always 9:

FailedComponents=$(pdweb status | awk '$2 == "no" || $3 == "no" {print substr($1, 10)}')

if [ "$FailedComponents" != "" ]; then

   for i in $FailedComponents; do

      /etc/rc.d/init.d/ahpdweb_start.sh start $i

   done

else
   echo "All components are up"
fi

Dennis Handly
Acclaimed Contributor

Re: sed keyword

>   You don't really need "awk".

 

An alternate statement is that you don't need sed nor grep if you have awk.  :-)

 

(some how I never posted this back in Sep 20, found it my autosave drafts.)