Manipulating text in first column only? [duplicate]












4















This question already has an answer here:




  • modify specific column with sed or awk

    4 answers




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













marked as duplicate by Jeff Schaller, RalfFriedl, Stephen Harris, jimmij, DarkHeart Dec 12 at 6:03


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 2




    Why not just leave off the "g" at the end of the sed command? Then it will only replace the first occurrence...
    – twalberg
    Dec 11 at 16:46










  • I’m a simple man. I see columns, I think “awk”.
    – Konrad Rudolph
    Dec 11 at 17:45












  • @twalberg What if the first column has no .com but the second does?
    – Nic Hartley
    Dec 11 at 18:22










  • @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
    Dec 11 at 18:23
















4















This question already has an answer here:




  • modify specific column with sed or awk

    4 answers




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













marked as duplicate by Jeff Schaller, RalfFriedl, Stephen Harris, jimmij, DarkHeart Dec 12 at 6:03


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 2




    Why not just leave off the "g" at the end of the sed command? Then it will only replace the first occurrence...
    – twalberg
    Dec 11 at 16:46










  • I’m a simple man. I see columns, I think “awk”.
    – Konrad Rudolph
    Dec 11 at 17:45












  • @twalberg What if the first column has no .com but the second does?
    – Nic Hartley
    Dec 11 at 18:22










  • @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
    Dec 11 at 18:23














4












4








4








This question already has an answer here:




  • modify specific column with sed or awk

    4 answers




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














This question already has an answer here:




  • modify specific column with sed or awk

    4 answers




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).





This question already has an answer here:




  • modify specific column with sed or awk

    4 answers








text-processing awk sed perl






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 11 at 9:25









Little Code

1928




1928




marked as duplicate by Jeff Schaller, RalfFriedl, Stephen Harris, jimmij, DarkHeart Dec 12 at 6:03


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Jeff Schaller, RalfFriedl, Stephen Harris, jimmij, DarkHeart Dec 12 at 6:03


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 2




    Why not just leave off the "g" at the end of the sed command? Then it will only replace the first occurrence...
    – twalberg
    Dec 11 at 16:46










  • I’m a simple man. I see columns, I think “awk”.
    – Konrad Rudolph
    Dec 11 at 17:45












  • @twalberg What if the first column has no .com but the second does?
    – Nic Hartley
    Dec 11 at 18:22










  • @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
    Dec 11 at 18:23














  • 2




    Why not just leave off the "g" at the end of the sed command? Then it will only replace the first occurrence...
    – twalberg
    Dec 11 at 16:46










  • I’m a simple man. I see columns, I think “awk”.
    – Konrad Rudolph
    Dec 11 at 17:45












  • @twalberg What if the first column has no .com but the second does?
    – Nic Hartley
    Dec 11 at 18:22










  • @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
    Dec 11 at 18:23








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
Dec 11 at 16:46




Why not just leave off the "g" at the end of the sed command? Then it will only replace the first occurrence...
– twalberg
Dec 11 at 16:46












I’m a simple man. I see columns, I think “awk”.
– Konrad Rudolph
Dec 11 at 17:45






I’m a simple man. I see columns, I think “awk”.
– Konrad Rudolph
Dec 11 at 17:45














@twalberg What if the first column has no .com but the second does?
– Nic Hartley
Dec 11 at 18:22




@twalberg What if the first column has no .com but the second does?
– Nic Hartley
Dec 11 at 18:22












@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
Dec 11 at 18:23




@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
Dec 11 at 18:23










3 Answers
3






active

oldest

votes


















4














$ 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































    5














    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































      4














      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
        Dec 11 at 10:10










      • 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
        Dec 11 at 10:47










      • Note that the use of PCRE requires GNU sed.
        – Kusalananda
        Dec 11 at 12:04










      • OP asked for "standard Linux" :)
        – xenoid
        Dec 11 at 12:54




















      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      4














      $ 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




























        4














        $ 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


























          4












          4








          4






          $ 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 Dec 11 at 9:34

























          answered Dec 11 at 9:30









          Kusalananda

          121k16228372




          121k16228372

























              5














              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




























                5














                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


























                  5












                  5








                  5






                  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 Dec 11 at 12:29

























                  answered Dec 11 at 10:34









                  terdon

                  128k31246423




                  128k31246423























                      4














                      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
                        Dec 11 at 10:10










                      • 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
                        Dec 11 at 10:47










                      • Note that the use of PCRE requires GNU sed.
                        – Kusalananda
                        Dec 11 at 12:04










                      • OP asked for "standard Linux" :)
                        – xenoid
                        Dec 11 at 12:54


















                      4














                      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
                        Dec 11 at 10:10










                      • 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
                        Dec 11 at 10:47










                      • Note that the use of PCRE requires GNU sed.
                        – Kusalananda
                        Dec 11 at 12:04










                      • OP asked for "standard Linux" :)
                        – xenoid
                        Dec 11 at 12:54
















                      4












                      4








                      4






                      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 Dec 11 at 10:14

























                      answered Dec 11 at 9:59









                      xenoid

                      2,6681724




                      2,6681724












                      • Thanks, fixed and improved.
                        – xenoid
                        Dec 11 at 10:10










                      • 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
                        Dec 11 at 10:47










                      • Note that the use of PCRE requires GNU sed.
                        – Kusalananda
                        Dec 11 at 12:04










                      • OP asked for "standard Linux" :)
                        – xenoid
                        Dec 11 at 12:54




















                      • Thanks, fixed and improved.
                        – xenoid
                        Dec 11 at 10:10










                      • 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
                        Dec 11 at 10:47










                      • Note that the use of PCRE requires GNU sed.
                        – Kusalananda
                        Dec 11 at 12:04










                      • OP asked for "standard Linux" :)
                        – xenoid
                        Dec 11 at 12:54


















                      Thanks, fixed and improved.
                      – xenoid
                      Dec 11 at 10:10




                      Thanks, fixed and improved.
                      – xenoid
                      Dec 11 at 10:10












                      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
                      Dec 11 at 10:47




                      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
                      Dec 11 at 10:47












                      Note that the use of PCRE requires GNU sed.
                      – Kusalananda
                      Dec 11 at 12:04




                      Note that the use of PCRE requires GNU sed.
                      – Kusalananda
                      Dec 11 at 12:04












                      OP asked for "standard Linux" :)
                      – xenoid
                      Dec 11 at 12:54






                      OP asked for "standard Linux" :)
                      – xenoid
                      Dec 11 at 12:54





                      Popular posts from this blog

                      Probability when a professor distributes a quiz and homework assignment to a class of n students.

                      Aardman Animations

                      Are they similar matrix