A question about "grep" command

Linux OS Topics
Post Reply
User avatar
Herath
Major
Major
Posts: 417
Joined: Thu Aug 05, 2010 7:09 pm

A question about "grep" command

Post by Herath » Mon May 21, 2012 8:30 pm

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?.
User avatar
SemiconductorCat
Major
Major
Posts: 455
Joined: Mon Aug 22, 2011 8:42 pm
Location: currently in hyperspace

Re: A question about "grep" command

Post by SemiconductorCat » Mon May 21, 2012 8:38 pm

^ - 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.
User avatar
Herath
Major
Major
Posts: 417
Joined: Thu Aug 05, 2010 7:09 pm

Re: A question about "grep" command

Post by Herath » Mon May 21, 2012 8:55 pm

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 6615 times
And output from "ps -A"
psOutput.txt
(5.14 KiB) Downloaded 377 times
User avatar
SemiconductorCat
Major
Major
Posts: 455
Joined: Mon Aug 22, 2011 8:42 pm
Location: currently in hyperspace

Re: A question about "grep" command

Post by SemiconductorCat » Mon May 21, 2012 9:16 pm

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.
User avatar
Herath
Major
Major
Posts: 417
Joined: Thu Aug 05, 2010 7:09 pm

Re: A question about "grep" command

Post by Herath » Mon May 21, 2012 9:20 pm

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
Post Reply

Return to “Linux”