LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-25-2016, 10:59 AM   #1
backslashV
LQ Newbie
 
Registered: Nov 2016
Posts: 7

Rep: Reputation: Disabled
Question Question about awk


Hi,

I am printing the children of a process using this command:

Code:
ps -aef | grep 13783 | grep -v grep | grep -v ps | grep -v $0
and it shows the children correctly. (2 in my case)

The thing is that when I pipe it to awk to get the PIDs like this:

Code:
ps -aef | grep 13783 | grep -v grep | grep -v ps | grep -v $0 | awk '{print $2}'
I get 3 numbers. I don't know where that 3rd number is coming from.
 
Old 11-25-2016, 11:47 AM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
You might check that "grep 13783" looks for the string 13783 - and that may be a substring of another process id.

I did a "ps -aef | grep $$ | grep -v grep | grep -v ps" to see what this looked like - and got two processes, one mine (1245), and one from another user (11245).

You can prevent that by doing better pattern search: grep " $pid " as the spaces will reduce that issue. It doesn't prevent the resulting string from being in some other field though.

To do that would require more of the effort to be put into the awk script. Let it do the search for the parent pid ( third field), as well as being able to remove the cmd 'ps' from the selected list.

Last edited by jpollard; 11-25-2016 at 11:51 AM.
 
Old 11-25-2016, 11:53 AM   #3
backslashV
LQ Newbie
 
Registered: Nov 2016
Posts: 7

Original Poster
Rep: Reputation: Disabled
if i just do:

Code:
ps-aef | grep 13783
I get:

Code:
cs11amb  13783 13324  0 08:44 pts/0    00:00:00 bash
cs11amb  13971 13783  0 08:44 pts/0    00:00:00 vim ff
cs11amb  14000 13783  0 08:44 pts/0    00:00:00 vim ss
cs11amb  19461 13783  7 09:49 pts/0    00:00:00 ps -aef
cs11amb  19463 13783  0 09:49 pts/0    00:00:00 grep 13783
when i grep out the extras, I only get

Code:
cs11amb  13971 13783  0 08:44 pts/0    00:00:00 vim ff
cs11amb  14000 13783  0 08:44 pts/0    00:00:00 vim ss
which are the correct children and what I want.

The problem is when I use awk, it prints:

Code:
13971
14000
21253
 
Old 11-25-2016, 12:02 PM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,001
Blog Entries: 3

Rep: Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632
Is that last PID that of the "awk" process itself?

Also, with "awk" there you don't need any of the "grep" processes at all.
 
1 members found this post helpful.
Old 11-25-2016, 12:06 PM   #5
backslashV
LQ Newbie
 
Registered: Nov 2016
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
Is that last PID that of the "awk" process itself?

Also, with "awk" there you don't need any of the "grep" processes at all.
I swear to god I tried to grep out awk before but it still showed up.
It is working now. Thank youuu.

Could you be more specific when you say I don't need any of the greps?
 
Old 11-25-2016, 12:13 PM   #6
backslashV
LQ Newbie
 
Registered: Nov 2016
Posts: 7

Original Poster
Rep: Reputation: Disabled
In a script, I am trying to put the children in an array by saying:

Code:
for child in `ps -aef | grep $pid | grep -v grep | grep -v ps | grep -v $0 | grep -v awk | grep -v bash | awk '{print $2}'`; do
	arrayOfChildren+=("$child")
done
but echoing the array afterward
instead of the children PIDs as the array contents I get:

5
5

If I do
Code:
echo `ps -p ${#arrayOfChildren[0]}`
echo `ps -p ${#arrayOfChildren[1]}`
I get

PID TTY TIME CMD 5 ? 00:00:00 stopper/0
PID TTY TIME CMD 5 ? 00:00:00 stopper/0

Last edited by backslashV; 11-25-2016 at 01:21 PM.
 
Old 11-25-2016, 12:47 PM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 20,764

Rep: Reputation: 7053Reputation: 7053Reputation: 7053Reputation: 7053Reputation: 7053Reputation: 7053Reputation: 7053Reputation: 7053Reputation: 7053Reputation: 7053Reputation: 7053
why don't you try pgrep -P ?
 
Old 11-25-2016, 12:48 PM   #8
backslashV
LQ Newbie
 
Registered: Nov 2016
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
why don't you try pgrep -P ?
I am not allowed.
 
Old 11-25-2016, 01:16 PM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 20,764

Rep: Reputation: 7053Reputation: 7053Reputation: 7053Reputation: 7053Reputation: 7053Reputation: 7053Reputation: 7053Reputation: 7053Reputation: 7053Reputation: 7053Reputation: 7053
You will not be able to list all the children "in one", because all the members of your chain (grep, awk, whatever) will be children and you need to exclude them, but actually there can be other grep/awk/whatever children which should be reported but will be ignored too. The best you can do is to save the output of the command ps and process the result afterward, or use a single awk which will exclude itself and print everything else.
ps -aef | awk -vmypid=$$ { look for $3 as parent pid and also look for aw }

why aren't you allowed to use pgrep?
 
Old 11-25-2016, 01:23 PM   #10
backslashV
LQ Newbie
 
Registered: Nov 2016
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
You will not be able to list all the children "in one", because all the members of your chain (grep, awk, whatever) will be children and you need to exclude them, but actually there can be other grep/awk/whatever children which should be reported but will be ignored too. The best you can do is to save the output of the command ps and process the result afterward, or use a single awk which will exclude itself and print everything else.
ps -aef | awk -vmypid=$$ { look for $3 as parent pid and also look for aw }

why aren't you allowed to use pgrep?
I want to store the result of ps in an array.

Code:
for child in `ps -aef | grep $pid | grep -v grep | grep -v ps | grep -v $0 | grep -v awk | grep -v bash | awk '{print $2}'`; do
       arrayOfChildren+=("$child")
done
echoing the contents with ps shows:

Code:
PID TTY TIME CMD 5 ? 00:00:00 stopper/0
PID TTY TIME CMD 5 ? 00:00:00 stopper/0
so basically the contents of the array are:
5
5

but before putting into the array I echo the output of
Code:
ps -aef | grep $pid | grep -v grep | grep -v ps | grep -v $0 | grep -v awk | grep -v bash | awk '{print $2}'
and i get the correct children PIDs like this:
13432 13324

I am not sure why I am not allowed to use that. I just asked and the TA said no.
 
Old 11-25-2016, 02:35 PM   #11
backslashV
LQ Newbie
 
Registered: Nov 2016
Posts: 7

Original Poster
Rep: Reputation: Disabled
Solved

It appears that the problem was putting a # before the array name: ${#arrayOfChildren[0]}
removing that solved the problem.
Thanks everyone.
 
Old 11-25-2016, 02:54 PM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,983

Rep: Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182
Also, here is your possible all awk solution to feed your loop:
Code:
ps -aef | awk -vpid=$pid '$3 == pid && $NF != "-aef" && $NF !~ "pid"'
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] AWK question Weekend_warrior1999 Programming 10 03-07-2013 09:32 AM
[SOLVED] awk question ghantauke Linux - Newbie 7 11-15-2010 03:54 PM
awk question on handling *.CSV "text fields" in awk jschiwal Programming 8 05-27-2010 06:23 AM
an awk question zoshr Programming 5 04-13-2007 05:29 AM
question about awk perfect_circle Programming 4 07-19-2006 02:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration