Operating System - HP-UX
1836741 Members
2751 Online
110109 Solutions
New Discussion

awk and special character

 
SOLVED
Go to solution
suki
Frequent Advisor

awk and special character

Hi,
How can I get the list of processes which are not associated with any terminal device by using awk?( The processes with TTY column "?" when you execute ps -ef)
TIA.
-Suki

 

 

P.S. this thread has been moved from HP-UX > System Administration to HP-UX > languages - HP Forums moderator

6 REPLIES 6
Patrick Wallek
Honored Contributor

Re: awk and special character

How about just:

# ps -ef | grep " ? "

NOTE: There are supposed to be 2 spaces between (before and after) the " characters and the ? character.

Please be careful when doing this though. There are a LOT of processes that are started by root when the system starts that have a ? in the tty column of 'ps' output. If you kill these you could potentially harm your system.
suki
Frequent Advisor

Re: awk and special character

Hi Patrick,
It sounds good but if there is a file named "abc ? cde" and somebody is using that file I will be in trouble when I run the script.
If we use awk and take the value of TTY column it will be accurate and I won't be in trouble.Can u please help me.
Thanks in Advance,
-Suki
S.K. Chan
Honored Contributor

Re: awk and special character

You can simplify it by using "ps -e" which makes it easier to extract. The downside is it does not show the full path of the process. For example..
$ ps -e|sed 's/^[ \t]*//'|grep -v \^PID|awk '{print $2, $4}'|grep \^?
The sed statement removes leading spaces, the first grep filters out line starting with "PID" (ie the header), the awk prints the tty and the command and the last grep only shows line starting with "?".
Balaji N
Honored Contributor
Solution

Re: awk and special character

hi,
here is a snippet. does this help. (the if loops are to check if ? appears in the sizth coulumn (if the time is in hh:mm:ss format) or in the seventh column if the time is in mmm dd format)

ps -ef | awk '{ if ($5~/:/ && $6~/\?/ ) print; else if ($7~/\?/ ) print }'


hth
-balaji
Its Always Important To Know, What People Think Of You. Then, Of Course, You Surprise Them By Giving More.
suki
Frequent Advisor

Re: awk and special character

Thanks to all who responded . Balaji has given the script what I was searching. Thanks Balaji.

-Suki
David Totsch
Valued Contributor

Re: awk and special character

ps(1) supports the -t option to which you give a tty as argument. Merely supply "?".

-dlt-