How to use regular expression result into another expression












1















I have two regular expressions i.e. command1 and command2 where i need to combine both expressions into a single expression using | for that command1 output should be passed to next expression.



command1:



grep 0x017a /sys/bus/pci/devices/*/device | cut -d/ -f6
>> Output : 00:00:01


command 2:



head -n1 /sys/bus/pci/devices/00:00:01/resource | cut -d ' ' -f 1 | tail -c 9


Can you please let me how to use command1 output (00:00:01) into command2 and combine into a single expression.










share|improve this question

























  • Does head -n1 /sys/bus/pci/devices/`grep 0x017a /sys/bus/pci/devices/*/device | cut -d/ -f6`/resource | cut -d ' ' -f 1 | tail -c 9 work?

    – Hagen von Eitzen
    Feb 12 at 1:01













  • I have tried but didn't work this way.

    – Seshagiri Lekkala
    Feb 12 at 1:09











  • what does command 2 return, what is the desired output?

    – Evan Carroll
    Feb 12 at 1:59













  • "head -n1 /sys/bus/pci/devices/00:00:01/resource | cut -d ' ' -f 1 | tail -c 9 " output is : da00000.

    – Seshagiri Lekkala
    Feb 12 at 2:03











  • Here, 00:00:01 is command 1 output.

    – Seshagiri Lekkala
    Feb 12 at 2:04
















1















I have two regular expressions i.e. command1 and command2 where i need to combine both expressions into a single expression using | for that command1 output should be passed to next expression.



command1:



grep 0x017a /sys/bus/pci/devices/*/device | cut -d/ -f6
>> Output : 00:00:01


command 2:



head -n1 /sys/bus/pci/devices/00:00:01/resource | cut -d ' ' -f 1 | tail -c 9


Can you please let me how to use command1 output (00:00:01) into command2 and combine into a single expression.










share|improve this question

























  • Does head -n1 /sys/bus/pci/devices/`grep 0x017a /sys/bus/pci/devices/*/device | cut -d/ -f6`/resource | cut -d ' ' -f 1 | tail -c 9 work?

    – Hagen von Eitzen
    Feb 12 at 1:01













  • I have tried but didn't work this way.

    – Seshagiri Lekkala
    Feb 12 at 1:09











  • what does command 2 return, what is the desired output?

    – Evan Carroll
    Feb 12 at 1:59













  • "head -n1 /sys/bus/pci/devices/00:00:01/resource | cut -d ' ' -f 1 | tail -c 9 " output is : da00000.

    – Seshagiri Lekkala
    Feb 12 at 2:03











  • Here, 00:00:01 is command 1 output.

    – Seshagiri Lekkala
    Feb 12 at 2:04














1












1








1








I have two regular expressions i.e. command1 and command2 where i need to combine both expressions into a single expression using | for that command1 output should be passed to next expression.



command1:



grep 0x017a /sys/bus/pci/devices/*/device | cut -d/ -f6
>> Output : 00:00:01


command 2:



head -n1 /sys/bus/pci/devices/00:00:01/resource | cut -d ' ' -f 1 | tail -c 9


Can you please let me how to use command1 output (00:00:01) into command2 and combine into a single expression.










share|improve this question
















I have two regular expressions i.e. command1 and command2 where i need to combine both expressions into a single expression using | for that command1 output should be passed to next expression.



command1:



grep 0x017a /sys/bus/pci/devices/*/device | cut -d/ -f6
>> Output : 00:00:01


command 2:



head -n1 /sys/bus/pci/devices/00:00:01/resource | cut -d ' ' -f 1 | tail -c 9


Can you please let me how to use command1 output (00:00:01) into command2 and combine into a single expression.







shell command-line command-substitution






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 12 at 10:41









jimmij

31.9k874108




31.9k874108










asked Feb 12 at 0:55









Seshagiri LekkalaSeshagiri Lekkala

154




154













  • Does head -n1 /sys/bus/pci/devices/`grep 0x017a /sys/bus/pci/devices/*/device | cut -d/ -f6`/resource | cut -d ' ' -f 1 | tail -c 9 work?

    – Hagen von Eitzen
    Feb 12 at 1:01













  • I have tried but didn't work this way.

    – Seshagiri Lekkala
    Feb 12 at 1:09











  • what does command 2 return, what is the desired output?

    – Evan Carroll
    Feb 12 at 1:59













  • "head -n1 /sys/bus/pci/devices/00:00:01/resource | cut -d ' ' -f 1 | tail -c 9 " output is : da00000.

    – Seshagiri Lekkala
    Feb 12 at 2:03











  • Here, 00:00:01 is command 1 output.

    – Seshagiri Lekkala
    Feb 12 at 2:04



















  • Does head -n1 /sys/bus/pci/devices/`grep 0x017a /sys/bus/pci/devices/*/device | cut -d/ -f6`/resource | cut -d ' ' -f 1 | tail -c 9 work?

    – Hagen von Eitzen
    Feb 12 at 1:01













  • I have tried but didn't work this way.

    – Seshagiri Lekkala
    Feb 12 at 1:09











  • what does command 2 return, what is the desired output?

    – Evan Carroll
    Feb 12 at 1:59













  • "head -n1 /sys/bus/pci/devices/00:00:01/resource | cut -d ' ' -f 1 | tail -c 9 " output is : da00000.

    – Seshagiri Lekkala
    Feb 12 at 2:03











  • Here, 00:00:01 is command 1 output.

    – Seshagiri Lekkala
    Feb 12 at 2:04

















Does head -n1 /sys/bus/pci/devices/`grep 0x017a /sys/bus/pci/devices/*/device | cut -d/ -f6`/resource | cut -d ' ' -f 1 | tail -c 9 work?

– Hagen von Eitzen
Feb 12 at 1:01







Does head -n1 /sys/bus/pci/devices/`grep 0x017a /sys/bus/pci/devices/*/device | cut -d/ -f6`/resource | cut -d ' ' -f 1 | tail -c 9 work?

– Hagen von Eitzen
Feb 12 at 1:01















I have tried but didn't work this way.

– Seshagiri Lekkala
Feb 12 at 1:09





I have tried but didn't work this way.

– Seshagiri Lekkala
Feb 12 at 1:09













what does command 2 return, what is the desired output?

– Evan Carroll
Feb 12 at 1:59







what does command 2 return, what is the desired output?

– Evan Carroll
Feb 12 at 1:59















"head -n1 /sys/bus/pci/devices/00:00:01/resource | cut -d ' ' -f 1 | tail -c 9 " output is : da00000.

– Seshagiri Lekkala
Feb 12 at 2:03





"head -n1 /sys/bus/pci/devices/00:00:01/resource | cut -d ' ' -f 1 | tail -c 9 " output is : da00000.

– Seshagiri Lekkala
Feb 12 at 2:03













Here, 00:00:01 is command 1 output.

– Seshagiri Lekkala
Feb 12 at 2:04





Here, 00:00:01 is command 1 output.

– Seshagiri Lekkala
Feb 12 at 2:04










2 Answers
2






active

oldest

votes


















1














To use the output of one command as argument of the second command the mechanism of command substitution $() can be utilized. For example:



Instead of



$ whoami
jimmij

$ ls /home/jimmij/tmp
file1 file2


you can do



$ ls /home/"$(whoami)"/tmp
file file2




In your specific case the single command become



head -n1 "/sys/bus/pci/devices/$(grep 0x017a /sys/bus/pci/devices/*/device | cut -d/ -f6)/resource" | cut -d ' ' -f 1 | tail -c 9


Notice I also quoted the entire expression, read here why you should do that.






share|improve this answer

































    1














    Use the $(command) syntax (or the older `command` syntax).





    DEVICE=$(grep 0x017a /sys/bus/pci/devices/*/device | cut -d/ -f6)
    head -n1 "/sys/bus/pci/devices/$DEVICE/resource" | cut -d ' ' -f 1 | tail -c 9


    Oh. "Combine into a single expression" similarly.



    DEVICE=$(grep 0x017a /sys/bus/pci/devices/*/device | cut -d/ -f6)
    OUTPUT=$(head -n1 "/sys/bus/pci/devices/$DEVICE/resource" | cut -d ' ' -f 1 | tail -c 9)

    echo "Output is $OUTPUT"





    share|improve this answer


























    • May i know how can we combine both line into single line ?

      – Seshagiri Lekkala
      Feb 12 at 1:57











    • Get out of the habit of using ALLCAPS varnames: one day you'll use PATH=... and then wonder why your script is broken.

      – glenn jackman
      Feb 12 at 15:05











    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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    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%2f500075%2fhow-to-use-regular-expression-result-into-another-expression%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    To use the output of one command as argument of the second command the mechanism of command substitution $() can be utilized. For example:



    Instead of



    $ whoami
    jimmij

    $ ls /home/jimmij/tmp
    file1 file2


    you can do



    $ ls /home/"$(whoami)"/tmp
    file file2




    In your specific case the single command become



    head -n1 "/sys/bus/pci/devices/$(grep 0x017a /sys/bus/pci/devices/*/device | cut -d/ -f6)/resource" | cut -d ' ' -f 1 | tail -c 9


    Notice I also quoted the entire expression, read here why you should do that.






    share|improve this answer






























      1














      To use the output of one command as argument of the second command the mechanism of command substitution $() can be utilized. For example:



      Instead of



      $ whoami
      jimmij

      $ ls /home/jimmij/tmp
      file1 file2


      you can do



      $ ls /home/"$(whoami)"/tmp
      file file2




      In your specific case the single command become



      head -n1 "/sys/bus/pci/devices/$(grep 0x017a /sys/bus/pci/devices/*/device | cut -d/ -f6)/resource" | cut -d ' ' -f 1 | tail -c 9


      Notice I also quoted the entire expression, read here why you should do that.






      share|improve this answer




























        1












        1








        1







        To use the output of one command as argument of the second command the mechanism of command substitution $() can be utilized. For example:



        Instead of



        $ whoami
        jimmij

        $ ls /home/jimmij/tmp
        file1 file2


        you can do



        $ ls /home/"$(whoami)"/tmp
        file file2




        In your specific case the single command become



        head -n1 "/sys/bus/pci/devices/$(grep 0x017a /sys/bus/pci/devices/*/device | cut -d/ -f6)/resource" | cut -d ' ' -f 1 | tail -c 9


        Notice I also quoted the entire expression, read here why you should do that.






        share|improve this answer















        To use the output of one command as argument of the second command the mechanism of command substitution $() can be utilized. For example:



        Instead of



        $ whoami
        jimmij

        $ ls /home/jimmij/tmp
        file1 file2


        you can do



        $ ls /home/"$(whoami)"/tmp
        file file2




        In your specific case the single command become



        head -n1 "/sys/bus/pci/devices/$(grep 0x017a /sys/bus/pci/devices/*/device | cut -d/ -f6)/resource" | cut -d ' ' -f 1 | tail -c 9


        Notice I also quoted the entire expression, read here why you should do that.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Feb 12 at 20:06

























        answered Feb 12 at 10:40









        jimmijjimmij

        31.9k874108




        31.9k874108

























            1














            Use the $(command) syntax (or the older `command` syntax).





            DEVICE=$(grep 0x017a /sys/bus/pci/devices/*/device | cut -d/ -f6)
            head -n1 "/sys/bus/pci/devices/$DEVICE/resource" | cut -d ' ' -f 1 | tail -c 9


            Oh. "Combine into a single expression" similarly.



            DEVICE=$(grep 0x017a /sys/bus/pci/devices/*/device | cut -d/ -f6)
            OUTPUT=$(head -n1 "/sys/bus/pci/devices/$DEVICE/resource" | cut -d ' ' -f 1 | tail -c 9)

            echo "Output is $OUTPUT"





            share|improve this answer


























            • May i know how can we combine both line into single line ?

              – Seshagiri Lekkala
              Feb 12 at 1:57











            • Get out of the habit of using ALLCAPS varnames: one day you'll use PATH=... and then wonder why your script is broken.

              – glenn jackman
              Feb 12 at 15:05
















            1














            Use the $(command) syntax (or the older `command` syntax).





            DEVICE=$(grep 0x017a /sys/bus/pci/devices/*/device | cut -d/ -f6)
            head -n1 "/sys/bus/pci/devices/$DEVICE/resource" | cut -d ' ' -f 1 | tail -c 9


            Oh. "Combine into a single expression" similarly.



            DEVICE=$(grep 0x017a /sys/bus/pci/devices/*/device | cut -d/ -f6)
            OUTPUT=$(head -n1 "/sys/bus/pci/devices/$DEVICE/resource" | cut -d ' ' -f 1 | tail -c 9)

            echo "Output is $OUTPUT"





            share|improve this answer


























            • May i know how can we combine both line into single line ?

              – Seshagiri Lekkala
              Feb 12 at 1:57











            • Get out of the habit of using ALLCAPS varnames: one day you'll use PATH=... and then wonder why your script is broken.

              – glenn jackman
              Feb 12 at 15:05














            1












            1








            1







            Use the $(command) syntax (or the older `command` syntax).





            DEVICE=$(grep 0x017a /sys/bus/pci/devices/*/device | cut -d/ -f6)
            head -n1 "/sys/bus/pci/devices/$DEVICE/resource" | cut -d ' ' -f 1 | tail -c 9


            Oh. "Combine into a single expression" similarly.



            DEVICE=$(grep 0x017a /sys/bus/pci/devices/*/device | cut -d/ -f6)
            OUTPUT=$(head -n1 "/sys/bus/pci/devices/$DEVICE/resource" | cut -d ' ' -f 1 | tail -c 9)

            echo "Output is $OUTPUT"





            share|improve this answer















            Use the $(command) syntax (or the older `command` syntax).





            DEVICE=$(grep 0x017a /sys/bus/pci/devices/*/device | cut -d/ -f6)
            head -n1 "/sys/bus/pci/devices/$DEVICE/resource" | cut -d ' ' -f 1 | tail -c 9


            Oh. "Combine into a single expression" similarly.



            DEVICE=$(grep 0x017a /sys/bus/pci/devices/*/device | cut -d/ -f6)
            OUTPUT=$(head -n1 "/sys/bus/pci/devices/$DEVICE/resource" | cut -d ' ' -f 1 | tail -c 9)

            echo "Output is $OUTPUT"






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 12 at 1:55

























            answered Feb 12 at 1:46









            Ken JacksonKen Jackson

            1063




            1063













            • May i know how can we combine both line into single line ?

              – Seshagiri Lekkala
              Feb 12 at 1:57











            • Get out of the habit of using ALLCAPS varnames: one day you'll use PATH=... and then wonder why your script is broken.

              – glenn jackman
              Feb 12 at 15:05



















            • May i know how can we combine both line into single line ?

              – Seshagiri Lekkala
              Feb 12 at 1:57











            • Get out of the habit of using ALLCAPS varnames: one day you'll use PATH=... and then wonder why your script is broken.

              – glenn jackman
              Feb 12 at 15:05

















            May i know how can we combine both line into single line ?

            – Seshagiri Lekkala
            Feb 12 at 1:57





            May i know how can we combine both line into single line ?

            – Seshagiri Lekkala
            Feb 12 at 1:57













            Get out of the habit of using ALLCAPS varnames: one day you'll use PATH=... and then wonder why your script is broken.

            – glenn jackman
            Feb 12 at 15:05





            Get out of the habit of using ALLCAPS varnames: one day you'll use PATH=... and then wonder why your script is broken.

            – glenn jackman
            Feb 12 at 15:05


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f500075%2fhow-to-use-regular-expression-result-into-another-expression%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

            is 'sed' thread safe

            How to make a Squid Proxy server?