0

I need to reformat the output of the following command using awk for static and dynamic column numbers:

 ps -eopid,lstart,cmd | grep java |grep -v grep

following would be sample input to the awk command:

17524 Wed May  9 08:50:37 2018 /opt/java/latest/bin/java -client -Xms256m -Xmx512m -XX:CompileThreshold=8000 -XX:PermSize=128m -XX:MaxPermSize=256m -Dweblogic.Name=AdminServer -Djava.security.policy=/app/oracle/wls1036/wlserver_10.3/server/lib/weblogic.policy -Dweblogic.system.BootIdentityFile=/projects/domainName/servers/AdminServer/security/boot.properties -Dweblogic.nodemanager.ServiceEnabled=true -Xverify:none -da -Dplatform.home=/app/oracle/wls1036/wlserver_10.3 -Dwls.home=/app/oracle/wls1036/wlserver_10.3/server -Dweblogic.home=/app/oracle/wls1036/wlserver_10.3/server -Ddomain.home=/projects/domainName -Does.client.home=/app/oracle/wls1036/oesclient -Doracle.home=/app/oracle/wls1036/oesclient -Doracle.security.jps.config=/projects/mydomain/config/oeswlssmconfig/AdminServer/jps-config.xml -Dweblogic.management.discover=true -Dwlw.iterativeDev= -Dwlw.testConsole= -Dwlw.logErrorsToConsole= -Dweblogic.ext.dirs=/app/oracle/wls1036/patch_wls1036/profiles/default/sysext_manifest_classpath:/app/oracle/wls1036/patch_ocp371/profiles/default/sysext_manifest_classpath weblogic.Server

Sample Output:

24519 Wed May 9 23:50:09 2018 -Dweblogic.Name=AdminServer

Issue: I am able to print the value of initial PID, Startup date and time based on the column number using awk but the last value's column number can be different in cases(eg: column9 in one output and column 17 in another). How can i print the last value searching corresponding column number containing keyword "-Dweblogic.Name=" and add to the existing output. Combining output for column numbers and column search is throwing exception. Any easier way to format this output is welcome too using (sed,grep,cut etc).

2
  • The last column in awk is $NF. Other than that, I cannot tell what you want to do because your sample input bears no relation to your sample output.
    – NickD
    May 9, 2018 at 14:31
  • @Nick the output is based on fixed columns ( 24519 Wed May 9 23:50:09 2018) and a not fixed column containing keyword "-Dweblogic.Name=" . i.e (-Dweblogic.Name=AdminServer)
    – Dexter
    May 9, 2018 at 14:55

3 Answers 3

0

Perhaps you can search Dweblogic with grep and after with sed

search="-Dweblogic.Name="
ps -eopid,lstart,cmd | \
grep "java.*$search" | \
sed -E 's#([^/]* )/.*('"$search"'[^ ]*).*#\1\2#'
1
  • doesn't seem to work when i use it in ssh script block.Is there any escape sequence to help it work. > ssh -q -o "StrictHostKeyChecking no" -t myuserid@$ip << EOF
    – Dexter
    May 16, 2018 at 4:32
0

to get column, use a loop

| awk '{for(i=NF;i>1;i--) 
     if ( $i ~ /-Dweblogic.name/ ) { wln=$i; break } ;
     printf "... %s ...",wln}'

where

  • NF is represent the number of fields
  • for( ) construct will loop from end of line to begin
  • $i ~ /-Dweblogic.name/ match -Dweblogic.name=admin1 , -Dweblogic.name=otheradmin (you may want /^-D/ to not match yourself)

may I sugest you drop grep java | grep -v grep to grep [j]ava witch will not grep itslef (and amaze coworker), or

| awk '/java/ {...} '

or, to have a mainimal number of arg :

| awk '/java/ && NF>10 { ... }'
0

Using Raku (formerly known as Perl_6)

raku -ne 'my @a = .words; put "@a[0..5] @a.grep(/^ \-Dweblogic\.Name\= /)" if .words > 6;' 

Sample Input:

17524 Wed May  9 08:50:37 2018 /opt/java/latest/bin/java -client -Xms256m -Xmx512m -XX:CompileThreshold=8000 -XX:PermSize=128m -XX:MaxPermSize=256m -Dweblogic.Name=AdminServer -Djava.security.policy=/app/oracle/wls1036/wlserver_10.3/server/lib/weblogic.policy -Dweblogic.system.BootIdentityFile=/projects/domainName/servers/AdminServer/security/boot.properties -Dweblogic.nodemanager.ServiceEnabled=true -Xverify:none -da -Dplatform.home=/app/oracle/wls1036/wlserver_10.3 -Dwls.home=/app/oracle/wls1036/wlserver_10.3/server -Dweblogic.home=/app/oracle/wls1036/wlserver_10.3/server -Ddomain.home=/projects/domainName -Does.client.home=/app/oracle/wls1036/oesclient -Doracle.home=/app/oracle/wls1036/oesclient -Doracle.security.jps.config=/projects/mydomain/config/oeswlssmconfig/AdminServer/jps-config.xml -Dweblogic.management.discover=true -Dwlw.iterativeDev= -Dwlw.testConsole= -Dwlw.logErrorsToConsole= -Dweblogic.ext.dirs=/app/oracle/wls1036/patch_wls1036/profiles/default/sysext_manifest_classpath:/app/oracle/wls1036/patch_ocp371/profiles/default/sysext_manifest_classpath weblogic.Server

Sample Output:

17524 Wed May 9 08:50:37 2018 -Dweblogic.Name=AdminServer

Briefly, -ne linewise input is broken into whitespace-separated words, and assigned to @a array. Then the initial 6 columns (words [0..5]) are output, followed by grepping for full words that match the /^ \-Dweblogic\.Name\= / regex (also can be written: /^ "-Dweblogic.Name=" / ).

The code above handles short/blank lines in the input, however it prints the first 6 columns (PID and timestamp) even if /^ \-Dweblogic\.Name\= / isn't found. Much better to use the code below, which overcomes this issue using Raku's $_ topic variable:

raku -ne 'my @a = .words; $_ = @a.grep(/^ \-Dweblogic\.Name\= /); put "@a[0..5] $_" if $_;' 

https://raku.org

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.