LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 10-05-2012, 03:15 PM   #1
j-me
Member
 
Registered: Jan 2003
Location: des moines, ia
Distribution: suse RH
Posts: 129

Rep: Reputation: 17
command string in awk


is it possible to put a command string in awk using printf?

here is what I want to accomplish ... take this command:
Code:
ps -AH v | grep $1 | tail -1 | echo `expr $7 / 1024`
and add into this awk statement:
Code:
for line in `cat /tmp/pids`
do
  awk ' {if(index(FILENAME,"domain")!=0){split(FILENAME,a,"/");app=a["6"];printf "%25s %5d %10d\n", app" ", $1"  ", $1 }};
        {if(index(FILENAME,"nodeagent")!=0){split(FILENAME,a,"/");app=a["7"];printf "%25s %5d %10d\n", app" ", $1"  ", $1}} ' $line
done
What this script performs currently is display the jvm name and pid. I want to add the jvm mem allocated. btw, the command works outside of the script when I input a valid pid.

Thank you.
 
Old 10-06-2012, 08:36 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
You can use the system function; the syntax is
Code:
system(expression)
For example, to cat a file where the file name is the second argument on the command line
Code:
system("cat " $2)
Hope this helps some.
 
Old 10-07-2012, 06:45 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I have to ask what you think you're doing with this though.
Code:
ps -AH v | grep $1 | tail -1 | echo `expr $7 / 1024`
To start with, a single awk or sed would be much better than grep+tail.

But then you're piping the output into echo, which is a command that doesn't read from stdin.

Finally, there's a pointless call to expr, when the shell has perfectly good integer math built right into it. (And $(..) is highly recommended over `..`, too.)


Oh, and by the way:
Code:
for line in `cat /tmp/pids`
Don't Read Lines With For! Always use a while+read loop when working with the output of files and commands.

Code:
while read -r line; do
...
done </tmp/pids
 
Old 10-08-2012, 08:55 AM   #4
j-me
Member
 
Registered: Jan 2003
Location: des moines, ia
Distribution: suse RH
Posts: 129

Original Poster
Rep: Reputation: 17
I'll try the system command and see what the result is [thank you tronayne].

I would try the awk/sed line if I knew what it would look like. David, I appreciate what you are saying but if you are going to correct someone who is asking/learning this, might I suggest providing the proper syntax for what you are explaining. I created the for loop many years ago and have had not an issue with it's performance. I would be willing to re-write this in the future with the while loop as I understand why. What I need is the
Code:
ps -AH v | grep $1 | tail -1 | echo `expr $7 / 1024`
at the moment working with the existing script due to time constraints. If you have the knowledge of writing this in awk, please provide so I can test with my existing awk statement and implement this. I appreciate your input and knowledge sharing.
regards.
 
Old 10-08-2012, 09:35 AM   #5
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Actually, you can probably accomplish what you what with an AWK one-liner; e.g.,
Code:
ps -AH v | tail -1 | awk '{ printf ("%-45s %d\n", $10, $7/1024) }'
The addition of the format string ("%-45s %d\n") makes the output "look nicer;" not necessary but a little easier to read. Plus you do the arithmetic in the printf.

I suspect you're running this in a loop of some sort with a sleep? Sorta like
Code:
while true
do
     ps -AH v | tail -1 | awk '{ printf ("%-45s %d\n", $10, $7/1024) }'
     sleep 10
done
and you let it run for a while and watch it then abort with Ctrl-C?

Hope this helps some.
 
Old 10-08-2012, 11:09 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Quote:
Originally Posted by j-me View Post
I would try the awk/sed line if I knew what it would look like. David, I appreciate what you are saying but if you are going to correct someone who is asking/learning this, might I suggest providing the proper syntax for what you are explaining.
I didn't provide a solution because I'm not sure what you are trying to do exactly. The error of trying to pipe a command into echo is particularly confusing.

Quote:
I created the for loop many years ago and have had not an issue with it's performance. I would be willing to re-write this in the future with the while loop as I understand why.
Using a for loop like that is not actually a problem as long as the command produces output in individual "words", and doesn't include word-breaking whitespace or globbing patterns that could expand. But it's never a bad idea to get in the habit of doing it correctly, even when unnecessary. Better safe than sorry.
 
Old 10-08-2012, 11:28 AM   #7
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
Quote:
Originally Posted by tronayne View Post
You can use the system function
With system the output won’t go to the awk script again. But:
Code:
command | getline
would do, e.g. to get the first output:
Code:
$ awk 'BEGIN { "ls" | getline NAME; print NAME}'
 
Old 10-08-2012, 11:47 AM   #8
j-me
Member
 
Registered: Jan 2003
Location: des moines, ia
Distribution: suse RH
Posts: 129

Original Poster
Rep: Reputation: 17
okay, understand this is confusing to explain so I'll try better.

I have this code that works great and has for several years. I want to ADD the JVM memory to that which is displayed to the console.
Current code:
Code:
#!/bin/bash

clear
sudo find /opt/SUN/SUNAppSrv -name .__com* | grep domain > /tmp/pids
sudo find /opt/SUN/SUNAppSrv -name .__com* | grep nodeagent >> /tmp/pids
printf "      Java Name\t\t   PID\n------------------------ --------\n"

while read -r line; do
  awk ' {if(index(FILENAME,"domain")!=0){split(FILENAME,a,"/");app=a["6"];printf "%25s %5d", app" ", $1"  ", $1 }};
        {if(index(FILENAME,"nodeagent")!=0){split(FILENAME,a,"/");app=a["7"];printf "%25s %5d", app" ", $1"  ", $1}} ' $line
done < /tmp/pids
As you will notice, I have made it while loop NOT for loop per suggestion.

output looks like:
Code:
      Java Name            PID
------------------------ --------
           PCQAProdSup01   8207
                   agent   8361
                     TRP   9011
                     ALU   9329
                     MCA   9476
                     SLR   9361
   AutomatedUnderwriting   6359
           AURulesEngine   5718
   AUManagementReporting   6973
          AUTrapAnalyzer   9340
                     AWS   9244
  PropertyCasualtyClaims   9612
   SubrogationAssignment   9305
                AUGuvnor   9573
What I want to ADD is a column for MEM:
Code:
      Java Name            PID       MEM
------------------------ --------   ------
I found I could obtain the jvm memory allocated using the command:
Code:
 ps -AH v | grep 29742 | tail -1 | echo `expr $7 / 1024`
How would I incorporate this in the EXISTING awk script to display in the proper column/line location corresponding to the appropriate JVM???
So far I have:
Code:
#!/bin/bash

### this command will provide the memory the JVM[pid] is currently allocatted
### ps -AH v | grep 29742 | tail -1 | echo `expr $7 / 1024`

clear
sudo find /opt/SUN/SUNAppSrv -name .__com* | grep domain > /tmp/pids
sudo find /opt/SUN/SUNAppSrv -name .__com* | grep nodeagent >> /tmp/pids
printf "      Java Name\t\t   PID       MEM\n------------------------ --------   ------\n"

while read -r line; do
  awk ' {if(index(FILENAME,"domain")!=0){split(FILENAME,a,"/");app=a["6"];printf "%25s %5d %10d\n", app" ", $1"  ", $1 }};
        {if(index(FILENAME,"nodeagent")!=0){split(FILENAME,a,"/");app=a["7"];printf "%25s %5d %10d\n", app" ", $1"  ", $1 }} ' $line
done < /tmp/pids
what I am not sure of is how/where to put the command to obtain the memory allocated to the JVM. Can I even perform this inside the awk statement or does the value need to be obtained outside the awk and how is that done then printed in the appropriate line/column within the awk statement.

right now the output looks like this:
Code:
      Java Name            PID       MEM
------------------------ --------   ------
           PCQAProdSup01   8207       8207
                   agent   8361       8361
                     TRP   9011       9011
                     ALU   9329       9329
                     MCA   9476       9476
                     SLR   9361       9361
   AutomatedUnderwriting   6359       6359
           AURulesEngine   5718       5718
   AUManagementReporting   6973       6973
          AUTrapAnalyzer   9340       9340
                     AWS   9244       9244
  PropertyCasualtyClaims   9612       9612
   SubrogationAssignment   9305       9305
                AUGuvnor   9573       9573
Duplicating the obtained PID information ...

I hope this explains the current issue more plainly.
 
Old 10-08-2012, 02:28 PM   #9
j-me
Member
 
Registered: Jan 2003
Location: des moines, ia
Distribution: suse RH
Posts: 129

Original Poster
Rep: Reputation: 17
not pretty but it is functional and provides what I was needing ...
Code:
#!/bin/bash

### this command will provide the memory the JVM[pid] is currently allocatted
### ps -AH v | grep 29742 | tail -1 | echo `expr $7 / 1024`


clear
sudo find /opt/SUN/SUNAppSrv -name .__com* | grep domain > /tmp/pids
sudo find /opt/SUN/SUNAppSrv -name .__com* | grep nodeagent >> /tmp/pids
printf "      Java Name\t\t   PID       MEM[MB]\n------------------------ --------   -------\n"

while read -r line; do

 jvm_pid=`cat $line`

  set $(ps -AH v | grep $jvm_pid | tail -1)

  jvm_mem=`expr $7 / 1024`


  awk -v mem=$jvm_mem ' {if(index(FILENAME,"domain")!=0){split(FILENAME,a,"/");app=a["6"];printf "%25s %5d %10d\n", app" ", $1"  ", mem }};
        {if(index(FILENAME,"nodeagent")!=0){split(FILENAME,a,"/");app=a["7"];printf "%25s %5d %10d\n", app" ", $1"  ", mem }} ' $line
done < /tmp/pids
OUTPUT:
Code:
      Java Name            PID       MEM[MB]
------------------------ --------   -------
           PCQAProdSup01   8207        986
                   agent   8361       1252
                     TRP   9011        857
                     ALU   9329        843
                     MCA   9476        854
                     SLR   9361        875
   AutomatedUnderwriting   6359        854
           AURulesEngine   5718        865
   AUManagementReporting   6973        850
          AUTrapAnalyzer   9340        854
                     AWS   9244        887
  PropertyCasualtyClaims   9612        854
   SubrogationAssignment   9305        879
                AUGuvnor   9573        880
thank you for your input/advice. I'm sure it can be written more efficiently/effective but it works in my pinch at the moment. If you feel compelled to improve on it, then by all means please advance this.
 
  


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] Sed/awk/cut to pull a repeating string out of a longer string StupidNewbie Programming 4 09-13-2018 03:41 AM
linux grep or awk command - how to search for string + execute couta Linux - Newbie 10 09-03-2011 01:58 AM
tokenizing string using awk girish_hilage Linux - General 3 05-29-2011 04:40 AM
awk gsub() command - string (column) manipulation - substitution casperdaghost Linux - Newbie 1 03-08-2010 02:12 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 05:49 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
Open Source Consulting | Domain Registration