Extract a number in a txt file by using regular expressions

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








up vote
5
down vote

favorite












I am saving the output of terminal by 2>&1 | tee ./
results.txt
in a .txt file which has the following text:



executing: ./home/images/image-001-041.png
0,33, /results/image-001-041.png
1.7828,32, /results/image-001-040.png
1.86051,34, /results/image-001-042.png
1.90462,31, /results/image-001-039.png
1.90954,30, /results/image-001-038.png
1.91953,35, /results/image-001-043.png
1.92677,28, /results/image-001-036.png
1.92723,3160, /results/image-037-035.png
1.93353,7450, /results/image-086-035.png
1.93375,1600, /results/image-019-044.png


I need to take the second numbers (after first comma sign, i.e. 33,32,34,...) and save it in a list in Python. What is the bash command, or the regular expression command in python?
Thanks










share|improve this question





















  • awk -F ',' 'print $2' results.txt
    – stumblebee
    Apr 8 at 8:01










  • @EliahKagan I had intended to put an answer but got interrupted. By the time I posted, there were already a couple answers there so I decided to leave it as a helpful comment. I appreciate the insight and nudge.
    – stumblebee
    Apr 8 at 19:35







  • 2




    @stumblebee Who said having couple answers is an issue ? Ask Ubuntu isn't a race, it's a marathon and all about usefulness of answers. I see you posted one already. Good job !
    – Sergiy Kolodyazhnyy
    Apr 8 at 19:44















up vote
5
down vote

favorite












I am saving the output of terminal by 2>&1 | tee ./
results.txt
in a .txt file which has the following text:



executing: ./home/images/image-001-041.png
0,33, /results/image-001-041.png
1.7828,32, /results/image-001-040.png
1.86051,34, /results/image-001-042.png
1.90462,31, /results/image-001-039.png
1.90954,30, /results/image-001-038.png
1.91953,35, /results/image-001-043.png
1.92677,28, /results/image-001-036.png
1.92723,3160, /results/image-037-035.png
1.93353,7450, /results/image-086-035.png
1.93375,1600, /results/image-019-044.png


I need to take the second numbers (after first comma sign, i.e. 33,32,34,...) and save it in a list in Python. What is the bash command, or the regular expression command in python?
Thanks










share|improve this question





















  • awk -F ',' 'print $2' results.txt
    – stumblebee
    Apr 8 at 8:01










  • @EliahKagan I had intended to put an answer but got interrupted. By the time I posted, there were already a couple answers there so I decided to leave it as a helpful comment. I appreciate the insight and nudge.
    – stumblebee
    Apr 8 at 19:35







  • 2




    @stumblebee Who said having couple answers is an issue ? Ask Ubuntu isn't a race, it's a marathon and all about usefulness of answers. I see you posted one already. Good job !
    – Sergiy Kolodyazhnyy
    Apr 8 at 19:44













up vote
5
down vote

favorite









up vote
5
down vote

favorite











I am saving the output of terminal by 2>&1 | tee ./
results.txt
in a .txt file which has the following text:



executing: ./home/images/image-001-041.png
0,33, /results/image-001-041.png
1.7828,32, /results/image-001-040.png
1.86051,34, /results/image-001-042.png
1.90462,31, /results/image-001-039.png
1.90954,30, /results/image-001-038.png
1.91953,35, /results/image-001-043.png
1.92677,28, /results/image-001-036.png
1.92723,3160, /results/image-037-035.png
1.93353,7450, /results/image-086-035.png
1.93375,1600, /results/image-019-044.png


I need to take the second numbers (after first comma sign, i.e. 33,32,34,...) and save it in a list in Python. What is the bash command, or the regular expression command in python?
Thanks










share|improve this question













I am saving the output of terminal by 2>&1 | tee ./
results.txt
in a .txt file which has the following text:



executing: ./home/images/image-001-041.png
0,33, /results/image-001-041.png
1.7828,32, /results/image-001-040.png
1.86051,34, /results/image-001-042.png
1.90462,31, /results/image-001-039.png
1.90954,30, /results/image-001-038.png
1.91953,35, /results/image-001-043.png
1.92677,28, /results/image-001-036.png
1.92723,3160, /results/image-037-035.png
1.93353,7450, /results/image-086-035.png
1.93375,1600, /results/image-019-044.png


I need to take the second numbers (after first comma sign, i.e. 33,32,34,...) and save it in a list in Python. What is the bash command, or the regular expression command in python?
Thanks







command-line bash python text-processing






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Apr 8 at 7:37









sc241

506




506











  • awk -F ',' 'print $2' results.txt
    – stumblebee
    Apr 8 at 8:01










  • @EliahKagan I had intended to put an answer but got interrupted. By the time I posted, there were already a couple answers there so I decided to leave it as a helpful comment. I appreciate the insight and nudge.
    – stumblebee
    Apr 8 at 19:35







  • 2




    @stumblebee Who said having couple answers is an issue ? Ask Ubuntu isn't a race, it's a marathon and all about usefulness of answers. I see you posted one already. Good job !
    – Sergiy Kolodyazhnyy
    Apr 8 at 19:44

















  • awk -F ',' 'print $2' results.txt
    – stumblebee
    Apr 8 at 8:01










  • @EliahKagan I had intended to put an answer but got interrupted. By the time I posted, there were already a couple answers there so I decided to leave it as a helpful comment. I appreciate the insight and nudge.
    – stumblebee
    Apr 8 at 19:35







  • 2




    @stumblebee Who said having couple answers is an issue ? Ask Ubuntu isn't a race, it's a marathon and all about usefulness of answers. I see you posted one already. Good job !
    – Sergiy Kolodyazhnyy
    Apr 8 at 19:44
















awk -F ',' 'print $2' results.txt
– stumblebee
Apr 8 at 8:01




awk -F ',' 'print $2' results.txt
– stumblebee
Apr 8 at 8:01












@EliahKagan I had intended to put an answer but got interrupted. By the time I posted, there were already a couple answers there so I decided to leave it as a helpful comment. I appreciate the insight and nudge.
– stumblebee
Apr 8 at 19:35





@EliahKagan I had intended to put an answer but got interrupted. By the time I posted, there were already a couple answers there so I decided to leave it as a helpful comment. I appreciate the insight and nudge.
– stumblebee
Apr 8 at 19:35





2




2




@stumblebee Who said having couple answers is an issue ? Ask Ubuntu isn't a race, it's a marathon and all about usefulness of answers. I see you posted one already. Good job !
– Sergiy Kolodyazhnyy
Apr 8 at 19:44





@stumblebee Who said having couple answers is an issue ? Ask Ubuntu isn't a race, it's a marathon and all about usefulness of answers. I see you posted one already. Good job !
– Sergiy Kolodyazhnyy
Apr 8 at 19:44











5 Answers
5






active

oldest

votes

















up vote
11
down vote



accepted










Using cut:



cut -sd',' -f2 < result.txt


from man cut:



-d, --delimiter=DELIM
use DELIM instead of TAB for field delimiter
-s, --only-delimited
do not print lines not containing delimiters
-f, --fields=LIST
select only these fields; also print any line that contains
no delimiter character, unless the -s option is specified





share|improve this answer





























    up vote
    6
    down vote













    You could use awk



    awk -F ',' 'print $2' results.txt


    Define a comma as the field separator and print the second column.






    share|improve this answer





























      up vote
      5
      down vote













      Example with sed



      $ sed -rn 's/[^,]+,([^,]+),.*/1/p' results.txt
      33
      32
      34
      31
      30
      35
      28
      3160
      7450
      1600


      Notes




      • -n don't print anything until we ask for it (removes non-matching lines)


      • -r use ERE (so we don't need backslashes for + and ( ) metacharacters)


      • [^,]+, some non-commas followed by a comma


      • ([^,]+), save some non-commas followed by a comma for later (we only want this part)


      • .* any number of any characters (gets rid of the rest of the line)


      • 1 the pattern we saved


      • p print the lines we changed (needed with -n)





      share|improve this answer



























        up vote
        3
        down vote













        Since you mention Python:



        with open('results.txt') as results:
        ids = [int(line.split(',')[1]) for line in results if ',' in line]
        print(ids)


        It creates a list of integers as ids, and displays it:



        [33, 32, 34, 31, 30, 35, 28, 3160, 7450, 1600]





        share|improve this answer






















        • One may also turn the output into a string to print on stdout for further processing, since having list (with brackets and commas) can be undesirable. Use "n".join(ids) for printing each item on separate line or " ".join(ids) for space-separated list.
          – Sergiy Kolodyazhnyy
          Apr 8 at 19:40










        • @SergiyKolodyazhnyy: OP specifically mentioned that the ids should be saved as a list in python. That's what the above code does. Once its in a list, in can be processed as you wish.
          – Eric Duminil
          Apr 9 at 6:32










        • Ah, missed that part. Typically these types of questions ask to just extract the data and that's it. Don't worry, you've got a good answer there.
          – Sergiy Kolodyazhnyy
          Apr 9 at 7:48

















        up vote
        2
        down vote













        You can use Perl which is similar to the awk and sed solutions posted.



        -a enables automatic splitting on each line.



        -F is used to specify the delimiter to split each line. It defaults to ' '. Then the result is stored in @F. Hence $F[1] gives us the second column.



        -l makes sure a newline is added to each line.



        -e is used to specify the command we need to execute on each line which is print



        $ perl -F, -ale 'print $F[1]' results.txt
        33
        32
        34
        31
        30
        35
        28
        3160
        7450
        1600


        The above expands to the below program :



        $ perl -MO=Deparse -F, -ale 'print $F[1]' results.txt
        BEGIN $/ = "n"; $ = "n";
        LINE: while (defined($_ = readline ARGV))
        chomp $_;
        our @F = split(/,/, $_, 0);
        print $F[1];

        -e syntax OK





        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',
          convertImagesToLinks: true,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          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%2faskubuntu.com%2fquestions%2f1023000%2fextract-a-number-in-a-txt-file-by-using-regular-expressions%23new-answer', 'question_page');

          );

          Post as a guest






























          5 Answers
          5






          active

          oldest

          votes








          5 Answers
          5






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          11
          down vote



          accepted










          Using cut:



          cut -sd',' -f2 < result.txt


          from man cut:



          -d, --delimiter=DELIM
          use DELIM instead of TAB for field delimiter
          -s, --only-delimited
          do not print lines not containing delimiters
          -f, --fields=LIST
          select only these fields; also print any line that contains
          no delimiter character, unless the -s option is specified





          share|improve this answer


























            up vote
            11
            down vote



            accepted










            Using cut:



            cut -sd',' -f2 < result.txt


            from man cut:



            -d, --delimiter=DELIM
            use DELIM instead of TAB for field delimiter
            -s, --only-delimited
            do not print lines not containing delimiters
            -f, --fields=LIST
            select only these fields; also print any line that contains
            no delimiter character, unless the -s option is specified





            share|improve this answer
























              up vote
              11
              down vote



              accepted







              up vote
              11
              down vote



              accepted






              Using cut:



              cut -sd',' -f2 < result.txt


              from man cut:



              -d, --delimiter=DELIM
              use DELIM instead of TAB for field delimiter
              -s, --only-delimited
              do not print lines not containing delimiters
              -f, --fields=LIST
              select only these fields; also print any line that contains
              no delimiter character, unless the -s option is specified





              share|improve this answer














              Using cut:



              cut -sd',' -f2 < result.txt


              from man cut:



              -d, --delimiter=DELIM
              use DELIM instead of TAB for field delimiter
              -s, --only-delimited
              do not print lines not containing delimiters
              -f, --fields=LIST
              select only these fields; also print any line that contains
              no delimiter character, unless the -s option is specified






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Apr 9 at 6:59

























              answered Apr 8 at 8:16









              αғsнιη

              23.4k2191152




              23.4k2191152






















                  up vote
                  6
                  down vote













                  You could use awk



                  awk -F ',' 'print $2' results.txt


                  Define a comma as the field separator and print the second column.






                  share|improve this answer


























                    up vote
                    6
                    down vote













                    You could use awk



                    awk -F ',' 'print $2' results.txt


                    Define a comma as the field separator and print the second column.






                    share|improve this answer
























                      up vote
                      6
                      down vote










                      up vote
                      6
                      down vote









                      You could use awk



                      awk -F ',' 'print $2' results.txt


                      Define a comma as the field separator and print the second column.






                      share|improve this answer














                      You could use awk



                      awk -F ',' 'print $2' results.txt


                      Define a comma as the field separator and print the second column.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Apr 8 at 19:36

























                      answered Apr 8 at 8:19









                      stumblebee

                      2,3083922




                      2,3083922




















                          up vote
                          5
                          down vote













                          Example with sed



                          $ sed -rn 's/[^,]+,([^,]+),.*/1/p' results.txt
                          33
                          32
                          34
                          31
                          30
                          35
                          28
                          3160
                          7450
                          1600


                          Notes




                          • -n don't print anything until we ask for it (removes non-matching lines)


                          • -r use ERE (so we don't need backslashes for + and ( ) metacharacters)


                          • [^,]+, some non-commas followed by a comma


                          • ([^,]+), save some non-commas followed by a comma for later (we only want this part)


                          • .* any number of any characters (gets rid of the rest of the line)


                          • 1 the pattern we saved


                          • p print the lines we changed (needed with -n)





                          share|improve this answer
























                            up vote
                            5
                            down vote













                            Example with sed



                            $ sed -rn 's/[^,]+,([^,]+),.*/1/p' results.txt
                            33
                            32
                            34
                            31
                            30
                            35
                            28
                            3160
                            7450
                            1600


                            Notes




                            • -n don't print anything until we ask for it (removes non-matching lines)


                            • -r use ERE (so we don't need backslashes for + and ( ) metacharacters)


                            • [^,]+, some non-commas followed by a comma


                            • ([^,]+), save some non-commas followed by a comma for later (we only want this part)


                            • .* any number of any characters (gets rid of the rest of the line)


                            • 1 the pattern we saved


                            • p print the lines we changed (needed with -n)





                            share|improve this answer






















                              up vote
                              5
                              down vote










                              up vote
                              5
                              down vote









                              Example with sed



                              $ sed -rn 's/[^,]+,([^,]+),.*/1/p' results.txt
                              33
                              32
                              34
                              31
                              30
                              35
                              28
                              3160
                              7450
                              1600


                              Notes




                              • -n don't print anything until we ask for it (removes non-matching lines)


                              • -r use ERE (so we don't need backslashes for + and ( ) metacharacters)


                              • [^,]+, some non-commas followed by a comma


                              • ([^,]+), save some non-commas followed by a comma for later (we only want this part)


                              • .* any number of any characters (gets rid of the rest of the line)


                              • 1 the pattern we saved


                              • p print the lines we changed (needed with -n)





                              share|improve this answer












                              Example with sed



                              $ sed -rn 's/[^,]+,([^,]+),.*/1/p' results.txt
                              33
                              32
                              34
                              31
                              30
                              35
                              28
                              3160
                              7450
                              1600


                              Notes




                              • -n don't print anything until we ask for it (removes non-matching lines)


                              • -r use ERE (so we don't need backslashes for + and ( ) metacharacters)


                              • [^,]+, some non-commas followed by a comma


                              • ([^,]+), save some non-commas followed by a comma for later (we only want this part)


                              • .* any number of any characters (gets rid of the rest of the line)


                              • 1 the pattern we saved


                              • p print the lines we changed (needed with -n)






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Apr 8 at 8:12









                              Zanna

                              48k13119228




                              48k13119228




















                                  up vote
                                  3
                                  down vote













                                  Since you mention Python:



                                  with open('results.txt') as results:
                                  ids = [int(line.split(',')[1]) for line in results if ',' in line]
                                  print(ids)


                                  It creates a list of integers as ids, and displays it:



                                  [33, 32, 34, 31, 30, 35, 28, 3160, 7450, 1600]





                                  share|improve this answer






















                                  • One may also turn the output into a string to print on stdout for further processing, since having list (with brackets and commas) can be undesirable. Use "n".join(ids) for printing each item on separate line or " ".join(ids) for space-separated list.
                                    – Sergiy Kolodyazhnyy
                                    Apr 8 at 19:40










                                  • @SergiyKolodyazhnyy: OP specifically mentioned that the ids should be saved as a list in python. That's what the above code does. Once its in a list, in can be processed as you wish.
                                    – Eric Duminil
                                    Apr 9 at 6:32










                                  • Ah, missed that part. Typically these types of questions ask to just extract the data and that's it. Don't worry, you've got a good answer there.
                                    – Sergiy Kolodyazhnyy
                                    Apr 9 at 7:48














                                  up vote
                                  3
                                  down vote













                                  Since you mention Python:



                                  with open('results.txt') as results:
                                  ids = [int(line.split(',')[1]) for line in results if ',' in line]
                                  print(ids)


                                  It creates a list of integers as ids, and displays it:



                                  [33, 32, 34, 31, 30, 35, 28, 3160, 7450, 1600]





                                  share|improve this answer






















                                  • One may also turn the output into a string to print on stdout for further processing, since having list (with brackets and commas) can be undesirable. Use "n".join(ids) for printing each item on separate line or " ".join(ids) for space-separated list.
                                    – Sergiy Kolodyazhnyy
                                    Apr 8 at 19:40










                                  • @SergiyKolodyazhnyy: OP specifically mentioned that the ids should be saved as a list in python. That's what the above code does. Once its in a list, in can be processed as you wish.
                                    – Eric Duminil
                                    Apr 9 at 6:32










                                  • Ah, missed that part. Typically these types of questions ask to just extract the data and that's it. Don't worry, you've got a good answer there.
                                    – Sergiy Kolodyazhnyy
                                    Apr 9 at 7:48












                                  up vote
                                  3
                                  down vote










                                  up vote
                                  3
                                  down vote









                                  Since you mention Python:



                                  with open('results.txt') as results:
                                  ids = [int(line.split(',')[1]) for line in results if ',' in line]
                                  print(ids)


                                  It creates a list of integers as ids, and displays it:



                                  [33, 32, 34, 31, 30, 35, 28, 3160, 7450, 1600]





                                  share|improve this answer














                                  Since you mention Python:



                                  with open('results.txt') as results:
                                  ids = [int(line.split(',')[1]) for line in results if ',' in line]
                                  print(ids)


                                  It creates a list of integers as ids, and displays it:



                                  [33, 32, 34, 31, 30, 35, 28, 3160, 7450, 1600]






                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited Apr 8 at 15:43

























                                  answered Apr 8 at 11:51









                                  Eric Duminil

                                  1686




                                  1686











                                  • One may also turn the output into a string to print on stdout for further processing, since having list (with brackets and commas) can be undesirable. Use "n".join(ids) for printing each item on separate line or " ".join(ids) for space-separated list.
                                    – Sergiy Kolodyazhnyy
                                    Apr 8 at 19:40










                                  • @SergiyKolodyazhnyy: OP specifically mentioned that the ids should be saved as a list in python. That's what the above code does. Once its in a list, in can be processed as you wish.
                                    – Eric Duminil
                                    Apr 9 at 6:32










                                  • Ah, missed that part. Typically these types of questions ask to just extract the data and that's it. Don't worry, you've got a good answer there.
                                    – Sergiy Kolodyazhnyy
                                    Apr 9 at 7:48
















                                  • One may also turn the output into a string to print on stdout for further processing, since having list (with brackets and commas) can be undesirable. Use "n".join(ids) for printing each item on separate line or " ".join(ids) for space-separated list.
                                    – Sergiy Kolodyazhnyy
                                    Apr 8 at 19:40










                                  • @SergiyKolodyazhnyy: OP specifically mentioned that the ids should be saved as a list in python. That's what the above code does. Once its in a list, in can be processed as you wish.
                                    – Eric Duminil
                                    Apr 9 at 6:32










                                  • Ah, missed that part. Typically these types of questions ask to just extract the data and that's it. Don't worry, you've got a good answer there.
                                    – Sergiy Kolodyazhnyy
                                    Apr 9 at 7:48















                                  One may also turn the output into a string to print on stdout for further processing, since having list (with brackets and commas) can be undesirable. Use "n".join(ids) for printing each item on separate line or " ".join(ids) for space-separated list.
                                  – Sergiy Kolodyazhnyy
                                  Apr 8 at 19:40




                                  One may also turn the output into a string to print on stdout for further processing, since having list (with brackets and commas) can be undesirable. Use "n".join(ids) for printing each item on separate line or " ".join(ids) for space-separated list.
                                  – Sergiy Kolodyazhnyy
                                  Apr 8 at 19:40












                                  @SergiyKolodyazhnyy: OP specifically mentioned that the ids should be saved as a list in python. That's what the above code does. Once its in a list, in can be processed as you wish.
                                  – Eric Duminil
                                  Apr 9 at 6:32




                                  @SergiyKolodyazhnyy: OP specifically mentioned that the ids should be saved as a list in python. That's what the above code does. Once its in a list, in can be processed as you wish.
                                  – Eric Duminil
                                  Apr 9 at 6:32












                                  Ah, missed that part. Typically these types of questions ask to just extract the data and that's it. Don't worry, you've got a good answer there.
                                  – Sergiy Kolodyazhnyy
                                  Apr 9 at 7:48




                                  Ah, missed that part. Typically these types of questions ask to just extract the data and that's it. Don't worry, you've got a good answer there.
                                  – Sergiy Kolodyazhnyy
                                  Apr 9 at 7:48










                                  up vote
                                  2
                                  down vote













                                  You can use Perl which is similar to the awk and sed solutions posted.



                                  -a enables automatic splitting on each line.



                                  -F is used to specify the delimiter to split each line. It defaults to ' '. Then the result is stored in @F. Hence $F[1] gives us the second column.



                                  -l makes sure a newline is added to each line.



                                  -e is used to specify the command we need to execute on each line which is print



                                  $ perl -F, -ale 'print $F[1]' results.txt
                                  33
                                  32
                                  34
                                  31
                                  30
                                  35
                                  28
                                  3160
                                  7450
                                  1600


                                  The above expands to the below program :



                                  $ perl -MO=Deparse -F, -ale 'print $F[1]' results.txt
                                  BEGIN $/ = "n"; $ = "n";
                                  LINE: while (defined($_ = readline ARGV))
                                  chomp $_;
                                  our @F = split(/,/, $_, 0);
                                  print $F[1];

                                  -e syntax OK





                                  share|improve this answer
























                                    up vote
                                    2
                                    down vote













                                    You can use Perl which is similar to the awk and sed solutions posted.



                                    -a enables automatic splitting on each line.



                                    -F is used to specify the delimiter to split each line. It defaults to ' '. Then the result is stored in @F. Hence $F[1] gives us the second column.



                                    -l makes sure a newline is added to each line.



                                    -e is used to specify the command we need to execute on each line which is print



                                    $ perl -F, -ale 'print $F[1]' results.txt
                                    33
                                    32
                                    34
                                    31
                                    30
                                    35
                                    28
                                    3160
                                    7450
                                    1600


                                    The above expands to the below program :



                                    $ perl -MO=Deparse -F, -ale 'print $F[1]' results.txt
                                    BEGIN $/ = "n"; $ = "n";
                                    LINE: while (defined($_ = readline ARGV))
                                    chomp $_;
                                    our @F = split(/,/, $_, 0);
                                    print $F[1];

                                    -e syntax OK





                                    share|improve this answer






















                                      up vote
                                      2
                                      down vote










                                      up vote
                                      2
                                      down vote









                                      You can use Perl which is similar to the awk and sed solutions posted.



                                      -a enables automatic splitting on each line.



                                      -F is used to specify the delimiter to split each line. It defaults to ' '. Then the result is stored in @F. Hence $F[1] gives us the second column.



                                      -l makes sure a newline is added to each line.



                                      -e is used to specify the command we need to execute on each line which is print



                                      $ perl -F, -ale 'print $F[1]' results.txt
                                      33
                                      32
                                      34
                                      31
                                      30
                                      35
                                      28
                                      3160
                                      7450
                                      1600


                                      The above expands to the below program :



                                      $ perl -MO=Deparse -F, -ale 'print $F[1]' results.txt
                                      BEGIN $/ = "n"; $ = "n";
                                      LINE: while (defined($_ = readline ARGV))
                                      chomp $_;
                                      our @F = split(/,/, $_, 0);
                                      print $F[1];

                                      -e syntax OK





                                      share|improve this answer












                                      You can use Perl which is similar to the awk and sed solutions posted.



                                      -a enables automatic splitting on each line.



                                      -F is used to specify the delimiter to split each line. It defaults to ' '. Then the result is stored in @F. Hence $F[1] gives us the second column.



                                      -l makes sure a newline is added to each line.



                                      -e is used to specify the command we need to execute on each line which is print



                                      $ perl -F, -ale 'print $F[1]' results.txt
                                      33
                                      32
                                      34
                                      31
                                      30
                                      35
                                      28
                                      3160
                                      7450
                                      1600


                                      The above expands to the below program :



                                      $ perl -MO=Deparse -F, -ale 'print $F[1]' results.txt
                                      BEGIN $/ = "n"; $ = "n";
                                      LINE: while (defined($_ = readline ARGV))
                                      chomp $_;
                                      our @F = split(/,/, $_, 0);
                                      print $F[1];

                                      -e syntax OK






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Apr 9 at 10:41









                                      Wordzilla

                                      16818




                                      16818



























                                           

                                          draft saved


                                          draft discarded















































                                           


                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1023000%2fextract-a-number-in-a-txt-file-by-using-regular-expressions%23new-answer', 'question_page');

                                          );

                                          Post as a guest













































































                                          Popular posts from this blog

                                          pylint3 and pip3 broken

                                          Missing snmpget and snmpwalk

                                          How to enroll fingerprints to Ubuntu 17.10 with VFS491