Get Substring assigned to a variable

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











up vote
3
down vote

favorite












bash-4.1$ ps -ef|grep component_events.xml
gdxx1122 1190 1181 0 Mar25 ? 00:00:00 /xx1122/fmw/product/111/ohs_111/ohs/bin/odl_rotatelogs -l -h:/xx1122/admin/xx1122_vmabcd123/config/OHS/ohs1/component_events.xml_ohs1
/xx1122/admin/xx1122_vmabcd123/auditlogs/OHS/ohs1/audit-pid1181-%Y%m%d%H%M%S.log 1M 4M
gdxx1122 19885 19205 0 08:05 pts/1 00:00:00 grep component_events.xml


When i run the above command in a script, can extract "/xx1122/admin/xx1122_vmabcd123/config/OHS/ohs1" and assigned to a variable. The output of above may vary server to server, so it has to be dynamic. Starting string xx1122 has to be passed dynamically







share|improve this question





















  • you want the whole string "/xx1122/fmw/product/111/ohs_111/ohs/bin/odl_rotatelogs" or it must be trim after 'ohs1' ? If must be trim, 'ohs1' is constant?
    – lese
    Jun 15 at 7:37










  • You want the logging directory? Is there no better way of getting than than from the ps output?
    – Kusalananda
    Jun 15 at 7:41










  • I am looking for the directory where the file "component_events.xml" is found .. the reason i am taking it through ps is .. the same file is found in back up locations as well
    – Gayathri Gowda
    Jun 15 at 8:55










  • Do you know beforehand what the backup directories are called? You could use locate together with grep. That would work even if the process isn't running.
    – Kusalananda
    Jun 15 at 13:48










  • i will not know because this script is going to run on many servers. so it will be quite difficult to find the back up directories. That is the reason i am trying to find the file associated with the process
    – Gayathri Gowda
    Jun 15 at 17:22














up vote
3
down vote

favorite












bash-4.1$ ps -ef|grep component_events.xml
gdxx1122 1190 1181 0 Mar25 ? 00:00:00 /xx1122/fmw/product/111/ohs_111/ohs/bin/odl_rotatelogs -l -h:/xx1122/admin/xx1122_vmabcd123/config/OHS/ohs1/component_events.xml_ohs1
/xx1122/admin/xx1122_vmabcd123/auditlogs/OHS/ohs1/audit-pid1181-%Y%m%d%H%M%S.log 1M 4M
gdxx1122 19885 19205 0 08:05 pts/1 00:00:00 grep component_events.xml


When i run the above command in a script, can extract "/xx1122/admin/xx1122_vmabcd123/config/OHS/ohs1" and assigned to a variable. The output of above may vary server to server, so it has to be dynamic. Starting string xx1122 has to be passed dynamically







share|improve this question





















  • you want the whole string "/xx1122/fmw/product/111/ohs_111/ohs/bin/odl_rotatelogs" or it must be trim after 'ohs1' ? If must be trim, 'ohs1' is constant?
    – lese
    Jun 15 at 7:37










  • You want the logging directory? Is there no better way of getting than than from the ps output?
    – Kusalananda
    Jun 15 at 7:41










  • I am looking for the directory where the file "component_events.xml" is found .. the reason i am taking it through ps is .. the same file is found in back up locations as well
    – Gayathri Gowda
    Jun 15 at 8:55










  • Do you know beforehand what the backup directories are called? You could use locate together with grep. That would work even if the process isn't running.
    – Kusalananda
    Jun 15 at 13:48










  • i will not know because this script is going to run on many servers. so it will be quite difficult to find the back up directories. That is the reason i am trying to find the file associated with the process
    – Gayathri Gowda
    Jun 15 at 17:22












up vote
3
down vote

favorite









up vote
3
down vote

favorite











bash-4.1$ ps -ef|grep component_events.xml
gdxx1122 1190 1181 0 Mar25 ? 00:00:00 /xx1122/fmw/product/111/ohs_111/ohs/bin/odl_rotatelogs -l -h:/xx1122/admin/xx1122_vmabcd123/config/OHS/ohs1/component_events.xml_ohs1
/xx1122/admin/xx1122_vmabcd123/auditlogs/OHS/ohs1/audit-pid1181-%Y%m%d%H%M%S.log 1M 4M
gdxx1122 19885 19205 0 08:05 pts/1 00:00:00 grep component_events.xml


When i run the above command in a script, can extract "/xx1122/admin/xx1122_vmabcd123/config/OHS/ohs1" and assigned to a variable. The output of above may vary server to server, so it has to be dynamic. Starting string xx1122 has to be passed dynamically







share|improve this question













bash-4.1$ ps -ef|grep component_events.xml
gdxx1122 1190 1181 0 Mar25 ? 00:00:00 /xx1122/fmw/product/111/ohs_111/ohs/bin/odl_rotatelogs -l -h:/xx1122/admin/xx1122_vmabcd123/config/OHS/ohs1/component_events.xml_ohs1
/xx1122/admin/xx1122_vmabcd123/auditlogs/OHS/ohs1/audit-pid1181-%Y%m%d%H%M%S.log 1M 4M
gdxx1122 19885 19205 0 08:05 pts/1 00:00:00 grep component_events.xml


When i run the above command in a script, can extract "/xx1122/admin/xx1122_vmabcd123/config/OHS/ohs1" and assigned to a variable. The output of above may vary server to server, so it has to be dynamic. Starting string xx1122 has to be passed dynamically









share|improve this question












share|improve this question




share|improve this question








edited Jun 15 at 8:41
























asked Jun 15 at 7:25









Gayathri Gowda

203




203











  • you want the whole string "/xx1122/fmw/product/111/ohs_111/ohs/bin/odl_rotatelogs" or it must be trim after 'ohs1' ? If must be trim, 'ohs1' is constant?
    – lese
    Jun 15 at 7:37










  • You want the logging directory? Is there no better way of getting than than from the ps output?
    – Kusalananda
    Jun 15 at 7:41










  • I am looking for the directory where the file "component_events.xml" is found .. the reason i am taking it through ps is .. the same file is found in back up locations as well
    – Gayathri Gowda
    Jun 15 at 8:55










  • Do you know beforehand what the backup directories are called? You could use locate together with grep. That would work even if the process isn't running.
    – Kusalananda
    Jun 15 at 13:48










  • i will not know because this script is going to run on many servers. so it will be quite difficult to find the back up directories. That is the reason i am trying to find the file associated with the process
    – Gayathri Gowda
    Jun 15 at 17:22
















  • you want the whole string "/xx1122/fmw/product/111/ohs_111/ohs/bin/odl_rotatelogs" or it must be trim after 'ohs1' ? If must be trim, 'ohs1' is constant?
    – lese
    Jun 15 at 7:37










  • You want the logging directory? Is there no better way of getting than than from the ps output?
    – Kusalananda
    Jun 15 at 7:41










  • I am looking for the directory where the file "component_events.xml" is found .. the reason i am taking it through ps is .. the same file is found in back up locations as well
    – Gayathri Gowda
    Jun 15 at 8:55










  • Do you know beforehand what the backup directories are called? You could use locate together with grep. That would work even if the process isn't running.
    – Kusalananda
    Jun 15 at 13:48










  • i will not know because this script is going to run on many servers. so it will be quite difficult to find the back up directories. That is the reason i am trying to find the file associated with the process
    – Gayathri Gowda
    Jun 15 at 17:22















you want the whole string "/xx1122/fmw/product/111/ohs_111/ohs/bin/odl_rotatelogs" or it must be trim after 'ohs1' ? If must be trim, 'ohs1' is constant?
– lese
Jun 15 at 7:37




you want the whole string "/xx1122/fmw/product/111/ohs_111/ohs/bin/odl_rotatelogs" or it must be trim after 'ohs1' ? If must be trim, 'ohs1' is constant?
– lese
Jun 15 at 7:37












You want the logging directory? Is there no better way of getting than than from the ps output?
– Kusalananda
Jun 15 at 7:41




You want the logging directory? Is there no better way of getting than than from the ps output?
– Kusalananda
Jun 15 at 7:41












I am looking for the directory where the file "component_events.xml" is found .. the reason i am taking it through ps is .. the same file is found in back up locations as well
– Gayathri Gowda
Jun 15 at 8:55




I am looking for the directory where the file "component_events.xml" is found .. the reason i am taking it through ps is .. the same file is found in back up locations as well
– Gayathri Gowda
Jun 15 at 8:55












Do you know beforehand what the backup directories are called? You could use locate together with grep. That would work even if the process isn't running.
– Kusalananda
Jun 15 at 13:48




Do you know beforehand what the backup directories are called? You could use locate together with grep. That would work even if the process isn't running.
– Kusalananda
Jun 15 at 13:48












i will not know because this script is going to run on many servers. so it will be quite difficult to find the back up directories. That is the reason i am trying to find the file associated with the process
– Gayathri Gowda
Jun 15 at 17:22




i will not know because this script is going to run on many servers. so it will be quite difficult to find the back up directories. That is the reason i am trying to find the file associated with the process
– Gayathri Gowda
Jun 15 at 17:22










2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










With sed, POSIXly:



var=$(
ps -Ao args= | sed -n 's|.* -h:(.*)/component_events.xml.*|1|p'
)


In effect, that returns the part of the command line in between the last occurrence of /component_events.xml and the last occurrence before that of -h:.



So if the command line was:



... -h:x/component_events.xml -g:y/component_events.xml


You'd get x/components_events.xml -g:y instead of x.



You could change it to sed -n 's|.* -h:([^ ]*)/component_events.xml.*|1|p' to avoid that, but then that would no longer for directory names that contain space characters.



On a recent GNU/Linux system, you could also do:



LC_ALL=C grep -zhPo '^-h:K.*(?=/component_events.xml)' /proc/*/cmdline |
tr '' 'n'


Which would work for any directory name.






share|improve this answer























  • @RoVo, I can't explain your first problem. For your second, yes some versions of GNU grep have had this kind of problem. Some didn't even support -P combined with -z. With your version, A might work better than ^. See edit.
    – Stéphane Chazelas
    Jun 15 at 11:55






  • 1




    @RoVo, and you have a process with a -h:.../component_events.xml... in its list of arguments?
    – Stéphane Chazelas
    Jun 15 at 12:33










  • no ^^ and this might be the problem ;-)
    – RoVo
    Jun 15 at 12:37






  • 1




    @RoVo, It seems the version of grep on 16.04 has that bug
    – Stéphane Chazelas
    Jun 15 at 12:40











  • @RoVo, I've reverted to ^ as A won't help with those broken grep versions.
    – Stéphane Chazelas
    Jun 15 at 13:18

















up vote
0
down vote













Using pgrep to find the pid and /proc/$PID/cmdline:



var=$(
pgrep -f "component_events.xml" |
xargs -I sh -c "head -z -n1 /proc//cmdline | tr '' 'n'" |
xargs -n1 dirname
)


This also works when the command path has arguments and when multiple PIDs were found.




Explanation:



Search for PIDs:



pgrep -f "component_events.xml"


For each found PID, get the full command (/proc/PID/cmdline), but print only the command without args (head -z -n1).



xargs -I sh -c "head -z -n1 /proc//cmdline


Then replace trailing with newline (n):



tr '' 'n'"


Output the dirname only:



xargs -n1 dirname





share|improve this answer























    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%2f449943%2fget-substring-assigned-to-a-variable%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
    3
    down vote



    accepted










    With sed, POSIXly:



    var=$(
    ps -Ao args= | sed -n 's|.* -h:(.*)/component_events.xml.*|1|p'
    )


    In effect, that returns the part of the command line in between the last occurrence of /component_events.xml and the last occurrence before that of -h:.



    So if the command line was:



    ... -h:x/component_events.xml -g:y/component_events.xml


    You'd get x/components_events.xml -g:y instead of x.



    You could change it to sed -n 's|.* -h:([^ ]*)/component_events.xml.*|1|p' to avoid that, but then that would no longer for directory names that contain space characters.



    On a recent GNU/Linux system, you could also do:



    LC_ALL=C grep -zhPo '^-h:K.*(?=/component_events.xml)' /proc/*/cmdline |
    tr '' 'n'


    Which would work for any directory name.






    share|improve this answer























    • @RoVo, I can't explain your first problem. For your second, yes some versions of GNU grep have had this kind of problem. Some didn't even support -P combined with -z. With your version, A might work better than ^. See edit.
      – Stéphane Chazelas
      Jun 15 at 11:55






    • 1




      @RoVo, and you have a process with a -h:.../component_events.xml... in its list of arguments?
      – Stéphane Chazelas
      Jun 15 at 12:33










    • no ^^ and this might be the problem ;-)
      – RoVo
      Jun 15 at 12:37






    • 1




      @RoVo, It seems the version of grep on 16.04 has that bug
      – Stéphane Chazelas
      Jun 15 at 12:40











    • @RoVo, I've reverted to ^ as A won't help with those broken grep versions.
      – Stéphane Chazelas
      Jun 15 at 13:18














    up vote
    3
    down vote



    accepted










    With sed, POSIXly:



    var=$(
    ps -Ao args= | sed -n 's|.* -h:(.*)/component_events.xml.*|1|p'
    )


    In effect, that returns the part of the command line in between the last occurrence of /component_events.xml and the last occurrence before that of -h:.



    So if the command line was:



    ... -h:x/component_events.xml -g:y/component_events.xml


    You'd get x/components_events.xml -g:y instead of x.



    You could change it to sed -n 's|.* -h:([^ ]*)/component_events.xml.*|1|p' to avoid that, but then that would no longer for directory names that contain space characters.



    On a recent GNU/Linux system, you could also do:



    LC_ALL=C grep -zhPo '^-h:K.*(?=/component_events.xml)' /proc/*/cmdline |
    tr '' 'n'


    Which would work for any directory name.






    share|improve this answer























    • @RoVo, I can't explain your first problem. For your second, yes some versions of GNU grep have had this kind of problem. Some didn't even support -P combined with -z. With your version, A might work better than ^. See edit.
      – Stéphane Chazelas
      Jun 15 at 11:55






    • 1




      @RoVo, and you have a process with a -h:.../component_events.xml... in its list of arguments?
      – Stéphane Chazelas
      Jun 15 at 12:33










    • no ^^ and this might be the problem ;-)
      – RoVo
      Jun 15 at 12:37






    • 1




      @RoVo, It seems the version of grep on 16.04 has that bug
      – Stéphane Chazelas
      Jun 15 at 12:40











    • @RoVo, I've reverted to ^ as A won't help with those broken grep versions.
      – Stéphane Chazelas
      Jun 15 at 13:18












    up vote
    3
    down vote



    accepted







    up vote
    3
    down vote



    accepted






    With sed, POSIXly:



    var=$(
    ps -Ao args= | sed -n 's|.* -h:(.*)/component_events.xml.*|1|p'
    )


    In effect, that returns the part of the command line in between the last occurrence of /component_events.xml and the last occurrence before that of -h:.



    So if the command line was:



    ... -h:x/component_events.xml -g:y/component_events.xml


    You'd get x/components_events.xml -g:y instead of x.



    You could change it to sed -n 's|.* -h:([^ ]*)/component_events.xml.*|1|p' to avoid that, but then that would no longer for directory names that contain space characters.



    On a recent GNU/Linux system, you could also do:



    LC_ALL=C grep -zhPo '^-h:K.*(?=/component_events.xml)' /proc/*/cmdline |
    tr '' 'n'


    Which would work for any directory name.






    share|improve this answer















    With sed, POSIXly:



    var=$(
    ps -Ao args= | sed -n 's|.* -h:(.*)/component_events.xml.*|1|p'
    )


    In effect, that returns the part of the command line in between the last occurrence of /component_events.xml and the last occurrence before that of -h:.



    So if the command line was:



    ... -h:x/component_events.xml -g:y/component_events.xml


    You'd get x/components_events.xml -g:y instead of x.



    You could change it to sed -n 's|.* -h:([^ ]*)/component_events.xml.*|1|p' to avoid that, but then that would no longer for directory names that contain space characters.



    On a recent GNU/Linux system, you could also do:



    LC_ALL=C grep -zhPo '^-h:K.*(?=/component_events.xml)' /proc/*/cmdline |
    tr '' 'n'


    Which would work for any directory name.







    share|improve this answer















    share|improve this answer



    share|improve this answer








    edited Jun 15 at 13:18


























    answered Jun 15 at 7:42









    Stéphane Chazelas

    279k53513844




    279k53513844











    • @RoVo, I can't explain your first problem. For your second, yes some versions of GNU grep have had this kind of problem. Some didn't even support -P combined with -z. With your version, A might work better than ^. See edit.
      – Stéphane Chazelas
      Jun 15 at 11:55






    • 1




      @RoVo, and you have a process with a -h:.../component_events.xml... in its list of arguments?
      – Stéphane Chazelas
      Jun 15 at 12:33










    • no ^^ and this might be the problem ;-)
      – RoVo
      Jun 15 at 12:37






    • 1




      @RoVo, It seems the version of grep on 16.04 has that bug
      – Stéphane Chazelas
      Jun 15 at 12:40











    • @RoVo, I've reverted to ^ as A won't help with those broken grep versions.
      – Stéphane Chazelas
      Jun 15 at 13:18
















    • @RoVo, I can't explain your first problem. For your second, yes some versions of GNU grep have had this kind of problem. Some didn't even support -P combined with -z. With your version, A might work better than ^. See edit.
      – Stéphane Chazelas
      Jun 15 at 11:55






    • 1




      @RoVo, and you have a process with a -h:.../component_events.xml... in its list of arguments?
      – Stéphane Chazelas
      Jun 15 at 12:33










    • no ^^ and this might be the problem ;-)
      – RoVo
      Jun 15 at 12:37






    • 1




      @RoVo, It seems the version of grep on 16.04 has that bug
      – Stéphane Chazelas
      Jun 15 at 12:40











    • @RoVo, I've reverted to ^ as A won't help with those broken grep versions.
      – Stéphane Chazelas
      Jun 15 at 13:18















    @RoVo, I can't explain your first problem. For your second, yes some versions of GNU grep have had this kind of problem. Some didn't even support -P combined with -z. With your version, A might work better than ^. See edit.
    – Stéphane Chazelas
    Jun 15 at 11:55




    @RoVo, I can't explain your first problem. For your second, yes some versions of GNU grep have had this kind of problem. Some didn't even support -P combined with -z. With your version, A might work better than ^. See edit.
    – Stéphane Chazelas
    Jun 15 at 11:55




    1




    1




    @RoVo, and you have a process with a -h:.../component_events.xml... in its list of arguments?
    – Stéphane Chazelas
    Jun 15 at 12:33




    @RoVo, and you have a process with a -h:.../component_events.xml... in its list of arguments?
    – Stéphane Chazelas
    Jun 15 at 12:33












    no ^^ and this might be the problem ;-)
    – RoVo
    Jun 15 at 12:37




    no ^^ and this might be the problem ;-)
    – RoVo
    Jun 15 at 12:37




    1




    1




    @RoVo, It seems the version of grep on 16.04 has that bug
    – Stéphane Chazelas
    Jun 15 at 12:40





    @RoVo, It seems the version of grep on 16.04 has that bug
    – Stéphane Chazelas
    Jun 15 at 12:40













    @RoVo, I've reverted to ^ as A won't help with those broken grep versions.
    – Stéphane Chazelas
    Jun 15 at 13:18




    @RoVo, I've reverted to ^ as A won't help with those broken grep versions.
    – Stéphane Chazelas
    Jun 15 at 13:18












    up vote
    0
    down vote













    Using pgrep to find the pid and /proc/$PID/cmdline:



    var=$(
    pgrep -f "component_events.xml" |
    xargs -I sh -c "head -z -n1 /proc//cmdline | tr '' 'n'" |
    xargs -n1 dirname
    )


    This also works when the command path has arguments and when multiple PIDs were found.




    Explanation:



    Search for PIDs:



    pgrep -f "component_events.xml"


    For each found PID, get the full command (/proc/PID/cmdline), but print only the command without args (head -z -n1).



    xargs -I sh -c "head -z -n1 /proc//cmdline


    Then replace trailing with newline (n):



    tr '' 'n'"


    Output the dirname only:



    xargs -n1 dirname





    share|improve this answer



























      up vote
      0
      down vote













      Using pgrep to find the pid and /proc/$PID/cmdline:



      var=$(
      pgrep -f "component_events.xml" |
      xargs -I sh -c "head -z -n1 /proc//cmdline | tr '' 'n'" |
      xargs -n1 dirname
      )


      This also works when the command path has arguments and when multiple PIDs were found.




      Explanation:



      Search for PIDs:



      pgrep -f "component_events.xml"


      For each found PID, get the full command (/proc/PID/cmdline), but print only the command without args (head -z -n1).



      xargs -I sh -c "head -z -n1 /proc//cmdline


      Then replace trailing with newline (n):



      tr '' 'n'"


      Output the dirname only:



      xargs -n1 dirname





      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        Using pgrep to find the pid and /proc/$PID/cmdline:



        var=$(
        pgrep -f "component_events.xml" |
        xargs -I sh -c "head -z -n1 /proc//cmdline | tr '' 'n'" |
        xargs -n1 dirname
        )


        This also works when the command path has arguments and when multiple PIDs were found.




        Explanation:



        Search for PIDs:



        pgrep -f "component_events.xml"


        For each found PID, get the full command (/proc/PID/cmdline), but print only the command without args (head -z -n1).



        xargs -I sh -c "head -z -n1 /proc//cmdline


        Then replace trailing with newline (n):



        tr '' 'n'"


        Output the dirname only:



        xargs -n1 dirname





        share|improve this answer















        Using pgrep to find the pid and /proc/$PID/cmdline:



        var=$(
        pgrep -f "component_events.xml" |
        xargs -I sh -c "head -z -n1 /proc//cmdline | tr '' 'n'" |
        xargs -n1 dirname
        )


        This also works when the command path has arguments and when multiple PIDs were found.




        Explanation:



        Search for PIDs:



        pgrep -f "component_events.xml"


        For each found PID, get the full command (/proc/PID/cmdline), but print only the command without args (head -z -n1).



        xargs -I sh -c "head -z -n1 /proc//cmdline


        Then replace trailing with newline (n):



        tr '' 'n'"


        Output the dirname only:



        xargs -n1 dirname






        share|improve this answer















        share|improve this answer



        share|improve this answer








        edited Jun 15 at 8:35


























        answered Jun 15 at 8:16









        RoVo

        83219




        83219






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f449943%2fget-substring-assigned-to-a-variable%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)