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).

  • 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 '18 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 '18 at 14:55

to get column, use a loop

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


  • $NF is last field
  • 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 { ... }'

Perhaps you can search Dweblogic with grep and after with sed

ps -eopid,lstart,cmd | \
grep "java.*$search" | \
sed -E 's#([^/]* )/.*('"$search"'[^ ]*).*#\1\2#'
  • 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 '18 at 4:32

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.