LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-13-2010, 04:39 AM   #1
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
Question search a string and print column


Hi All,

I need to search a string which is in the first line and print that column.

For e.g
Code:
$ top -bn1 | head -20 | awk '/PID/,/*/' 
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                                               
13737 admin     15   0  2476  936  680 R    2  0.0   0:00.01 top                                                                                                                   
    1 root      16   0  3116  548  468 S    0  0.0   3:46.43 init                                                                                                                  
    2 root      RT   0     0    0    0 S    0  0.0   1:21.03 migration/0                                                                                                           
    3 root      34  19     0    0    0 S    0  0.0   0:06.24 ksoftirqd/0                                                                                                           
    4 root      RT   0     0    0    0 S    0  0.0   0:39.39 migration/1                                                                                                           
    5 root      34  19     0    0    0 S    0  0.0   0:04.85 ksoftirqd/1                                                                                                           
    6 root      RT   0     0    0    0 S    0  0.0   0:45.57 migration/2                                                                                                           
    7 root      34  19     0    0    0 S    0  0.0   0:02.77 ksoftirqd/2                                                                                                           
    8 root      RT   0     0    0    0 S    0  0.0   0:28.31 migration/3                                                                                                           
    9 root      34  19     0    0    0 S    0  0.0   0:02.16 ksoftirqd/3                                                                                                           
   10 root       5 -10     0    0    0 S    0  0.0   0:00.10 events/0                                                                                                              
   11 root       5 -10     0    0    0 S    0  0.0   0:00.11 events/1                                                                                                              
   12 root       5 -10     0    0    0 S    0  0.0   0:00.06 events/2
Here, I need to search "MEM" and print the column.
Code:
%MEM
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
Any suggestions.
 
Old 03-13-2010, 04:52 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
top -bn1 |awk 'NR>5&&NF{print $10}'
 
Old 03-13-2010, 05:43 AM   #3
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Original Poster
Rep: Reputation: 107Reputation: 107
Question

Quote:
Originally Posted by ghostdog74 View Post
Code:
top -bn1 |awk 'NR>5&&NF{print $10}'
Hi Ghostdog,

Thanks for you reply. Actually, I am making a health check script for Linux Boxes, a part of which needs to print MEM column. But the problem is that on one machine MEM is in 7th column and in the other machine MEM is in the 10th column.

Code:
 MACHINE 1
$ top -bn1 | head -20 | awk '/PID/,/*/' | head -5
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                                               
20524 smsc      16   0 1718m 592m 1064 S    2  9.8   3:08.84 sigserver                                                                                                             
20525 smsc      15   0  1592  604  508 S    2  0.0   1:00.13 OamLogServer                                                                                                          
    1 root      16   0  2148  548  468 S    0  0.0   2:19.38 init                                                                                                                  
    2 root      RT   0     0    0    0 S    0  0.0  11:22.97 migration/0  


MACHINE 2
-sh-3.00$ top -bn1 | head -20 | awk '/PID/,/*/' | head -5
  PID USER      PR  NI %CPU    TIME+  %MEM  VIRT  RES  SHR S COMMAND                                                                                                               
 4421 sanjeev.  25   0 16.8   1572:53  0.1  5116 1188  872 R dialog                                                                                                                
 4678 sanjeev.  25   0 15.3   1572:13  0.1  5156 1188  872 R dialog                                                                                                                
10604 sanjeev.  25   0 15.3   2002:44  0.1  5356 1184  872 R dialog                                                                                                                
29118 sanjeev.  25   0 15.3   3834:31  0.0  5732  924  612 R dialog
Thus, I am looking a way out so that I can search MEM in the output and print that column.
I hope I made my point clear.
 
Old 03-13-2010, 07:36 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
top -bn1 |awk '/MEM/{for(i=1;i<=NF;i++)if($i~/MEM/){v=i}}'
Code:
/MEM/ -> search for "MEM"
for(i=1;i<=NF;i++) -> for each field, 
    if($i~/MEM/) --> if "MEM" is found
        {v=i} ===> put the position "i" into variable "v". this is the field number where "MEM" is
     NR>5&&NF{print $(v)} --> print the field denoted by "v" from record 6 onwards.

Last edited by ghostdog74; 03-14-2010 at 12:43 AM.
 
1 members found this post helpful.
Old 03-13-2010, 01:31 PM   #5
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Original Poster
Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by ghostdog74 View Post
Code:
top -bn1 |awk '/MEM/{for(i=1;i<=NF;i++)if($i~/MEM/){v=i}}NR>5&&NF{print $(v)}'

Hi,

This works perfect . Could you please explain this code also.
It will help me in future. Thanks a ton for the help.
 
Old 03-14-2010, 12:43 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by vikas027 View Post
Hi,

This works perfect . Could you please explain this code also.
It will help me in future. Thanks a ton for the help.
no problem. see my edit.
 
Old 03-14-2010, 10:20 AM   #7
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Original Poster
Rep: Reputation: 107Reputation: 107
Thumbs up

Quote:
Originally Posted by ghostdog74 View Post
no problem. see my edit.
Thanks a ton !

Will keep this in mind.
 
  


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
awk gsub() command - string (column) manipulation - substitution casperdaghost Linux - Newbie 1 03-08-2010 03:12 AM
[Help me, plz!!] Compare string in the column in the file... nightmare520 Linux - Newbie 1 12-04-2009 07:40 AM
search string in text and print string wolfipa Linux - Software 4 09-17-2009 09:50 AM
Can you make search ...search a string in a link....a url...a web address aus9 LQ Suggestions & Feedback 4 04-16-2008 10:37 AM
MYSQL search query by column name using wildcard AQG Programming 9 08-07-2007 10:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:33 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration