AWK printing columns based on column number and pattern match condition

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite
1












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







share|improve this question





















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














up vote
0
down vote

favorite
1












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







share|improve this question





















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












up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





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







share|improve this question













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









share|improve this question












share|improve this question




share|improve this question








edited May 16 at 4:36
























asked May 9 at 14:18









Dexter

165




165











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
















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















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 at 14:31





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




@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 at 14:55










2 Answers
2






active

oldest

votes

















up vote
0
down vote













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





share|improve this answer




























    up vote
    0
    down vote













    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"'[^ ]*).*#12#'





    share|improve this answer





















    • 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 at 4:32










    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f442783%2fawk-printing-columns-based-on-column-number-and-pattern-match-condition%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













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





    share|improve this answer

























      up vote
      0
      down vote













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





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









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





        share|improve this answer













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






        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered May 9 at 15:04









        Archemar

        18.9k93365




        18.9k93365






















            up vote
            0
            down vote













            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"'[^ ]*).*#12#'





            share|improve this answer





















            • 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 at 4:32














            up vote
            0
            down vote













            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"'[^ ]*).*#12#'





            share|improve this answer





















            • 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 at 4:32












            up vote
            0
            down vote










            up vote
            0
            down vote









            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"'[^ ]*).*#12#'





            share|improve this answer













            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"'[^ ]*).*#12#'






            share|improve this answer













            share|improve this answer



            share|improve this answer











            answered May 10 at 15:42









            ctac_

            1,006116




            1,006116











            • 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 at 4:32
















            • 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 at 4:32















            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 at 4:32




            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 at 4:32












             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f442783%2fawk-printing-columns-based-on-column-number-and-pattern-match-condition%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)