LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 07-16-2013, 08:07 AM   #1
Madhu Desai
Member
 
Registered: Mar 2013
Distribution: Rocky, Fedora, Ubuntu
Posts: 541

Rep: Reputation: 153Reputation: 153
Is there any builtin option to show title and searched option?


Hi

I’m wondering is there any builtin command or trick to display grep-ed result along with title. Many a times, when using grep on different commands, which have long listing, it becomes difficult to figure-out what each column mean without looking at title header, especially when the command is not frequently used, and you kinda get lost looking at the results.

Although I use sed to achieve that, just curious what others usually do to achieve same result.

For example:
Code:
$ ps aux | grep vsftpd
root      2788  0.0  0.0  52104   744 ?        Ss   10:50   0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
madhu     9000  0.0  0.0 103248   916 pts/0    S+   18:27   0:00 grep --color=auto vsftpd
I use sed (in this example for ps)
Code:
$ ps aux | sed -ne '1p; /vsftpd/p'
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root      2788  0.0  0.0  52104   744 ?        Ss   10:50   0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
madhu     9014  0.0  0.0 105252   840 pts/0    S+   18:29   0:00 sed -ne 1p; /vsftpd/p
Thanks
 
Old 07-16-2013, 08:35 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Most people use ps in its simpler form (ps -ef / ps aux) and use (e)grep to get the details they want.

ps has however a ton of options that can be used to influence what is shown and how this info is shown.

This, for example, is the ps ... | sed .... shown above by only using ps:
Code:
ps -C vsftpd -o user,pid,%cpu,%mem,vsize,rss,tty,stat,start_time,time,cmd
The -c name specifies the process that you want to see and the -o ...... line determines what you want to see about this process.

Have a look at the man page (STANDARD FORMAT SPECIFIERS) to get a list of what it is you can specify to see.

Last edited by druuna; 07-16-2013 at 08:41 AM. Reason: simplest -> simpler
 
1 members found this post helpful.
Old 07-16-2013, 08:37 AM   #3
shm0
Member
 
Registered: Aug 2012
Location: Bahrain
Distribution: Slackware
Posts: 58

Rep: Reputation: 16
Just do this:

Code:
echo "USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND" ; ps aux | grep <procname>
 
Old 07-16-2013, 09:09 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Just in case: If the ps stuff isn't your thing, this is the best I can come up with using a pipe:
Code:
ps aux | egrep "^USER|[v]sftpd"
 
2 members found this post helpful.
Old 07-16-2013, 09:25 AM   #5
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Interesting question! Well, for those who are used to the ps output, don't need it's header since they're aware about the various fields.

In the meantime, if you're printing all fields of ps output, then I would go with what suggested in post #3. Otherwise if you're printing only certain fields, then I will go with druuna's solution suggested in post #2, since in that case you will be knowing that what fields you're going to print. Also both solutions can be combined to get desired results. You can also give try to:
Code:
~$ ps aux | awk 'NR<2' ; ps aux | grep vsftpd
 
Old 07-17-2013, 07:21 AM   #6
Madhu Desai
Member
 
Registered: Mar 2013
Distribution: Rocky, Fedora, Ubuntu
Posts: 541

Original Poster
Rep: Reputation: 153Reputation: 153
Thanks all, especially druuna for showing '-C' and '-o' options.

Cheers!!!
 
Old 07-17-2013, 07:29 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
You're welcome
 
  


Reply



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] unetbootin in ubuntu doesnt show persistent option Fcukinyahoo Linux - Newbie 1 11-16-2011 01:00 PM
[SOLVED] Where's the option to change your user title? MTK358 LQ Suggestions & Feedback 2 05-19-2011 11:16 AM
RHES 4 new PPD file does not show up as a driver option through printconf. Nahmus Red Hat 1 04-12-2007 02:54 AM
How to disable the option to show the description for a shortcut? zahadumy Linux - Software 3 11-28-2006 04:30 AM
Acidrip does not show xvid as an option nkoplm Linux - Software 2 02-26-2006 11:12 AM

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

All times are GMT -5. The time now is 05:56 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