How to Sed or Grep number next to text

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











up vote
0
down vote

favorite












I have a file like the one below. I want to return the value to the right of info2. Secondly I would want to return the value of info5 in the same manner.



FILE:



info1 218, info2 111, info3 415, info5 done,






share|improve this question


















  • 3




    I think awk is a better tool for this.
    – NickD
    Jan 30 at 19:51










  • Is info1 always in the first field? Is info5 always in the fourth field? How big is the file? Are you going to do this once and then you are done? Or are you going to be repeating it? How often?
    – NickD
    Jan 30 at 19:58










  • They are not always in the same place unfortunately. I am planning to run this about once an hour for a couple weeks.
    – james
    Jan 30 at 20:09






  • 1




    Do they occur on every line (or at least on most lines)? If so, DopeGhoti's solution in a comment below should work fine. If they are very rare, then you might want to run a grep "info[25]" in the pipeline instead of his cat.
    – NickD
    Jan 30 at 20:22










  • If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" Accepting an answer also indicates to future readers that the answer actually solved the problem.
    – Kusalananda
    Feb 7 at 10:56














up vote
0
down vote

favorite












I have a file like the one below. I want to return the value to the right of info2. Secondly I would want to return the value of info5 in the same manner.



FILE:



info1 218, info2 111, info3 415, info5 done,






share|improve this question


















  • 3




    I think awk is a better tool for this.
    – NickD
    Jan 30 at 19:51










  • Is info1 always in the first field? Is info5 always in the fourth field? How big is the file? Are you going to do this once and then you are done? Or are you going to be repeating it? How often?
    – NickD
    Jan 30 at 19:58










  • They are not always in the same place unfortunately. I am planning to run this about once an hour for a couple weeks.
    – james
    Jan 30 at 20:09






  • 1




    Do they occur on every line (or at least on most lines)? If so, DopeGhoti's solution in a comment below should work fine. If they are very rare, then you might want to run a grep "info[25]" in the pipeline instead of his cat.
    – NickD
    Jan 30 at 20:22










  • If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" Accepting an answer also indicates to future readers that the answer actually solved the problem.
    – Kusalananda
    Feb 7 at 10:56












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a file like the one below. I want to return the value to the right of info2. Secondly I would want to return the value of info5 in the same manner.



FILE:



info1 218, info2 111, info3 415, info5 done,






share|improve this question














I have a file like the one below. I want to return the value to the right of info2. Secondly I would want to return the value of info5 in the same manner.



FILE:



info1 218, info2 111, info3 415, info5 done,








share|improve this question













share|improve this question




share|improve this question








edited Jan 30 at 19:50









Jeff Schaller

31.4k846105




31.4k846105










asked Jan 30 at 19:43









james

1




1







  • 3




    I think awk is a better tool for this.
    – NickD
    Jan 30 at 19:51










  • Is info1 always in the first field? Is info5 always in the fourth field? How big is the file? Are you going to do this once and then you are done? Or are you going to be repeating it? How often?
    – NickD
    Jan 30 at 19:58










  • They are not always in the same place unfortunately. I am planning to run this about once an hour for a couple weeks.
    – james
    Jan 30 at 20:09






  • 1




    Do they occur on every line (or at least on most lines)? If so, DopeGhoti's solution in a comment below should work fine. If they are very rare, then you might want to run a grep "info[25]" in the pipeline instead of his cat.
    – NickD
    Jan 30 at 20:22










  • If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" Accepting an answer also indicates to future readers that the answer actually solved the problem.
    – Kusalananda
    Feb 7 at 10:56












  • 3




    I think awk is a better tool for this.
    – NickD
    Jan 30 at 19:51










  • Is info1 always in the first field? Is info5 always in the fourth field? How big is the file? Are you going to do this once and then you are done? Or are you going to be repeating it? How often?
    – NickD
    Jan 30 at 19:58










  • They are not always in the same place unfortunately. I am planning to run this about once an hour for a couple weeks.
    – james
    Jan 30 at 20:09






  • 1




    Do they occur on every line (or at least on most lines)? If so, DopeGhoti's solution in a comment below should work fine. If they are very rare, then you might want to run a grep "info[25]" in the pipeline instead of his cat.
    – NickD
    Jan 30 at 20:22










  • If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" Accepting an answer also indicates to future readers that the answer actually solved the problem.
    – Kusalananda
    Feb 7 at 10:56







3




3




I think awk is a better tool for this.
– NickD
Jan 30 at 19:51




I think awk is a better tool for this.
– NickD
Jan 30 at 19:51












Is info1 always in the first field? Is info5 always in the fourth field? How big is the file? Are you going to do this once and then you are done? Or are you going to be repeating it? How often?
– NickD
Jan 30 at 19:58




Is info1 always in the first field? Is info5 always in the fourth field? How big is the file? Are you going to do this once and then you are done? Or are you going to be repeating it? How often?
– NickD
Jan 30 at 19:58












They are not always in the same place unfortunately. I am planning to run this about once an hour for a couple weeks.
– james
Jan 30 at 20:09




They are not always in the same place unfortunately. I am planning to run this about once an hour for a couple weeks.
– james
Jan 30 at 20:09




1




1




Do they occur on every line (or at least on most lines)? If so, DopeGhoti's solution in a comment below should work fine. If they are very rare, then you might want to run a grep "info[25]" in the pipeline instead of his cat.
– NickD
Jan 30 at 20:22




Do they occur on every line (or at least on most lines)? If so, DopeGhoti's solution in a comment below should work fine. If they are very rare, then you might want to run a grep "info[25]" in the pipeline instead of his cat.
– NickD
Jan 30 at 20:22












If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" Accepting an answer also indicates to future readers that the answer actually solved the problem.
– Kusalananda
Feb 7 at 10:56




If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" Accepting an answer also indicates to future readers that the answer actually solved the problem.
– Kusalananda
Feb 7 at 10:56










4 Answers
4






active

oldest

votes

















up vote
2
down vote













With awk:



$ awk 'BEGIN RS="," $1 ~ /info[25]$/ print $2 ' input
111
done


We set the input record separator (RS) to the comma, and then for each record in which the first field contains info followed by either a two or a five (and nothing else), print the second field.






share|improve this answer



























    up vote
    0
    down vote













    A slight improvement on choroba’s answer:



    sed -n 's/.*info2 ([^,]*).*/1/p' file



    • -n means “don’t print anything
      except when specifically commanded to do so”. 
      This is (somewhat) equivalent to choroba’s use of !d
      to delete (ignore) lines that don’t match info2.

    • The guts of the command is very similar to choroba’s,
      except it captures a sequence of any characters except commas
      (not just numerals), so it can extract the done from info5 done,.

    • The command ends with p (print),
      because otherwise, with the -n, nothing would be printed.

    This has the advantage that
    it doesn’t require specifying the infoN label twice. 
    (And it doesn’t generate undesired results
    if a line contains info20, info21, etc.)






    share|improve this answer



























      up vote
      0
      down vote













      A two step solution:



      tr ',' 'n' <file | sed -n 's/^ *info[25] *//p'


      The tr transforms the example text into



      info1 218 
      info2 111
      info3 415
      info5 done


      and the sed script removes the info2 and info5 strings (and any surrounding whitespace), leaving the wanted pieces of text. The other lines are discarded.



      Result:



      111
      done



      This is better done with awk, as in DopeGhoti's solution.



      An awk variation based on my tr approach:



      tr ',' 'n' <file | awk '/info[25]/ print $2 '





      share|improve this answer




















      • you actually don't need tr if you use awk, look at the answer by @DopeGhoti
        – Diego Roccia
        Feb 2 at 17:14










      • @DiegoRoccia I'm well aware of his answer (otherwise I would not link to it). This is a variation an my sed solution, and as you can see, it replaces his setting of RS with preprocessing the data into newline-delimited records. The two are otherwise the same.
        – Kusalananda
        Feb 2 at 18:00










      • I agree that the output is the same, but IMO adding useless complexity is not a good variation, so I'm advising not to do it
        – Diego Roccia
        Feb 9 at 9:47

















      up vote
      -1
      down vote













      You can use sed:



      sed '/info2 [0-9]/!d;s/.*info2 ([0-9]*).*/1/' file



      • /info2/ is an "address", any line containing info2 is considered to match the address. !d means "delete lines outside of the address", so only lines containing info2 will be processed by the next command.

      • The substitution uses a capture group to remember the number after info2, and the whole line (everything before info2, info2 itself, the number, and anything following the number) is replaced by the number.

      If you need to capture "done" after info5, you can't use [0-9]*, but you can replace it by [^,]*, i.e. non-comma.






      share|improve this answer






















      • ... and he wants to do the same thing with info5 (and the value there is not a number).
        – NickD
        Jan 30 at 19:50










      • @Nick (just to make the answer complete) use [^,]* in place of [0-9]*, and I agree awk is a better fit.
        – Weijun Zhou
        Jan 30 at 19:57











      • What would Awk look like and why better?
        – james
        Jan 30 at 19:59










      • This generates undesired results if a line contains info2 without actually having an info2 label; e.g., info20, info21, etc.
        – G-Man
        Jan 31 at 2:03











      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );








       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f420776%2fhow-to-sed-or-grep-number-next-to-text%23new-answer', 'question_page');

      );

      Post as a guest






























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote













      With awk:



      $ awk 'BEGIN RS="," $1 ~ /info[25]$/ print $2 ' input
      111
      done


      We set the input record separator (RS) to the comma, and then for each record in which the first field contains info followed by either a two or a five (and nothing else), print the second field.






      share|improve this answer
























        up vote
        2
        down vote













        With awk:



        $ awk 'BEGIN RS="," $1 ~ /info[25]$/ print $2 ' input
        111
        done


        We set the input record separator (RS) to the comma, and then for each record in which the first field contains info followed by either a two or a five (and nothing else), print the second field.






        share|improve this answer






















          up vote
          2
          down vote










          up vote
          2
          down vote









          With awk:



          $ awk 'BEGIN RS="," $1 ~ /info[25]$/ print $2 ' input
          111
          done


          We set the input record separator (RS) to the comma, and then for each record in which the first field contains info followed by either a two or a five (and nothing else), print the second field.






          share|improve this answer












          With awk:



          $ awk 'BEGIN RS="," $1 ~ /info[25]$/ print $2 ' input
          111
          done


          We set the input record separator (RS) to the comma, and then for each record in which the first field contains info followed by either a two or a five (and nothing else), print the second field.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 30 at 20:21









          DopeGhoti

          40.4k54879




          40.4k54879






















              up vote
              0
              down vote













              A slight improvement on choroba’s answer:



              sed -n 's/.*info2 ([^,]*).*/1/p' file



              • -n means “don’t print anything
                except when specifically commanded to do so”. 
                This is (somewhat) equivalent to choroba’s use of !d
                to delete (ignore) lines that don’t match info2.

              • The guts of the command is very similar to choroba’s,
                except it captures a sequence of any characters except commas
                (not just numerals), so it can extract the done from info5 done,.

              • The command ends with p (print),
                because otherwise, with the -n, nothing would be printed.

              This has the advantage that
              it doesn’t require specifying the infoN label twice. 
              (And it doesn’t generate undesired results
              if a line contains info20, info21, etc.)






              share|improve this answer
























                up vote
                0
                down vote













                A slight improvement on choroba’s answer:



                sed -n 's/.*info2 ([^,]*).*/1/p' file



                • -n means “don’t print anything
                  except when specifically commanded to do so”. 
                  This is (somewhat) equivalent to choroba’s use of !d
                  to delete (ignore) lines that don’t match info2.

                • The guts of the command is very similar to choroba’s,
                  except it captures a sequence of any characters except commas
                  (not just numerals), so it can extract the done from info5 done,.

                • The command ends with p (print),
                  because otherwise, with the -n, nothing would be printed.

                This has the advantage that
                it doesn’t require specifying the infoN label twice. 
                (And it doesn’t generate undesired results
                if a line contains info20, info21, etc.)






                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  A slight improvement on choroba’s answer:



                  sed -n 's/.*info2 ([^,]*).*/1/p' file



                  • -n means “don’t print anything
                    except when specifically commanded to do so”. 
                    This is (somewhat) equivalent to choroba’s use of !d
                    to delete (ignore) lines that don’t match info2.

                  • The guts of the command is very similar to choroba’s,
                    except it captures a sequence of any characters except commas
                    (not just numerals), so it can extract the done from info5 done,.

                  • The command ends with p (print),
                    because otherwise, with the -n, nothing would be printed.

                  This has the advantage that
                  it doesn’t require specifying the infoN label twice. 
                  (And it doesn’t generate undesired results
                  if a line contains info20, info21, etc.)






                  share|improve this answer












                  A slight improvement on choroba’s answer:



                  sed -n 's/.*info2 ([^,]*).*/1/p' file



                  • -n means “don’t print anything
                    except when specifically commanded to do so”. 
                    This is (somewhat) equivalent to choroba’s use of !d
                    to delete (ignore) lines that don’t match info2.

                  • The guts of the command is very similar to choroba’s,
                    except it captures a sequence of any characters except commas
                    (not just numerals), so it can extract the done from info5 done,.

                  • The command ends with p (print),
                    because otherwise, with the -n, nothing would be printed.

                  This has the advantage that
                  it doesn’t require specifying the infoN label twice. 
                  (And it doesn’t generate undesired results
                  if a line contains info20, info21, etc.)







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 31 at 2:01









                  G-Man

                  11.5k82657




                  11.5k82657




















                      up vote
                      0
                      down vote













                      A two step solution:



                      tr ',' 'n' <file | sed -n 's/^ *info[25] *//p'


                      The tr transforms the example text into



                      info1 218 
                      info2 111
                      info3 415
                      info5 done


                      and the sed script removes the info2 and info5 strings (and any surrounding whitespace), leaving the wanted pieces of text. The other lines are discarded.



                      Result:



                      111
                      done



                      This is better done with awk, as in DopeGhoti's solution.



                      An awk variation based on my tr approach:



                      tr ',' 'n' <file | awk '/info[25]/ print $2 '





                      share|improve this answer




















                      • you actually don't need tr if you use awk, look at the answer by @DopeGhoti
                        – Diego Roccia
                        Feb 2 at 17:14










                      • @DiegoRoccia I'm well aware of his answer (otherwise I would not link to it). This is a variation an my sed solution, and as you can see, it replaces his setting of RS with preprocessing the data into newline-delimited records. The two are otherwise the same.
                        – Kusalananda
                        Feb 2 at 18:00










                      • I agree that the output is the same, but IMO adding useless complexity is not a good variation, so I'm advising not to do it
                        – Diego Roccia
                        Feb 9 at 9:47














                      up vote
                      0
                      down vote













                      A two step solution:



                      tr ',' 'n' <file | sed -n 's/^ *info[25] *//p'


                      The tr transforms the example text into



                      info1 218 
                      info2 111
                      info3 415
                      info5 done


                      and the sed script removes the info2 and info5 strings (and any surrounding whitespace), leaving the wanted pieces of text. The other lines are discarded.



                      Result:



                      111
                      done



                      This is better done with awk, as in DopeGhoti's solution.



                      An awk variation based on my tr approach:



                      tr ',' 'n' <file | awk '/info[25]/ print $2 '





                      share|improve this answer




















                      • you actually don't need tr if you use awk, look at the answer by @DopeGhoti
                        – Diego Roccia
                        Feb 2 at 17:14










                      • @DiegoRoccia I'm well aware of his answer (otherwise I would not link to it). This is a variation an my sed solution, and as you can see, it replaces his setting of RS with preprocessing the data into newline-delimited records. The two are otherwise the same.
                        – Kusalananda
                        Feb 2 at 18:00










                      • I agree that the output is the same, but IMO adding useless complexity is not a good variation, so I'm advising not to do it
                        – Diego Roccia
                        Feb 9 at 9:47












                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      A two step solution:



                      tr ',' 'n' <file | sed -n 's/^ *info[25] *//p'


                      The tr transforms the example text into



                      info1 218 
                      info2 111
                      info3 415
                      info5 done


                      and the sed script removes the info2 and info5 strings (and any surrounding whitespace), leaving the wanted pieces of text. The other lines are discarded.



                      Result:



                      111
                      done



                      This is better done with awk, as in DopeGhoti's solution.



                      An awk variation based on my tr approach:



                      tr ',' 'n' <file | awk '/info[25]/ print $2 '





                      share|improve this answer












                      A two step solution:



                      tr ',' 'n' <file | sed -n 's/^ *info[25] *//p'


                      The tr transforms the example text into



                      info1 218 
                      info2 111
                      info3 415
                      info5 done


                      and the sed script removes the info2 and info5 strings (and any surrounding whitespace), leaving the wanted pieces of text. The other lines are discarded.



                      Result:



                      111
                      done



                      This is better done with awk, as in DopeGhoti's solution.



                      An awk variation based on my tr approach:



                      tr ',' 'n' <file | awk '/info[25]/ print $2 '






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Feb 2 at 15:53









                      Kusalananda

                      103k13202318




                      103k13202318











                      • you actually don't need tr if you use awk, look at the answer by @DopeGhoti
                        – Diego Roccia
                        Feb 2 at 17:14










                      • @DiegoRoccia I'm well aware of his answer (otherwise I would not link to it). This is a variation an my sed solution, and as you can see, it replaces his setting of RS with preprocessing the data into newline-delimited records. The two are otherwise the same.
                        – Kusalananda
                        Feb 2 at 18:00










                      • I agree that the output is the same, but IMO adding useless complexity is not a good variation, so I'm advising not to do it
                        – Diego Roccia
                        Feb 9 at 9:47
















                      • you actually don't need tr if you use awk, look at the answer by @DopeGhoti
                        – Diego Roccia
                        Feb 2 at 17:14










                      • @DiegoRoccia I'm well aware of his answer (otherwise I would not link to it). This is a variation an my sed solution, and as you can see, it replaces his setting of RS with preprocessing the data into newline-delimited records. The two are otherwise the same.
                        – Kusalananda
                        Feb 2 at 18:00










                      • I agree that the output is the same, but IMO adding useless complexity is not a good variation, so I'm advising not to do it
                        – Diego Roccia
                        Feb 9 at 9:47















                      you actually don't need tr if you use awk, look at the answer by @DopeGhoti
                      – Diego Roccia
                      Feb 2 at 17:14




                      you actually don't need tr if you use awk, look at the answer by @DopeGhoti
                      – Diego Roccia
                      Feb 2 at 17:14












                      @DiegoRoccia I'm well aware of his answer (otherwise I would not link to it). This is a variation an my sed solution, and as you can see, it replaces his setting of RS with preprocessing the data into newline-delimited records. The two are otherwise the same.
                      – Kusalananda
                      Feb 2 at 18:00




                      @DiegoRoccia I'm well aware of his answer (otherwise I would not link to it). This is a variation an my sed solution, and as you can see, it replaces his setting of RS with preprocessing the data into newline-delimited records. The two are otherwise the same.
                      – Kusalananda
                      Feb 2 at 18:00












                      I agree that the output is the same, but IMO adding useless complexity is not a good variation, so I'm advising not to do it
                      – Diego Roccia
                      Feb 9 at 9:47




                      I agree that the output is the same, but IMO adding useless complexity is not a good variation, so I'm advising not to do it
                      – Diego Roccia
                      Feb 9 at 9:47










                      up vote
                      -1
                      down vote













                      You can use sed:



                      sed '/info2 [0-9]/!d;s/.*info2 ([0-9]*).*/1/' file



                      • /info2/ is an "address", any line containing info2 is considered to match the address. !d means "delete lines outside of the address", so only lines containing info2 will be processed by the next command.

                      • The substitution uses a capture group to remember the number after info2, and the whole line (everything before info2, info2 itself, the number, and anything following the number) is replaced by the number.

                      If you need to capture "done" after info5, you can't use [0-9]*, but you can replace it by [^,]*, i.e. non-comma.






                      share|improve this answer






















                      • ... and he wants to do the same thing with info5 (and the value there is not a number).
                        – NickD
                        Jan 30 at 19:50










                      • @Nick (just to make the answer complete) use [^,]* in place of [0-9]*, and I agree awk is a better fit.
                        – Weijun Zhou
                        Jan 30 at 19:57











                      • What would Awk look like and why better?
                        – james
                        Jan 30 at 19:59










                      • This generates undesired results if a line contains info2 without actually having an info2 label; e.g., info20, info21, etc.
                        – G-Man
                        Jan 31 at 2:03















                      up vote
                      -1
                      down vote













                      You can use sed:



                      sed '/info2 [0-9]/!d;s/.*info2 ([0-9]*).*/1/' file



                      • /info2/ is an "address", any line containing info2 is considered to match the address. !d means "delete lines outside of the address", so only lines containing info2 will be processed by the next command.

                      • The substitution uses a capture group to remember the number after info2, and the whole line (everything before info2, info2 itself, the number, and anything following the number) is replaced by the number.

                      If you need to capture "done" after info5, you can't use [0-9]*, but you can replace it by [^,]*, i.e. non-comma.






                      share|improve this answer






















                      • ... and he wants to do the same thing with info5 (and the value there is not a number).
                        – NickD
                        Jan 30 at 19:50










                      • @Nick (just to make the answer complete) use [^,]* in place of [0-9]*, and I agree awk is a better fit.
                        – Weijun Zhou
                        Jan 30 at 19:57











                      • What would Awk look like and why better?
                        – james
                        Jan 30 at 19:59










                      • This generates undesired results if a line contains info2 without actually having an info2 label; e.g., info20, info21, etc.
                        – G-Man
                        Jan 31 at 2:03













                      up vote
                      -1
                      down vote










                      up vote
                      -1
                      down vote









                      You can use sed:



                      sed '/info2 [0-9]/!d;s/.*info2 ([0-9]*).*/1/' file



                      • /info2/ is an "address", any line containing info2 is considered to match the address. !d means "delete lines outside of the address", so only lines containing info2 will be processed by the next command.

                      • The substitution uses a capture group to remember the number after info2, and the whole line (everything before info2, info2 itself, the number, and anything following the number) is replaced by the number.

                      If you need to capture "done" after info5, you can't use [0-9]*, but you can replace it by [^,]*, i.e. non-comma.






                      share|improve this answer














                      You can use sed:



                      sed '/info2 [0-9]/!d;s/.*info2 ([0-9]*).*/1/' file



                      • /info2/ is an "address", any line containing info2 is considered to match the address. !d means "delete lines outside of the address", so only lines containing info2 will be processed by the next command.

                      • The substitution uses a capture group to remember the number after info2, and the whole line (everything before info2, info2 itself, the number, and anything following the number) is replaced by the number.

                      If you need to capture "done" after info5, you can't use [0-9]*, but you can replace it by [^,]*, i.e. non-comma.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jan 31 at 9:58

























                      answered Jan 30 at 19:48









                      choroba

                      24.3k33967




                      24.3k33967











                      • ... and he wants to do the same thing with info5 (and the value there is not a number).
                        – NickD
                        Jan 30 at 19:50










                      • @Nick (just to make the answer complete) use [^,]* in place of [0-9]*, and I agree awk is a better fit.
                        – Weijun Zhou
                        Jan 30 at 19:57











                      • What would Awk look like and why better?
                        – james
                        Jan 30 at 19:59










                      • This generates undesired results if a line contains info2 without actually having an info2 label; e.g., info20, info21, etc.
                        – G-Man
                        Jan 31 at 2:03

















                      • ... and he wants to do the same thing with info5 (and the value there is not a number).
                        – NickD
                        Jan 30 at 19:50










                      • @Nick (just to make the answer complete) use [^,]* in place of [0-9]*, and I agree awk is a better fit.
                        – Weijun Zhou
                        Jan 30 at 19:57











                      • What would Awk look like and why better?
                        – james
                        Jan 30 at 19:59










                      • This generates undesired results if a line contains info2 without actually having an info2 label; e.g., info20, info21, etc.
                        – G-Man
                        Jan 31 at 2:03
















                      ... and he wants to do the same thing with info5 (and the value there is not a number).
                      – NickD
                      Jan 30 at 19:50




                      ... and he wants to do the same thing with info5 (and the value there is not a number).
                      – NickD
                      Jan 30 at 19:50












                      @Nick (just to make the answer complete) use [^,]* in place of [0-9]*, and I agree awk is a better fit.
                      – Weijun Zhou
                      Jan 30 at 19:57





                      @Nick (just to make the answer complete) use [^,]* in place of [0-9]*, and I agree awk is a better fit.
                      – Weijun Zhou
                      Jan 30 at 19:57













                      What would Awk look like and why better?
                      – james
                      Jan 30 at 19:59




                      What would Awk look like and why better?
                      – james
                      Jan 30 at 19:59












                      This generates undesired results if a line contains info2 without actually having an info2 label; e.g., info20, info21, etc.
                      – G-Man
                      Jan 31 at 2:03





                      This generates undesired results if a line contains info2 without actually having an info2 label; e.g., info20, info21, etc.
                      – G-Man
                      Jan 31 at 2:03













                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f420776%2fhow-to-sed-or-grep-number-next-to-text%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?