Return all output columns only if the content in the second column is positive

I am looking for a way to return a string only if the second column returns a positive number value. In the example below ...

PID Status  Label
-   0   com.apple.CoreAuthentication.daemon
937 0   com.wacom.TabletDriver.126696
-   0   com.apple.quicklook
-   0   com.apple.parentalcontrols.check
715 48  com.apple.Finder
-   0   com.apple.PackageKit.InstallStatus
-   0   com.apple.FontWorker
786 -44 com.apple.bird
-   0   com.apple.familycontrols.useragent
-   0   com.apple.aos.migrate
-   0   com.apple.universalaccessAuthWarn
839 -44 com.apple.nsurlsessiond

      

I need a command that will result in:

PID Status  Label
715 48  com.apple.Finder

      

I am pulling my hair out trying to find a way to do this through AWK, SED, GREP, RegEx, etc. I cannot get the results I want.

I've tried things like:

launchctl list | awk '{for (i=1;i<=NF;i++) if ($i>=1) print $i} '

but the results are no longer in the table and are returned in an unreadable format, and I also get positive numbers back from the PID / $ 1 column when I would only like to get results if Status / $ 2.

Starting from launchctl list | awk '{ print $2 }'

, but if I pass it through something else like GREP / SED, I obviously can't get the other missing columns as the data has already been deleted. I am not an expert and perhaps I am just missing the keystream. through | to get what I want, so I ask for any help or experience to help point me in the right direction.

+3


source to share


2 answers


You can use this awk command:

launchctl list | awk 'NR==1 || $2>0'
PID Status  Label
715 48  com.apple.Finder

      



To format it in a table use:

launchctl list | awk 'NR==1 || $2>0' | column -t
PID  Status  Label
715  48      com.apple.Finder

      

+6


source


launchctl list | sed '1b
     /^[-0-9]\{1,\}[[:blank:]]\{1,\}[-0]/d'

      



  • sed version posix
  • keep the title (even this is not a positive number in the second column)
0


source







All Articles