Page 1 of 1

A question about "grep" command

Posted: Mon May 21, 2012 8:30 pm
by Herath
I am learning regular expressions these days. And I tried the following code in order to isolate the firefox process from the process list of "ps -A".

Code: Select all

ps -A|grep ^f
But it does not give any output. Or any error. I do not seem to able to figure out the problem. :?

Anyway,

Code: Select all

ps -A|grep f
would just show the names of the process of which contain "f" in their name. But can't get the ones starting with "f" using the "^f" regexp.

Any idea?.

Re: A question about "grep" command

Posted: Mon May 21, 2012 8:38 pm
by SemiconductorCat
^ - means start of a every line.

There is a ps -a option for all. But I didn't find a -A option in the man page.
Typically it prints the PID at the start of line.So that's why it won't work.


Are you want firefox process id to kill it? then

Code: Select all

$ ps -a | grep firefox-bin 

There is a column parse method using sed. But I'm not aware of it well. Somebody else will
post it for you.

Re: A question about "grep" command

Posted: Mon May 21, 2012 8:55 pm
by Herath
No I am not trying to kill firefox process or anything. I am just wanting to try that regular expression. To get the list of all items starting with "f".
scr.png
scr.png (139.07 KiB) Viewed 8115 times
And output from "ps -A"
psOutput.txt
(5.14 KiB) Downloaded 585 times

Re: A question about "grep" command

Posted: Mon May 21, 2012 9:16 pm
by SemiconductorCat
so as I said you have to take the 4th column.

I could post the code here. But I'm not aware of it.

Code: Select all

 ps -a | sed 's/\|/ /' | awk '{print $8}' | grep ps

In my cygwin.Sorry I have a assignment to do , can't reboot.
Image
as I told you before , I don't know how it work.

Re: A question about "grep" command

Posted: Mon May 21, 2012 9:20 pm
by Herath
OK. Got it. it should be due to grep looking only at line beginings. :D

sed edits the stream so awk can seperate them, you have then printed the 8th column, and grep takes it. The answer for my case would be.

Code: Select all

ps -A|sed 's/\|/ /'|awk '{print $4}'|grep ^f