Extract substring according to regexp with sed or grep2019 Community Moderator ElectionCan grep output only specified groupings that match?How to treat a file as a single line with grep to apply a regexp search pattern?Extracting a regex matched with 'sed' without printing the surrounding charactersgrep (/sed/awk) month rangeFunction to simplify grep with an often used logUsing sed instead of grep to output only matching part of lineErasing 2-lines pattern with sed/grep/whateverHow to split an output to two files with grep?How to make BSD grep respect start-of-line anchorbash script to replace script tags in html with their content

What is the meaning of "You've never met a graph you didn't like?"

Showing mass murder in a kid's book

Output visual diagram of picture

Did I make a mistake by ccing email to boss to others?

Mortal danger in mid-grade literature

Would this string work as string?

What is the period/term used describe Giuseppe Arcimboldo's style of painting?

Hashing password to increase entropy

Why is participating in the European Parliamentary elections used as a threat?

Asserting that Atheism and Theism are both faith based positions

Why do Radio Buttons not fill the entire outer circle?

Reason why a kingside attack is not justified

Do I have to take mana from my deck or hand when tapping this card?

Why is implicit conversion not ambiguous for non-primitive types?

How do you justify more code being written by following clean code practices?

Unfrosted light bulb

What is the purpose of using a decision tree?

Derivative of an interpolated function

Is there a distance limit for minecart tracks?

How to test the sharpness of a knife?

What do the positive and negative (+/-) transmit and receive pins mean on Ethernet cables?

What is it called when someone votes for an option that's not their first choice?

Can you take a "free object interaction" while incapacitated?

Why can't I get pgrep output right to variable on bash script?



Extract substring according to regexp with sed or grep



2019 Community Moderator ElectionCan grep output only specified groupings that match?How to treat a file as a single line with grep to apply a regexp search pattern?Extracting a regex matched with 'sed' without printing the surrounding charactersgrep (/sed/awk) month rangeFunction to simplify grep with an often used logUsing sed instead of grep to output only matching part of lineErasing 2-lines pattern with sed/grep/whateverHow to split an output to two files with grep?How to make BSD grep respect start-of-line anchorbash script to replace script tags in html with their content










4















In a (BSD) UNIX environment, I would like to capture a specific substring using a regular expression.



Assume that the dmesg command output would include the following line:



pass2: <Marvell Console 1.01> Removable Processor SCSI device


I would like to capture the text between the < and > characters, like



dmesg | <sed command>



should output:



Marvell Console 1.01


However, it should not output anything if the regex does not match. Many solutions including sed -e 's/$regex/1/ will output the whole input if no match is found, which is not what i want.



The corresponding regexp could be:
regex="^pass2: <(.*)>"



How would i properly do a regex match using sed or grep? Note that the grep -P option is unavailable in my BSD UNIX distribution. The sed -E option is available, however.










share|improve this question






















  • It's possibly better to parse the output of camcontrol devlist than the output of dmesg.

    – JdeBP
    13 hours ago















4















In a (BSD) UNIX environment, I would like to capture a specific substring using a regular expression.



Assume that the dmesg command output would include the following line:



pass2: <Marvell Console 1.01> Removable Processor SCSI device


I would like to capture the text between the < and > characters, like



dmesg | <sed command>



should output:



Marvell Console 1.01


However, it should not output anything if the regex does not match. Many solutions including sed -e 's/$regex/1/ will output the whole input if no match is found, which is not what i want.



The corresponding regexp could be:
regex="^pass2: <(.*)>"



How would i properly do a regex match using sed or grep? Note that the grep -P option is unavailable in my BSD UNIX distribution. The sed -E option is available, however.










share|improve this question






















  • It's possibly better to parse the output of camcontrol devlist than the output of dmesg.

    – JdeBP
    13 hours ago













4












4








4








In a (BSD) UNIX environment, I would like to capture a specific substring using a regular expression.



Assume that the dmesg command output would include the following line:



pass2: <Marvell Console 1.01> Removable Processor SCSI device


I would like to capture the text between the < and > characters, like



dmesg | <sed command>



should output:



Marvell Console 1.01


However, it should not output anything if the regex does not match. Many solutions including sed -e 's/$regex/1/ will output the whole input if no match is found, which is not what i want.



The corresponding regexp could be:
regex="^pass2: <(.*)>"



How would i properly do a regex match using sed or grep? Note that the grep -P option is unavailable in my BSD UNIX distribution. The sed -E option is available, however.










share|improve this question














In a (BSD) UNIX environment, I would like to capture a specific substring using a regular expression.



Assume that the dmesg command output would include the following line:



pass2: <Marvell Console 1.01> Removable Processor SCSI device


I would like to capture the text between the < and > characters, like



dmesg | <sed command>



should output:



Marvell Console 1.01


However, it should not output anything if the regex does not match. Many solutions including sed -e 's/$regex/1/ will output the whole input if no match is found, which is not what i want.



The corresponding regexp could be:
regex="^pass2: <(.*)>"



How would i properly do a regex match using sed or grep? Note that the grep -P option is unavailable in my BSD UNIX distribution. The sed -E option is available, however.







sed grep regular-expression






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 17 hours ago









SteinerSteiner

788




788












  • It's possibly better to parse the output of camcontrol devlist than the output of dmesg.

    – JdeBP
    13 hours ago

















  • It's possibly better to parse the output of camcontrol devlist than the output of dmesg.

    – JdeBP
    13 hours ago
















It's possibly better to parse the output of camcontrol devlist than the output of dmesg.

– JdeBP
13 hours ago





It's possibly better to parse the output of camcontrol devlist than the output of dmesg.

– JdeBP
13 hours ago










3 Answers
3






active

oldest

votes


















7














Try this,



sed -nE 's/^pass2:.*<(.*)>.*$/1/p'


Or POSIXly (-E has not made it to the POSIX standard yet as of 2019):



sed -n 's/^pass2:.*<(.*)>.*$/1/p'


Output:



$ printf '%sn' 'pass2: <Marvell Console 1.01> Removable Processor SCSI device' | sed -nE 's/^pass2:.*<(.*)>.*$/1/p'
Marvell Console 1.01


This will only print the last occurrence of <...> for each line.






share|improve this answer

























  • This works for me, with both the -n parameter and the /p suffix inside the regex. Full command i used: dmesg | sed -nE 's/^pass2: <(.*)>.*$/1/p

    – Steiner
    17 hours ago






  • 1





    Why not use <([^>]+)>? I.e. not-> one-or-more times

    – Rich
    13 hours ago


















5














How about -o under grep to just print the matching part? We still need to remove the <>, though, but tr works there.



dmesg |egrep -o "<([a-zA-Z.0-9 ]+)>" |tr -d "<>"
Marvell Console 1.01





share|improve this answer
































    2














    I tried below 3 methods by using sed, awk and python



    sed command



    echo "pass2: <Marvell Console 1.01> Removable Processor SCSI device" | sed "s/.*<//g"|sed "s/>.*//g"


    output



    Marvell Console 1.01


    awk command



    echo "pass2: <Marvell Console 1.01> Removable Processor SCSI device" | awk -F "[<>]" 'print $2'


    output



    Marvell Console 1.01


    python



    #!/usr/bin/python
    import re
    h=[]
    k=open('l.txt','r')
    l=k.readlines()
    for i in l:
    o=i.split(' ')
    for i in o[1:4]:
    h.append(i)
    print (" ".join(h)).replace('>','').replace('<','')


    output



    Marvell Console 1.01





    share|improve this answer

























    • I was thinking the awk approach too. Should you constrain your print to lines beginning with "pass2:"? The OP didn't provide sufficient detail, but I can imagine that a naive pattern match would not be quite what was wanted.

      – jwm
      6 hours ago











    • Python can read from standard in, though perl specializes in this kind of text processing if you’re moving into higher level scripting languages.

      – D. Ben Knoble
      3 hours ago










    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%2f507188%2fextract-substring-according-to-regexp-with-sed-or-grep%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    7














    Try this,



    sed -nE 's/^pass2:.*<(.*)>.*$/1/p'


    Or POSIXly (-E has not made it to the POSIX standard yet as of 2019):



    sed -n 's/^pass2:.*<(.*)>.*$/1/p'


    Output:



    $ printf '%sn' 'pass2: <Marvell Console 1.01> Removable Processor SCSI device' | sed -nE 's/^pass2:.*<(.*)>.*$/1/p'
    Marvell Console 1.01


    This will only print the last occurrence of <...> for each line.






    share|improve this answer

























    • This works for me, with both the -n parameter and the /p suffix inside the regex. Full command i used: dmesg | sed -nE 's/^pass2: <(.*)>.*$/1/p

      – Steiner
      17 hours ago






    • 1





      Why not use <([^>]+)>? I.e. not-> one-or-more times

      – Rich
      13 hours ago















    7














    Try this,



    sed -nE 's/^pass2:.*<(.*)>.*$/1/p'


    Or POSIXly (-E has not made it to the POSIX standard yet as of 2019):



    sed -n 's/^pass2:.*<(.*)>.*$/1/p'


    Output:



    $ printf '%sn' 'pass2: <Marvell Console 1.01> Removable Processor SCSI device' | sed -nE 's/^pass2:.*<(.*)>.*$/1/p'
    Marvell Console 1.01


    This will only print the last occurrence of <...> for each line.






    share|improve this answer

























    • This works for me, with both the -n parameter and the /p suffix inside the regex. Full command i used: dmesg | sed -nE 's/^pass2: <(.*)>.*$/1/p

      – Steiner
      17 hours ago






    • 1





      Why not use <([^>]+)>? I.e. not-> one-or-more times

      – Rich
      13 hours ago













    7












    7








    7







    Try this,



    sed -nE 's/^pass2:.*<(.*)>.*$/1/p'


    Or POSIXly (-E has not made it to the POSIX standard yet as of 2019):



    sed -n 's/^pass2:.*<(.*)>.*$/1/p'


    Output:



    $ printf '%sn' 'pass2: <Marvell Console 1.01> Removable Processor SCSI device' | sed -nE 's/^pass2:.*<(.*)>.*$/1/p'
    Marvell Console 1.01


    This will only print the last occurrence of <...> for each line.






    share|improve this answer















    Try this,



    sed -nE 's/^pass2:.*<(.*)>.*$/1/p'


    Or POSIXly (-E has not made it to the POSIX standard yet as of 2019):



    sed -n 's/^pass2:.*<(.*)>.*$/1/p'


    Output:



    $ printf '%sn' 'pass2: <Marvell Console 1.01> Removable Processor SCSI device' | sed -nE 's/^pass2:.*<(.*)>.*$/1/p'
    Marvell Console 1.01


    This will only print the last occurrence of <...> for each line.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 16 hours ago









    Stéphane Chazelas

    310k57586945




    310k57586945










    answered 17 hours ago









    RoVoRoVo

    3,332317




    3,332317












    • This works for me, with both the -n parameter and the /p suffix inside the regex. Full command i used: dmesg | sed -nE 's/^pass2: <(.*)>.*$/1/p

      – Steiner
      17 hours ago






    • 1





      Why not use <([^>]+)>? I.e. not-> one-or-more times

      – Rich
      13 hours ago

















    • This works for me, with both the -n parameter and the /p suffix inside the regex. Full command i used: dmesg | sed -nE 's/^pass2: <(.*)>.*$/1/p

      – Steiner
      17 hours ago






    • 1





      Why not use <([^>]+)>? I.e. not-> one-or-more times

      – Rich
      13 hours ago
















    This works for me, with both the -n parameter and the /p suffix inside the regex. Full command i used: dmesg | sed -nE 's/^pass2: <(.*)>.*$/1/p

    – Steiner
    17 hours ago





    This works for me, with both the -n parameter and the /p suffix inside the regex. Full command i used: dmesg | sed -nE 's/^pass2: <(.*)>.*$/1/p

    – Steiner
    17 hours ago




    1




    1





    Why not use <([^>]+)>? I.e. not-> one-or-more times

    – Rich
    13 hours ago





    Why not use <([^>]+)>? I.e. not-> one-or-more times

    – Rich
    13 hours ago













    5














    How about -o under grep to just print the matching part? We still need to remove the <>, though, but tr works there.



    dmesg |egrep -o "<([a-zA-Z.0-9 ]+)>" |tr -d "<>"
    Marvell Console 1.01





    share|improve this answer





























      5














      How about -o under grep to just print the matching part? We still need to remove the <>, though, but tr works there.



      dmesg |egrep -o "<([a-zA-Z.0-9 ]+)>" |tr -d "<>"
      Marvell Console 1.01





      share|improve this answer



























        5












        5








        5







        How about -o under grep to just print the matching part? We still need to remove the <>, though, but tr works there.



        dmesg |egrep -o "<([a-zA-Z.0-9 ]+)>" |tr -d "<>"
        Marvell Console 1.01





        share|improve this answer















        How about -o under grep to just print the matching part? We still need to remove the <>, though, but tr works there.



        dmesg |egrep -o "<([a-zA-Z.0-9 ]+)>" |tr -d "<>"
        Marvell Console 1.01






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 10 hours ago









        ilkkachu

        61.7k10101177




        61.7k10101177










        answered 17 hours ago









        Radek RadekRadek Radek

        1014




        1014





















            2














            I tried below 3 methods by using sed, awk and python



            sed command



            echo "pass2: <Marvell Console 1.01> Removable Processor SCSI device" | sed "s/.*<//g"|sed "s/>.*//g"


            output



            Marvell Console 1.01


            awk command



            echo "pass2: <Marvell Console 1.01> Removable Processor SCSI device" | awk -F "[<>]" 'print $2'


            output



            Marvell Console 1.01


            python



            #!/usr/bin/python
            import re
            h=[]
            k=open('l.txt','r')
            l=k.readlines()
            for i in l:
            o=i.split(' ')
            for i in o[1:4]:
            h.append(i)
            print (" ".join(h)).replace('>','').replace('<','')


            output



            Marvell Console 1.01





            share|improve this answer

























            • I was thinking the awk approach too. Should you constrain your print to lines beginning with "pass2:"? The OP didn't provide sufficient detail, but I can imagine that a naive pattern match would not be quite what was wanted.

              – jwm
              6 hours ago











            • Python can read from standard in, though perl specializes in this kind of text processing if you’re moving into higher level scripting languages.

              – D. Ben Knoble
              3 hours ago















            2














            I tried below 3 methods by using sed, awk and python



            sed command



            echo "pass2: <Marvell Console 1.01> Removable Processor SCSI device" | sed "s/.*<//g"|sed "s/>.*//g"


            output



            Marvell Console 1.01


            awk command



            echo "pass2: <Marvell Console 1.01> Removable Processor SCSI device" | awk -F "[<>]" 'print $2'


            output



            Marvell Console 1.01


            python



            #!/usr/bin/python
            import re
            h=[]
            k=open('l.txt','r')
            l=k.readlines()
            for i in l:
            o=i.split(' ')
            for i in o[1:4]:
            h.append(i)
            print (" ".join(h)).replace('>','').replace('<','')


            output



            Marvell Console 1.01





            share|improve this answer

























            • I was thinking the awk approach too. Should you constrain your print to lines beginning with "pass2:"? The OP didn't provide sufficient detail, but I can imagine that a naive pattern match would not be quite what was wanted.

              – jwm
              6 hours ago











            • Python can read from standard in, though perl specializes in this kind of text processing if you’re moving into higher level scripting languages.

              – D. Ben Knoble
              3 hours ago













            2












            2








            2







            I tried below 3 methods by using sed, awk and python



            sed command



            echo "pass2: <Marvell Console 1.01> Removable Processor SCSI device" | sed "s/.*<//g"|sed "s/>.*//g"


            output



            Marvell Console 1.01


            awk command



            echo "pass2: <Marvell Console 1.01> Removable Processor SCSI device" | awk -F "[<>]" 'print $2'


            output



            Marvell Console 1.01


            python



            #!/usr/bin/python
            import re
            h=[]
            k=open('l.txt','r')
            l=k.readlines()
            for i in l:
            o=i.split(' ')
            for i in o[1:4]:
            h.append(i)
            print (" ".join(h)).replace('>','').replace('<','')


            output



            Marvell Console 1.01





            share|improve this answer















            I tried below 3 methods by using sed, awk and python



            sed command



            echo "pass2: <Marvell Console 1.01> Removable Processor SCSI device" | sed "s/.*<//g"|sed "s/>.*//g"


            output



            Marvell Console 1.01


            awk command



            echo "pass2: <Marvell Console 1.01> Removable Processor SCSI device" | awk -F "[<>]" 'print $2'


            output



            Marvell Console 1.01


            python



            #!/usr/bin/python
            import re
            h=[]
            k=open('l.txt','r')
            l=k.readlines()
            for i in l:
            o=i.split(' ')
            for i in o[1:4]:
            h.append(i)
            print (" ".join(h)).replace('>','').replace('<','')


            output



            Marvell Console 1.01






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 10 hours ago









            ilkkachu

            61.7k10101177




            61.7k10101177










            answered 13 hours ago









            Praveen Kumar BSPraveen Kumar BS

            1,6101311




            1,6101311












            • I was thinking the awk approach too. Should you constrain your print to lines beginning with "pass2:"? The OP didn't provide sufficient detail, but I can imagine that a naive pattern match would not be quite what was wanted.

              – jwm
              6 hours ago











            • Python can read from standard in, though perl specializes in this kind of text processing if you’re moving into higher level scripting languages.

              – D. Ben Knoble
              3 hours ago

















            • I was thinking the awk approach too. Should you constrain your print to lines beginning with "pass2:"? The OP didn't provide sufficient detail, but I can imagine that a naive pattern match would not be quite what was wanted.

              – jwm
              6 hours ago











            • Python can read from standard in, though perl specializes in this kind of text processing if you’re moving into higher level scripting languages.

              – D. Ben Knoble
              3 hours ago
















            I was thinking the awk approach too. Should you constrain your print to lines beginning with "pass2:"? The OP didn't provide sufficient detail, but I can imagine that a naive pattern match would not be quite what was wanted.

            – jwm
            6 hours ago





            I was thinking the awk approach too. Should you constrain your print to lines beginning with "pass2:"? The OP didn't provide sufficient detail, but I can imagine that a naive pattern match would not be quite what was wanted.

            – jwm
            6 hours ago













            Python can read from standard in, though perl specializes in this kind of text processing if you’re moving into higher level scripting languages.

            – D. Ben Knoble
            3 hours ago





            Python can read from standard in, though perl specializes in this kind of text processing if you’re moving into higher level scripting languages.

            – D. Ben Knoble
            3 hours ago

















            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%2f507188%2fextract-substring-according-to-regexp-with-sed-or-grep%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

            getting Checkpoint VPN SSL Network Extender working in the command lineHow to connect to CheckPoint VPN on Ubuntu 18.04LTS?Will the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayVPN SSL Network Extender in FirefoxLinux Checkpoint SNX tool configuration issuesCheck Point - Connect under Linux - snx + OTPSNX VPN Ububuntu 18.XXUsing Checkpoint VPN SSL Network Extender CLI with certificateVPN with network manager (nm-applet) is not workingWill the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayImport VPN config files to NetworkManager from command lineTrouble connecting to VPN using network-manager, while command line worksStart a VPN connection with PPTP protocol on command linestarting a docker service daemon breaks the vpn networkCan't connect to vpn with Network-managerVPN SSL Network Extender in FirefoxUsing Checkpoint VPN SSL Network Extender CLI with certificate

            Cannot Extend partition with GParted The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsCan't increase partition size with GParted?GParted doesn't recognize the unallocated space after my current partitionWhat is the best way to add unallocated space located before to Ubuntu 12.04 partition with GParted live?I can't figure out how to extend my Arch home partition into free spaceGparted Linux Mint 18.1 issueTrying to extend but swap partition is showing as Unknown in Gparted, shows proper from fdiskRearrange partitions in gparted to extend a partitionUnable to extend partition even though unallocated space is next to it using GPartedAllocate free space to root partitiongparted: how to merge unallocated space with a partition

            Marilyn Monroe Ny fiainany manokana | Jereo koa | Meny fitetezanafanitarana azy.