RegEx - How to extract second matches using grepHow to make grep display only the matching regexp?Using Modifiers of Perl-compatible Regex (PCRE) in grepUsing grep with regexgrep single pattern having more than one matchesgrep regex pattern causes unexpected command behaviorScript to extract text using grepHow can I use the grep command to extract line which is the next line of greping line?How do I use grep to generateHow to grep two numbers from the same line at different places using bash?couldn't understand output of grep with regular expression while using pipe

Why does Deadpool say "You're welcome, Canada," after shooting Ryan Reynolds in the end credits?

Virginia employer terminated employee and wants signing bonus returned

Aliens englobed the Solar System: will we notice?

What are some noteworthy "mic-drop" moments in math?

They call me Inspector Morse

Regex for certain words causes Spaces

Why is Beresheet doing a only a one-way trip?

"One can do his homework in the library"

How do you like my writing?

Make a transparent 448*448 image

Time travel short story where dinosaur doesn't taste like chicken

Is it true that real estate prices mainly go up?

Word for a person who has no opinion about whether god exists

Is "history" a male-biased word ("his+story")?

Set and print content of environment variable in cmd.exe subshell?

Could you please stop shuffling the deck and play already?

infinitive telling the purpose

What does a stand alone "T" index value do?

Good for you! in Russian

Offered promotion but I'm leaving. Should I tell?

Should I take out a loan for a friend to invest on my behalf?

Who deserves to be first and second author? PhD student who collected data, research associate who wrote the paper or supervisor?

What wound would be of little consequence to a biped but terrible for a quadruped?

Best approach to update all entries in a list that is paginated?



RegEx - How to extract second matches using grep


How to make grep display only the matching regexp?Using Modifiers of Perl-compatible Regex (PCRE) in grepUsing grep with regexgrep single pattern having more than one matchesgrep regex pattern causes unexpected command behaviorScript to extract text using grepHow can I use the grep command to extract line which is the next line of greping line?How do I use grep to generateHow to grep two numbers from the same line at different places using bash?couldn't understand output of grep with regular expression while using pipe













0















How to extract second numbers in this text using grep command in ubuntu.



filename : Results.html?owner=12345



14219,98.2,31-s,19225
39219,92.7,31-s,29225


ouput desired :



12345:98.2
12345:92.7


Thanks










share|improve this question









New contributor




H.Otmane is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Do you really need a regex for this? something like cut -d, -f2 file would be simpler

    – steeldriver
    1 hour ago











  • yes im really need a regex in grep, because i have multiple files and by grep its a good for me because i get ouput with file name like this : file1:98.2

    – H.Otmane
    1 hour ago
















0















How to extract second numbers in this text using grep command in ubuntu.



filename : Results.html?owner=12345



14219,98.2,31-s,19225
39219,92.7,31-s,29225


ouput desired :



12345:98.2
12345:92.7


Thanks










share|improve this question









New contributor




H.Otmane is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Do you really need a regex for this? something like cut -d, -f2 file would be simpler

    – steeldriver
    1 hour ago











  • yes im really need a regex in grep, because i have multiple files and by grep its a good for me because i get ouput with file name like this : file1:98.2

    – H.Otmane
    1 hour ago














0












0








0








How to extract second numbers in this text using grep command in ubuntu.



filename : Results.html?owner=12345



14219,98.2,31-s,19225
39219,92.7,31-s,29225


ouput desired :



12345:98.2
12345:92.7


Thanks










share|improve this question









New contributor




H.Otmane is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












How to extract second numbers in this text using grep command in ubuntu.



filename : Results.html?owner=12345



14219,98.2,31-s,19225
39219,92.7,31-s,29225


ouput desired :



12345:98.2
12345:92.7


Thanks







grep regex






share|improve this question









New contributor




H.Otmane is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




H.Otmane is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 45 mins ago







H.Otmane













New contributor




H.Otmane is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 1 hour ago









H.OtmaneH.Otmane

52




52




New contributor




H.Otmane is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





H.Otmane is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






H.Otmane is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Do you really need a regex for this? something like cut -d, -f2 file would be simpler

    – steeldriver
    1 hour ago











  • yes im really need a regex in grep, because i have multiple files and by grep its a good for me because i get ouput with file name like this : file1:98.2

    – H.Otmane
    1 hour ago


















  • Do you really need a regex for this? something like cut -d, -f2 file would be simpler

    – steeldriver
    1 hour ago











  • yes im really need a regex in grep, because i have multiple files and by grep its a good for me because i get ouput with file name like this : file1:98.2

    – H.Otmane
    1 hour ago

















Do you really need a regex for this? something like cut -d, -f2 file would be simpler

– steeldriver
1 hour ago





Do you really need a regex for this? something like cut -d, -f2 file would be simpler

– steeldriver
1 hour ago













yes im really need a regex in grep, because i have multiple files and by grep its a good for me because i get ouput with file name like this : file1:98.2

– H.Otmane
1 hour ago






yes im really need a regex in grep, because i have multiple files and by grep its a good for me because i get ouput with file name like this : file1:98.2

– H.Otmane
1 hour ago











2 Answers
2






active

oldest

votes


















1














With GNU grep, you can switch to PCRE mode where you can use lookarounds to match a number surrounded by commas:



$ grep -HPo '(?<=,)[0-9.]*(?=,)' file
file:98.2
file:92.7


OTOH if your data are comma-delimited a better option might be Awk:



$ awk -F, 'print FILENAME ":" $2' file
file:98.2
file:92.7


or equivalently in perl perl -F, -nlE 'say "$ARGV:$F[1]"' file




To split the filename in Awk you can use



$ awk -F, 'split(FILENAME,a,/=/); print a[2] ":" $2' Results.html?owner=12345 
12345:98.2
12345:92.7


or (slightly more efficiently) if you have GNU Awk



gawk -F, 'BEGINFILEsplit(FILENAME,a,/=/) print a[2] ":" $2' Results.html?owner=12345





share|improve this answer

























  • Thanks mr @steeldriver its working fine

    – H.Otmane
    55 mins ago











  • thanks awk its best, please my filename like this : Results.html?owner=12345 i want get ouput like this : 12345:98.2

    – H.Otmane
    50 mins ago











  • Edited :) thanks for your answer

    – H.Otmane
    44 mins ago











  • OMG i think isnt possible, but its possible with you mr steeldriver :) Thanks a lot

    – H.Otmane
    31 mins ago


















0














According to this thread, there is not a right way to achieve that using RegEx and grep.



In your case you can retrieve the desired second column of the input data you provided, using awk or sed. An example using awk is presented bellow:



#assuming test.dat contains your data
awk -F"," ' print $2 '

#assuming you want to read data from stdout
echo "14219,98.2,31-s,19225" | awk -F"," ' print $2 '





share|improve this answer






















    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "89"
    ;
    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: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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
    );



    );






    H.Otmane is a new contributor. Be nice, and check out our Code of Conduct.









    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1125160%2fregex-how-to-extract-second-matches-using-grep%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














    With GNU grep, you can switch to PCRE mode where you can use lookarounds to match a number surrounded by commas:



    $ grep -HPo '(?<=,)[0-9.]*(?=,)' file
    file:98.2
    file:92.7


    OTOH if your data are comma-delimited a better option might be Awk:



    $ awk -F, 'print FILENAME ":" $2' file
    file:98.2
    file:92.7


    or equivalently in perl perl -F, -nlE 'say "$ARGV:$F[1]"' file




    To split the filename in Awk you can use



    $ awk -F, 'split(FILENAME,a,/=/); print a[2] ":" $2' Results.html?owner=12345 
    12345:98.2
    12345:92.7


    or (slightly more efficiently) if you have GNU Awk



    gawk -F, 'BEGINFILEsplit(FILENAME,a,/=/) print a[2] ":" $2' Results.html?owner=12345





    share|improve this answer

























    • Thanks mr @steeldriver its working fine

      – H.Otmane
      55 mins ago











    • thanks awk its best, please my filename like this : Results.html?owner=12345 i want get ouput like this : 12345:98.2

      – H.Otmane
      50 mins ago











    • Edited :) thanks for your answer

      – H.Otmane
      44 mins ago











    • OMG i think isnt possible, but its possible with you mr steeldriver :) Thanks a lot

      – H.Otmane
      31 mins ago















    1














    With GNU grep, you can switch to PCRE mode where you can use lookarounds to match a number surrounded by commas:



    $ grep -HPo '(?<=,)[0-9.]*(?=,)' file
    file:98.2
    file:92.7


    OTOH if your data are comma-delimited a better option might be Awk:



    $ awk -F, 'print FILENAME ":" $2' file
    file:98.2
    file:92.7


    or equivalently in perl perl -F, -nlE 'say "$ARGV:$F[1]"' file




    To split the filename in Awk you can use



    $ awk -F, 'split(FILENAME,a,/=/); print a[2] ":" $2' Results.html?owner=12345 
    12345:98.2
    12345:92.7


    or (slightly more efficiently) if you have GNU Awk



    gawk -F, 'BEGINFILEsplit(FILENAME,a,/=/) print a[2] ":" $2' Results.html?owner=12345





    share|improve this answer

























    • Thanks mr @steeldriver its working fine

      – H.Otmane
      55 mins ago











    • thanks awk its best, please my filename like this : Results.html?owner=12345 i want get ouput like this : 12345:98.2

      – H.Otmane
      50 mins ago











    • Edited :) thanks for your answer

      – H.Otmane
      44 mins ago











    • OMG i think isnt possible, but its possible with you mr steeldriver :) Thanks a lot

      – H.Otmane
      31 mins ago













    1












    1








    1







    With GNU grep, you can switch to PCRE mode where you can use lookarounds to match a number surrounded by commas:



    $ grep -HPo '(?<=,)[0-9.]*(?=,)' file
    file:98.2
    file:92.7


    OTOH if your data are comma-delimited a better option might be Awk:



    $ awk -F, 'print FILENAME ":" $2' file
    file:98.2
    file:92.7


    or equivalently in perl perl -F, -nlE 'say "$ARGV:$F[1]"' file




    To split the filename in Awk you can use



    $ awk -F, 'split(FILENAME,a,/=/); print a[2] ":" $2' Results.html?owner=12345 
    12345:98.2
    12345:92.7


    or (slightly more efficiently) if you have GNU Awk



    gawk -F, 'BEGINFILEsplit(FILENAME,a,/=/) print a[2] ":" $2' Results.html?owner=12345





    share|improve this answer















    With GNU grep, you can switch to PCRE mode where you can use lookarounds to match a number surrounded by commas:



    $ grep -HPo '(?<=,)[0-9.]*(?=,)' file
    file:98.2
    file:92.7


    OTOH if your data are comma-delimited a better option might be Awk:



    $ awk -F, 'print FILENAME ":" $2' file
    file:98.2
    file:92.7


    or equivalently in perl perl -F, -nlE 'say "$ARGV:$F[1]"' file




    To split the filename in Awk you can use



    $ awk -F, 'split(FILENAME,a,/=/); print a[2] ":" $2' Results.html?owner=12345 
    12345:98.2
    12345:92.7


    or (slightly more efficiently) if you have GNU Awk



    gawk -F, 'BEGINFILEsplit(FILENAME,a,/=/) print a[2] ":" $2' Results.html?owner=12345






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 36 mins ago

























    answered 59 mins ago









    steeldriversteeldriver

    68.9k11113184




    68.9k11113184












    • Thanks mr @steeldriver its working fine

      – H.Otmane
      55 mins ago











    • thanks awk its best, please my filename like this : Results.html?owner=12345 i want get ouput like this : 12345:98.2

      – H.Otmane
      50 mins ago











    • Edited :) thanks for your answer

      – H.Otmane
      44 mins ago











    • OMG i think isnt possible, but its possible with you mr steeldriver :) Thanks a lot

      – H.Otmane
      31 mins ago

















    • Thanks mr @steeldriver its working fine

      – H.Otmane
      55 mins ago











    • thanks awk its best, please my filename like this : Results.html?owner=12345 i want get ouput like this : 12345:98.2

      – H.Otmane
      50 mins ago











    • Edited :) thanks for your answer

      – H.Otmane
      44 mins ago











    • OMG i think isnt possible, but its possible with you mr steeldriver :) Thanks a lot

      – H.Otmane
      31 mins ago
















    Thanks mr @steeldriver its working fine

    – H.Otmane
    55 mins ago





    Thanks mr @steeldriver its working fine

    – H.Otmane
    55 mins ago













    thanks awk its best, please my filename like this : Results.html?owner=12345 i want get ouput like this : 12345:98.2

    – H.Otmane
    50 mins ago





    thanks awk its best, please my filename like this : Results.html?owner=12345 i want get ouput like this : 12345:98.2

    – H.Otmane
    50 mins ago













    Edited :) thanks for your answer

    – H.Otmane
    44 mins ago





    Edited :) thanks for your answer

    – H.Otmane
    44 mins ago













    OMG i think isnt possible, but its possible with you mr steeldriver :) Thanks a lot

    – H.Otmane
    31 mins ago





    OMG i think isnt possible, but its possible with you mr steeldriver :) Thanks a lot

    – H.Otmane
    31 mins ago













    0














    According to this thread, there is not a right way to achieve that using RegEx and grep.



    In your case you can retrieve the desired second column of the input data you provided, using awk or sed. An example using awk is presented bellow:



    #assuming test.dat contains your data
    awk -F"," ' print $2 '

    #assuming you want to read data from stdout
    echo "14219,98.2,31-s,19225" | awk -F"," ' print $2 '





    share|improve this answer



























      0














      According to this thread, there is not a right way to achieve that using RegEx and grep.



      In your case you can retrieve the desired second column of the input data you provided, using awk or sed. An example using awk is presented bellow:



      #assuming test.dat contains your data
      awk -F"," ' print $2 '

      #assuming you want to read data from stdout
      echo "14219,98.2,31-s,19225" | awk -F"," ' print $2 '





      share|improve this answer

























        0












        0








        0







        According to this thread, there is not a right way to achieve that using RegEx and grep.



        In your case you can retrieve the desired second column of the input data you provided, using awk or sed. An example using awk is presented bellow:



        #assuming test.dat contains your data
        awk -F"," ' print $2 '

        #assuming you want to read data from stdout
        echo "14219,98.2,31-s,19225" | awk -F"," ' print $2 '





        share|improve this answer













        According to this thread, there is not a right way to achieve that using RegEx and grep.



        In your case you can retrieve the desired second column of the input data you provided, using awk or sed. An example using awk is presented bellow:



        #assuming test.dat contains your data
        awk -F"," ' print $2 '

        #assuming you want to read data from stdout
        echo "14219,98.2,31-s,19225" | awk -F"," ' print $2 '






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 45 mins ago









        SarrimanSarriman

        7317




        7317




















            H.Otmane is a new contributor. Be nice, and check out our Code of Conduct.









            draft saved

            draft discarded


















            H.Otmane is a new contributor. Be nice, and check out our Code of Conduct.












            H.Otmane is a new contributor. Be nice, and check out our Code of Conduct.











            H.Otmane is a new contributor. Be nice, and check out our Code of Conduct.














            Thanks for contributing an answer to Ask Ubuntu!


            • 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%2faskubuntu.com%2fquestions%2f1125160%2fregex-how-to-extract-second-matches-using-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

            Are there any comparative studies done between Ashtavakra Gita and Buddhim?How is it wrong to believe that a self exists, or that it doesn't?Can you criticise or improve Ven. Bodhi's description of MahayanaWas the doctrine of 'Anatta', accepted as doctrine by modern Buddhism, actually taught by the Buddha?Relationship between Buddhism, Hinduism and Yoga?Comparison of Nirvana, Tao and Brahman/AtmaIs there a distinction between “ego identity” and “craving/hating”?Are there many differences between Taoism and Buddhism?Loss of “faith” in buddhismSimilarity between creation in Abrahamic religions and beginning of life in Earth mentioned Agganna Sutta?Are there studies about the difference between meditating in the morning versus in the evening?Can one follow Hinduism and Buddhism at the same time?Are there any prohibitions on participating in other religion's practices?Psychology of 'flow'

            Where else does the Shulchan Aruch quote an authority by name?Parashat Metzora+HagadolPesach/PassoverShulchan Aruch UTF-8Anonymous glosses in the Shulchan AruchWhy is the Shulchan Aruch definitive?Siman 32, Kitzur Shulchan Aruch: UntranslatedLitvaks/Yeshivish and Shulchan AruchBuying a Shulchan AruchEnglish version of SHULCHAN ARUCHIs there any place where Shulchan Aruch rules with the Rosh against the Rif and Rambam?Are there practices where Sepharadim do not hold by Shulchan Aruch?5th part of the shulchan aruch

            fallocate: fallocate failed: Text file busy in Ubuntu 17.04? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)defragmenting and increasing performance of old lubuntu system with swap partitionIssue with increasing the root partition from the swapthis /usr/bin/dpkg returned error || ubuntu-16.04, 64bitDefault 17.04 swap file locationHow to Resize Ubuntu 17.04 Zesty Swap file size?Ubuntu freezes from online formsMy Laptop is not starting after upgrade ubuntu 16.04 (Kernel 4.8.0-38 to 04.10.0-36)hcp: ERROR: FALLOCATE FAILED!Not sure my swap is being usedWine 3.0 asking for more virtual free swap