Manipulating text in first column only?











up vote
4
down vote

favorite












Example Input:



foobar@example.com foo@example.com,bar@example.com


Example Output:



foobar@example.org foo@example.com,bar@example.com


So, in "normal" circumstances, it would obviously be easy to do something like:



sed 's/.com/.org/g'


But obviously in this case, I only want the suffix in the first column to be manipulated, I want the second column to be left untouched.



I don't mind what tool you propose to use. But I prefer it to available on a standard linux without needing further install (i.e. something like sed or awk or perl would be more preferable to bobsobscuretoolthatneedsinstalling).










share|improve this question


















  • 4




    Possible duplicate of modify specific column with sed or awk
    – Julien Lopez
    13 hours ago






  • 2




    Why not just leave off the "g" at the end of the sed command? Then it will only replace the first occurrence...
    – twalberg
    9 hours ago










  • I’m a simple man. I see columns, I think “awk”.
    – Konrad Rudolph
    8 hours ago












  • @twalberg What if the first column has no .com but the second does?
    – Nic Hartley
    7 hours ago










  • @NicHartley Valid point in general, in which case I would use something like the accepted awk solution. But, it doesn't match the supplied sample input, which may or may not mean anything...
    – twalberg
    7 hours ago















up vote
4
down vote

favorite












Example Input:



foobar@example.com foo@example.com,bar@example.com


Example Output:



foobar@example.org foo@example.com,bar@example.com


So, in "normal" circumstances, it would obviously be easy to do something like:



sed 's/.com/.org/g'


But obviously in this case, I only want the suffix in the first column to be manipulated, I want the second column to be left untouched.



I don't mind what tool you propose to use. But I prefer it to available on a standard linux without needing further install (i.e. something like sed or awk or perl would be more preferable to bobsobscuretoolthatneedsinstalling).










share|improve this question


















  • 4




    Possible duplicate of modify specific column with sed or awk
    – Julien Lopez
    13 hours ago






  • 2




    Why not just leave off the "g" at the end of the sed command? Then it will only replace the first occurrence...
    – twalberg
    9 hours ago










  • I’m a simple man. I see columns, I think “awk”.
    – Konrad Rudolph
    8 hours ago












  • @twalberg What if the first column has no .com but the second does?
    – Nic Hartley
    7 hours ago










  • @NicHartley Valid point in general, in which case I would use something like the accepted awk solution. But, it doesn't match the supplied sample input, which may or may not mean anything...
    – twalberg
    7 hours ago













up vote
4
down vote

favorite









up vote
4
down vote

favorite











Example Input:



foobar@example.com foo@example.com,bar@example.com


Example Output:



foobar@example.org foo@example.com,bar@example.com


So, in "normal" circumstances, it would obviously be easy to do something like:



sed 's/.com/.org/g'


But obviously in this case, I only want the suffix in the first column to be manipulated, I want the second column to be left untouched.



I don't mind what tool you propose to use. But I prefer it to available on a standard linux without needing further install (i.e. something like sed or awk or perl would be more preferable to bobsobscuretoolthatneedsinstalling).










share|improve this question













Example Input:



foobar@example.com foo@example.com,bar@example.com


Example Output:



foobar@example.org foo@example.com,bar@example.com


So, in "normal" circumstances, it would obviously be easy to do something like:



sed 's/.com/.org/g'


But obviously in this case, I only want the suffix in the first column to be manipulated, I want the second column to be left untouched.



I don't mind what tool you propose to use. But I prefer it to available on a standard linux without needing further install (i.e. something like sed or awk or perl would be more preferable to bobsobscuretoolthatneedsinstalling).







text-processing awk sed perl






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 16 hours ago









Little Code

1928




1928








  • 4




    Possible duplicate of modify specific column with sed or awk
    – Julien Lopez
    13 hours ago






  • 2




    Why not just leave off the "g" at the end of the sed command? Then it will only replace the first occurrence...
    – twalberg
    9 hours ago










  • I’m a simple man. I see columns, I think “awk”.
    – Konrad Rudolph
    8 hours ago












  • @twalberg What if the first column has no .com but the second does?
    – Nic Hartley
    7 hours ago










  • @NicHartley Valid point in general, in which case I would use something like the accepted awk solution. But, it doesn't match the supplied sample input, which may or may not mean anything...
    – twalberg
    7 hours ago














  • 4




    Possible duplicate of modify specific column with sed or awk
    – Julien Lopez
    13 hours ago






  • 2




    Why not just leave off the "g" at the end of the sed command? Then it will only replace the first occurrence...
    – twalberg
    9 hours ago










  • I’m a simple man. I see columns, I think “awk”.
    – Konrad Rudolph
    8 hours ago












  • @twalberg What if the first column has no .com but the second does?
    – Nic Hartley
    7 hours ago










  • @NicHartley Valid point in general, in which case I would use something like the accepted awk solution. But, it doesn't match the supplied sample input, which may or may not mean anything...
    – twalberg
    7 hours ago








4




4




Possible duplicate of modify specific column with sed or awk
– Julien Lopez
13 hours ago




Possible duplicate of modify specific column with sed or awk
– Julien Lopez
13 hours ago




2




2




Why not just leave off the "g" at the end of the sed command? Then it will only replace the first occurrence...
– twalberg
9 hours ago




Why not just leave off the "g" at the end of the sed command? Then it will only replace the first occurrence...
– twalberg
9 hours ago












I’m a simple man. I see columns, I think “awk”.
– Konrad Rudolph
8 hours ago






I’m a simple man. I see columns, I think “awk”.
– Konrad Rudolph
8 hours ago














@twalberg What if the first column has no .com but the second does?
– Nic Hartley
7 hours ago




@twalberg What if the first column has no .com but the second does?
– Nic Hartley
7 hours ago












@NicHartley Valid point in general, in which case I would use something like the accepted awk solution. But, it doesn't match the supplied sample input, which may or may not mean anything...
– twalberg
7 hours ago




@NicHartley Valid point in general, in which case I would use something like the accepted awk solution. But, it doesn't match the supplied sample input, which may or may not mean anything...
– twalberg
7 hours ago










3 Answers
3






active

oldest

votes

















up vote
4
down vote



accepted










$ awk '{ sub(".com$", ".org", $1); print }' <file
foobar@example.org foo@example.com,bar@example.com


This uses awk to substitute the text matched by .com$ with .org in the first whitespace-delimited field (only) of each line. The output will be space-delimited.






share|improve this answer






























    up vote
    5
    down vote













    If you just want to replace the first occurrence of .com with .org, all you need is the default behavior of sed's s/// operator. Just don't use the g flag:



    $ sed 's/.com/.org/' file 
    foobar@example.org foo@example.com,bar@example.com


    If you really want to only make the change on the first comma-defined field, so that if the first .com appears elsewhere in the line, it will remain unchanged, you can do something like:



    $ perl -pe 's/^(S+).com/$1.org/' file 
    foobar@example.org foo@example.com,bar@example.com


    Or, safer in case com occurs as a substring (e.g. foo.common.net):



    $ perl -pe 's/^(S+).comb/$1.org/' file 
    foobar@example.org foo@example.com,bar@example.com


    Alternatively, in GNU sed:



    $ sed -E 's/^(S+).comb/1.org/' file 
    foobar@example.org foo@example.com,bar@example.com


    Or, portably (assuming the first field is defined by the first space and not a tab or other whitespace):



    $ sed -E 's/^([^ ]).com /1.org /' file 
    foobar@example.org foo@example.com,bar@example.com





    share|improve this answer






























      up vote
      4
      down vote













      You can do it using sed, just use a regexp that can only match the first word of the line:



      sed -r 's/^(S+).com(s+)/1.org2/'


      In slo-mo, change:




      1. a sequence of one or more non-spaces from the beginning (^(S+))

      2. .com

      3. a sequence of one or more non spaces (so that we can only match a final .com)


      into:




      1. the first sequence of non-spaces

      2. .org

      3. the sequence of spaces






      share|improve this answer























      • Thanks, fixed and improved.
        – xenoid
        16 hours ago










      • Not using the /g doesn't prevent the substitution to occur in other items of the line if it doesn't happen on the first. And capturing the final s+ insures that we aren't replacing .com in the middle of a node id such as some.commonname.com.
        – xenoid
        15 hours ago










      • Note that the use of PCRE requires GNU sed.
        – Kusalananda
        14 hours ago










      • OP asked for "standard Linux" :)
        – xenoid
        13 hours ago













      Your Answer








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

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

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f487293%2fmanipulating-text-in-first-column-only%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      4
      down vote



      accepted










      $ awk '{ sub(".com$", ".org", $1); print }' <file
      foobar@example.org foo@example.com,bar@example.com


      This uses awk to substitute the text matched by .com$ with .org in the first whitespace-delimited field (only) of each line. The output will be space-delimited.






      share|improve this answer



























        up vote
        4
        down vote



        accepted










        $ awk '{ sub(".com$", ".org", $1); print }' <file
        foobar@example.org foo@example.com,bar@example.com


        This uses awk to substitute the text matched by .com$ with .org in the first whitespace-delimited field (only) of each line. The output will be space-delimited.






        share|improve this answer

























          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          $ awk '{ sub(".com$", ".org", $1); print }' <file
          foobar@example.org foo@example.com,bar@example.com


          This uses awk to substitute the text matched by .com$ with .org in the first whitespace-delimited field (only) of each line. The output will be space-delimited.






          share|improve this answer














          $ awk '{ sub(".com$", ".org", $1); print }' <file
          foobar@example.org foo@example.com,bar@example.com


          This uses awk to substitute the text matched by .com$ with .org in the first whitespace-delimited field (only) of each line. The output will be space-delimited.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 16 hours ago

























          answered 16 hours ago









          Kusalananda

          119k16223365




          119k16223365
























              up vote
              5
              down vote













              If you just want to replace the first occurrence of .com with .org, all you need is the default behavior of sed's s/// operator. Just don't use the g flag:



              $ sed 's/.com/.org/' file 
              foobar@example.org foo@example.com,bar@example.com


              If you really want to only make the change on the first comma-defined field, so that if the first .com appears elsewhere in the line, it will remain unchanged, you can do something like:



              $ perl -pe 's/^(S+).com/$1.org/' file 
              foobar@example.org foo@example.com,bar@example.com


              Or, safer in case com occurs as a substring (e.g. foo.common.net):



              $ perl -pe 's/^(S+).comb/$1.org/' file 
              foobar@example.org foo@example.com,bar@example.com


              Alternatively, in GNU sed:



              $ sed -E 's/^(S+).comb/1.org/' file 
              foobar@example.org foo@example.com,bar@example.com


              Or, portably (assuming the first field is defined by the first space and not a tab or other whitespace):



              $ sed -E 's/^([^ ]).com /1.org /' file 
              foobar@example.org foo@example.com,bar@example.com





              share|improve this answer



























                up vote
                5
                down vote













                If you just want to replace the first occurrence of .com with .org, all you need is the default behavior of sed's s/// operator. Just don't use the g flag:



                $ sed 's/.com/.org/' file 
                foobar@example.org foo@example.com,bar@example.com


                If you really want to only make the change on the first comma-defined field, so that if the first .com appears elsewhere in the line, it will remain unchanged, you can do something like:



                $ perl -pe 's/^(S+).com/$1.org/' file 
                foobar@example.org foo@example.com,bar@example.com


                Or, safer in case com occurs as a substring (e.g. foo.common.net):



                $ perl -pe 's/^(S+).comb/$1.org/' file 
                foobar@example.org foo@example.com,bar@example.com


                Alternatively, in GNU sed:



                $ sed -E 's/^(S+).comb/1.org/' file 
                foobar@example.org foo@example.com,bar@example.com


                Or, portably (assuming the first field is defined by the first space and not a tab or other whitespace):



                $ sed -E 's/^([^ ]).com /1.org /' file 
                foobar@example.org foo@example.com,bar@example.com





                share|improve this answer

























                  up vote
                  5
                  down vote










                  up vote
                  5
                  down vote









                  If you just want to replace the first occurrence of .com with .org, all you need is the default behavior of sed's s/// operator. Just don't use the g flag:



                  $ sed 's/.com/.org/' file 
                  foobar@example.org foo@example.com,bar@example.com


                  If you really want to only make the change on the first comma-defined field, so that if the first .com appears elsewhere in the line, it will remain unchanged, you can do something like:



                  $ perl -pe 's/^(S+).com/$1.org/' file 
                  foobar@example.org foo@example.com,bar@example.com


                  Or, safer in case com occurs as a substring (e.g. foo.common.net):



                  $ perl -pe 's/^(S+).comb/$1.org/' file 
                  foobar@example.org foo@example.com,bar@example.com


                  Alternatively, in GNU sed:



                  $ sed -E 's/^(S+).comb/1.org/' file 
                  foobar@example.org foo@example.com,bar@example.com


                  Or, portably (assuming the first field is defined by the first space and not a tab or other whitespace):



                  $ sed -E 's/^([^ ]).com /1.org /' file 
                  foobar@example.org foo@example.com,bar@example.com





                  share|improve this answer














                  If you just want to replace the first occurrence of .com with .org, all you need is the default behavior of sed's s/// operator. Just don't use the g flag:



                  $ sed 's/.com/.org/' file 
                  foobar@example.org foo@example.com,bar@example.com


                  If you really want to only make the change on the first comma-defined field, so that if the first .com appears elsewhere in the line, it will remain unchanged, you can do something like:



                  $ perl -pe 's/^(S+).com/$1.org/' file 
                  foobar@example.org foo@example.com,bar@example.com


                  Or, safer in case com occurs as a substring (e.g. foo.common.net):



                  $ perl -pe 's/^(S+).comb/$1.org/' file 
                  foobar@example.org foo@example.com,bar@example.com


                  Alternatively, in GNU sed:



                  $ sed -E 's/^(S+).comb/1.org/' file 
                  foobar@example.org foo@example.com,bar@example.com


                  Or, portably (assuming the first field is defined by the first space and not a tab or other whitespace):



                  $ sed -E 's/^([^ ]).com /1.org /' file 
                  foobar@example.org foo@example.com,bar@example.com






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 13 hours ago

























                  answered 15 hours ago









                  terdon

                  127k31245422




                  127k31245422






















                      up vote
                      4
                      down vote













                      You can do it using sed, just use a regexp that can only match the first word of the line:



                      sed -r 's/^(S+).com(s+)/1.org2/'


                      In slo-mo, change:




                      1. a sequence of one or more non-spaces from the beginning (^(S+))

                      2. .com

                      3. a sequence of one or more non spaces (so that we can only match a final .com)


                      into:




                      1. the first sequence of non-spaces

                      2. .org

                      3. the sequence of spaces






                      share|improve this answer























                      • Thanks, fixed and improved.
                        – xenoid
                        16 hours ago










                      • Not using the /g doesn't prevent the substitution to occur in other items of the line if it doesn't happen on the first. And capturing the final s+ insures that we aren't replacing .com in the middle of a node id such as some.commonname.com.
                        – xenoid
                        15 hours ago










                      • Note that the use of PCRE requires GNU sed.
                        – Kusalananda
                        14 hours ago










                      • OP asked for "standard Linux" :)
                        – xenoid
                        13 hours ago

















                      up vote
                      4
                      down vote













                      You can do it using sed, just use a regexp that can only match the first word of the line:



                      sed -r 's/^(S+).com(s+)/1.org2/'


                      In slo-mo, change:




                      1. a sequence of one or more non-spaces from the beginning (^(S+))

                      2. .com

                      3. a sequence of one or more non spaces (so that we can only match a final .com)


                      into:




                      1. the first sequence of non-spaces

                      2. .org

                      3. the sequence of spaces






                      share|improve this answer























                      • Thanks, fixed and improved.
                        – xenoid
                        16 hours ago










                      • Not using the /g doesn't prevent the substitution to occur in other items of the line if it doesn't happen on the first. And capturing the final s+ insures that we aren't replacing .com in the middle of a node id such as some.commonname.com.
                        – xenoid
                        15 hours ago










                      • Note that the use of PCRE requires GNU sed.
                        – Kusalananda
                        14 hours ago










                      • OP asked for "standard Linux" :)
                        – xenoid
                        13 hours ago















                      up vote
                      4
                      down vote










                      up vote
                      4
                      down vote









                      You can do it using sed, just use a regexp that can only match the first word of the line:



                      sed -r 's/^(S+).com(s+)/1.org2/'


                      In slo-mo, change:




                      1. a sequence of one or more non-spaces from the beginning (^(S+))

                      2. .com

                      3. a sequence of one or more non spaces (so that we can only match a final .com)


                      into:




                      1. the first sequence of non-spaces

                      2. .org

                      3. the sequence of spaces






                      share|improve this answer














                      You can do it using sed, just use a regexp that can only match the first word of the line:



                      sed -r 's/^(S+).com(s+)/1.org2/'


                      In slo-mo, change:




                      1. a sequence of one or more non-spaces from the beginning (^(S+))

                      2. .com

                      3. a sequence of one or more non spaces (so that we can only match a final .com)


                      into:




                      1. the first sequence of non-spaces

                      2. .org

                      3. the sequence of spaces







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited 16 hours ago

























                      answered 16 hours ago









                      xenoid

                      2,6701724




                      2,6701724












                      • Thanks, fixed and improved.
                        – xenoid
                        16 hours ago










                      • Not using the /g doesn't prevent the substitution to occur in other items of the line if it doesn't happen on the first. And capturing the final s+ insures that we aren't replacing .com in the middle of a node id such as some.commonname.com.
                        – xenoid
                        15 hours ago










                      • Note that the use of PCRE requires GNU sed.
                        – Kusalananda
                        14 hours ago










                      • OP asked for "standard Linux" :)
                        – xenoid
                        13 hours ago




















                      • Thanks, fixed and improved.
                        – xenoid
                        16 hours ago










                      • Not using the /g doesn't prevent the substitution to occur in other items of the line if it doesn't happen on the first. And capturing the final s+ insures that we aren't replacing .com in the middle of a node id such as some.commonname.com.
                        – xenoid
                        15 hours ago










                      • Note that the use of PCRE requires GNU sed.
                        – Kusalananda
                        14 hours ago










                      • OP asked for "standard Linux" :)
                        – xenoid
                        13 hours ago


















                      Thanks, fixed and improved.
                      – xenoid
                      16 hours ago




                      Thanks, fixed and improved.
                      – xenoid
                      16 hours ago












                      Not using the /g doesn't prevent the substitution to occur in other items of the line if it doesn't happen on the first. And capturing the final s+ insures that we aren't replacing .com in the middle of a node id such as some.commonname.com.
                      – xenoid
                      15 hours ago




                      Not using the /g doesn't prevent the substitution to occur in other items of the line if it doesn't happen on the first. And capturing the final s+ insures that we aren't replacing .com in the middle of a node id such as some.commonname.com.
                      – xenoid
                      15 hours ago












                      Note that the use of PCRE requires GNU sed.
                      – Kusalananda
                      14 hours ago




                      Note that the use of PCRE requires GNU sed.
                      – Kusalananda
                      14 hours ago












                      OP asked for "standard Linux" :)
                      – xenoid
                      13 hours ago






                      OP asked for "standard Linux" :)
                      – xenoid
                      13 hours ago




















                      draft saved

                      draft discarded




















































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


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

                      But avoid



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

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


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





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


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

                      But avoid



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

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


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




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f487293%2fmanipulating-text-in-first-column-only%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

                      Quarter-circle Tiles

                      build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

                      Mont Emei