Replace all values in one column to 1

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











up vote
8
down vote

favorite












I have multiple text files containing 12 lines and 3 columns.



Example:



2 6 0.74 
42 6 0.58
80 6 0
112 6 0.24
132 6 1
216 6 0.7
342 6 0
390 6 0.21
432 6 0.56
466 6 0.75
524 6 0.6
646 6 0.9


I want to set all the values of the third column to 1 in all lines.



The output should look like this :



2 6 1 
42 6 1
80 6 1
112 6 1
132 6 1
216 6 1
342 6 1
390 6 1
432 6 1
466 6 1
524 6 1
646 6 1


Does anyone know a command that can solve this problem?










share|improve this question



























    up vote
    8
    down vote

    favorite












    I have multiple text files containing 12 lines and 3 columns.



    Example:



    2 6 0.74 
    42 6 0.58
    80 6 0
    112 6 0.24
    132 6 1
    216 6 0.7
    342 6 0
    390 6 0.21
    432 6 0.56
    466 6 0.75
    524 6 0.6
    646 6 0.9


    I want to set all the values of the third column to 1 in all lines.



    The output should look like this :



    2 6 1 
    42 6 1
    80 6 1
    112 6 1
    132 6 1
    216 6 1
    342 6 1
    390 6 1
    432 6 1
    466 6 1
    524 6 1
    646 6 1


    Does anyone know a command that can solve this problem?










    share|improve this question

























      up vote
      8
      down vote

      favorite









      up vote
      8
      down vote

      favorite











      I have multiple text files containing 12 lines and 3 columns.



      Example:



      2 6 0.74 
      42 6 0.58
      80 6 0
      112 6 0.24
      132 6 1
      216 6 0.7
      342 6 0
      390 6 0.21
      432 6 0.56
      466 6 0.75
      524 6 0.6
      646 6 0.9


      I want to set all the values of the third column to 1 in all lines.



      The output should look like this :



      2 6 1 
      42 6 1
      80 6 1
      112 6 1
      132 6 1
      216 6 1
      342 6 1
      390 6 1
      432 6 1
      466 6 1
      524 6 1
      646 6 1


      Does anyone know a command that can solve this problem?










      share|improve this question















      I have multiple text files containing 12 lines and 3 columns.



      Example:



      2 6 0.74 
      42 6 0.58
      80 6 0
      112 6 0.24
      132 6 1
      216 6 0.7
      342 6 0
      390 6 0.21
      432 6 0.56
      466 6 0.75
      524 6 0.6
      646 6 0.9


      I want to set all the values of the third column to 1 in all lines.



      The output should look like this :



      2 6 1 
      42 6 1
      80 6 1
      112 6 1
      132 6 1
      216 6 1
      342 6 1
      390 6 1
      432 6 1
      466 6 1
      524 6 1
      646 6 1


      Does anyone know a command that can solve this problem?







      text-processing awk sed grep replace






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 6 at 22:40









      Jesse_b

      10.5k22659




      10.5k22659










      asked Dec 1 '16 at 9:19









      user203269

      41113




      41113




















          6 Answers
          6






          active

          oldest

          votes

















          up vote
          14
          down vote













          awk 'print $1, $2, "1"' inputfile





          share|improve this answer
















          • 1




            This command prints out the first line in the textfile corrrectly in terminal, but does not make changes in the file...
            – user203269
            Dec 2 '16 at 12:01










          • redirect the output to another file awk 'print $1, $2, "1"' inputfile > newfile
            – user1700494
            Dec 2 '16 at 13:51










          • Thanks! It works but only writes out the first line, column 1, 2 and 3. I would like to write out all 12 lines in the same manner :)
            – user203269
            Dec 2 '16 at 16:30


















          up vote
          10
          down vote













          try



          awk



           awk '$3=1 ; print ;' oldfile > newfile



          • $3 = 1 will set third field to 1

          sed (here GNU or busybox sed with its -i option for in-place editing)



          sed -i 's/[0-9.]*$/1/' file



          • [0-9.]*$ is a sequence from 0 to 9 and . up to the end of line.

          sed (golfed 4 bytes)



          sed -i 's/[^ ]*$/1/' file



          • [^ ]*$ any char other than space, until end of line.





          share|improve this answer


















          • 3




            Let's codegolf: sed 's/[^ ]*$/1/' :->
            – Ipor Sircer
            Dec 1 '16 at 9:40










          • Thank you so much! :) The awk seems to work, except for the first line: 2 6 1 6 1 80 6 1 112 6 1 132 6 1 216 6 1 342 6 1 390 6 1 432 6 1 466 6 1 524 6 1 646 6 1 The first line prints the 2nd and 3rd value twice?
            – user203269
            Dec 1 '16 at 9:44











          • @user203269 awk version works fine for me (albeit with a formating issue)
            – Archemar
            Dec 1 '16 at 10:04






          • 3




            awk golfed: awk $3=1 (POSIX but would not work with the awk from the 70s as found in /bin on Solaris)
            – Stéphane Chazelas
            Dec 1 '16 at 10:10











          • that awk solution is really cool....can you explain pls
            – mazs
            Dec 1 '16 at 13:59


















          up vote
          4
          down vote













          The lines in your expected output seem to end in two space characters and have fields separated by one tab and one space character.



          If that's indeed what you want, then you'd need:



          awk -v 'OFS=t ' '$3="1 "' < infile > outfile


          Or with sed:



          tab=$(printf 't')
          sed "
          s/[[:blank:]]1,/$tab /g
          s/[^[:blank:]]1,[[:blank:]]*$/1 /
          s/^[[:blank:]]*//" < infile > outfile





          share|improve this answer






















          • Why the spaces after 1?
            – 123
            Dec 1 '16 at 15:21











          • @123, like I said, in the OP's expected output, every line ends in two space characters.
            – Stéphane Chazelas
            Dec 1 '16 at 15:42










          • Misread thought you said fields were separated by two space and a tab. my bad.
            – 123
            Dec 1 '16 at 15:56










          • Hi Stephane, This awk does the first three values correctly, then it deletes column1 line 2 and continues without making changes..
            – user203269
            Dec 2 '16 at 12:32






          • 1




            @user203269, convert your file from MS-DOS to Unix first.
            – Stéphane Chazelas
            Dec 2 '16 at 12:50

















          up vote
          3
          down vote













          Simply with GNU sed, using -i to replace text directly in the file:



          sed -i 's:(.*s)(.*s)(.*):121:g' textfile


          The columns are matched by regex groups in the parenthesis, reusing them with 1 and 2 and then using a "1" to replace the last group.



          In this use case, the solution proposed using awk is nice and short as well.






          share|improve this answer





























            up vote
            2
            down vote













            this will do the job:



            cat textfiles | cut -d' ' -f-2 | sed 's/$/ 1/'





            share|improve this answer




















            • cat file.txt | cut -d' ' -f-2 | sed 's/$/ 1/' 646 6 0.5 1 prints out one line (the last line) in the terminal, but does not change the file.txt...
              – user203269
              Dec 2 '16 at 12:15

















            up vote
            -1
            down vote













            cat filename | awk -F ' ' '$3=1; print $0' > filename





            share|improve this answer






















            • Could you please edit your post to include more context as to why you feel this is the solution?
              – kemotep
              Apr 11 at 22:15










            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%2f327280%2freplace-all-values-in-one-column-to-1%23new-answer', 'question_page');

            );

            Post as a guest






























            6 Answers
            6






            active

            oldest

            votes








            6 Answers
            6






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            14
            down vote













            awk 'print $1, $2, "1"' inputfile





            share|improve this answer
















            • 1




              This command prints out the first line in the textfile corrrectly in terminal, but does not make changes in the file...
              – user203269
              Dec 2 '16 at 12:01










            • redirect the output to another file awk 'print $1, $2, "1"' inputfile > newfile
              – user1700494
              Dec 2 '16 at 13:51










            • Thanks! It works but only writes out the first line, column 1, 2 and 3. I would like to write out all 12 lines in the same manner :)
              – user203269
              Dec 2 '16 at 16:30















            up vote
            14
            down vote













            awk 'print $1, $2, "1"' inputfile





            share|improve this answer
















            • 1




              This command prints out the first line in the textfile corrrectly in terminal, but does not make changes in the file...
              – user203269
              Dec 2 '16 at 12:01










            • redirect the output to another file awk 'print $1, $2, "1"' inputfile > newfile
              – user1700494
              Dec 2 '16 at 13:51










            • Thanks! It works but only writes out the first line, column 1, 2 and 3. I would like to write out all 12 lines in the same manner :)
              – user203269
              Dec 2 '16 at 16:30













            up vote
            14
            down vote










            up vote
            14
            down vote









            awk 'print $1, $2, "1"' inputfile





            share|improve this answer












            awk 'print $1, $2, "1"' inputfile






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 1 '16 at 9:24









            user1700494

            1,650311




            1,650311







            • 1




              This command prints out the first line in the textfile corrrectly in terminal, but does not make changes in the file...
              – user203269
              Dec 2 '16 at 12:01










            • redirect the output to another file awk 'print $1, $2, "1"' inputfile > newfile
              – user1700494
              Dec 2 '16 at 13:51










            • Thanks! It works but only writes out the first line, column 1, 2 and 3. I would like to write out all 12 lines in the same manner :)
              – user203269
              Dec 2 '16 at 16:30













            • 1




              This command prints out the first line in the textfile corrrectly in terminal, but does not make changes in the file...
              – user203269
              Dec 2 '16 at 12:01










            • redirect the output to another file awk 'print $1, $2, "1"' inputfile > newfile
              – user1700494
              Dec 2 '16 at 13:51










            • Thanks! It works but only writes out the first line, column 1, 2 and 3. I would like to write out all 12 lines in the same manner :)
              – user203269
              Dec 2 '16 at 16:30








            1




            1




            This command prints out the first line in the textfile corrrectly in terminal, but does not make changes in the file...
            – user203269
            Dec 2 '16 at 12:01




            This command prints out the first line in the textfile corrrectly in terminal, but does not make changes in the file...
            – user203269
            Dec 2 '16 at 12:01












            redirect the output to another file awk 'print $1, $2, "1"' inputfile > newfile
            – user1700494
            Dec 2 '16 at 13:51




            redirect the output to another file awk 'print $1, $2, "1"' inputfile > newfile
            – user1700494
            Dec 2 '16 at 13:51












            Thanks! It works but only writes out the first line, column 1, 2 and 3. I would like to write out all 12 lines in the same manner :)
            – user203269
            Dec 2 '16 at 16:30





            Thanks! It works but only writes out the first line, column 1, 2 and 3. I would like to write out all 12 lines in the same manner :)
            – user203269
            Dec 2 '16 at 16:30













            up vote
            10
            down vote













            try



            awk



             awk '$3=1 ; print ;' oldfile > newfile



            • $3 = 1 will set third field to 1

            sed (here GNU or busybox sed with its -i option for in-place editing)



            sed -i 's/[0-9.]*$/1/' file



            • [0-9.]*$ is a sequence from 0 to 9 and . up to the end of line.

            sed (golfed 4 bytes)



            sed -i 's/[^ ]*$/1/' file



            • [^ ]*$ any char other than space, until end of line.





            share|improve this answer


















            • 3




              Let's codegolf: sed 's/[^ ]*$/1/' :->
              – Ipor Sircer
              Dec 1 '16 at 9:40










            • Thank you so much! :) The awk seems to work, except for the first line: 2 6 1 6 1 80 6 1 112 6 1 132 6 1 216 6 1 342 6 1 390 6 1 432 6 1 466 6 1 524 6 1 646 6 1 The first line prints the 2nd and 3rd value twice?
              – user203269
              Dec 1 '16 at 9:44











            • @user203269 awk version works fine for me (albeit with a formating issue)
              – Archemar
              Dec 1 '16 at 10:04






            • 3




              awk golfed: awk $3=1 (POSIX but would not work with the awk from the 70s as found in /bin on Solaris)
              – Stéphane Chazelas
              Dec 1 '16 at 10:10











            • that awk solution is really cool....can you explain pls
              – mazs
              Dec 1 '16 at 13:59















            up vote
            10
            down vote













            try



            awk



             awk '$3=1 ; print ;' oldfile > newfile



            • $3 = 1 will set third field to 1

            sed (here GNU or busybox sed with its -i option for in-place editing)



            sed -i 's/[0-9.]*$/1/' file



            • [0-9.]*$ is a sequence from 0 to 9 and . up to the end of line.

            sed (golfed 4 bytes)



            sed -i 's/[^ ]*$/1/' file



            • [^ ]*$ any char other than space, until end of line.





            share|improve this answer


















            • 3




              Let's codegolf: sed 's/[^ ]*$/1/' :->
              – Ipor Sircer
              Dec 1 '16 at 9:40










            • Thank you so much! :) The awk seems to work, except for the first line: 2 6 1 6 1 80 6 1 112 6 1 132 6 1 216 6 1 342 6 1 390 6 1 432 6 1 466 6 1 524 6 1 646 6 1 The first line prints the 2nd and 3rd value twice?
              – user203269
              Dec 1 '16 at 9:44











            • @user203269 awk version works fine for me (albeit with a formating issue)
              – Archemar
              Dec 1 '16 at 10:04






            • 3




              awk golfed: awk $3=1 (POSIX but would not work with the awk from the 70s as found in /bin on Solaris)
              – Stéphane Chazelas
              Dec 1 '16 at 10:10











            • that awk solution is really cool....can you explain pls
              – mazs
              Dec 1 '16 at 13:59













            up vote
            10
            down vote










            up vote
            10
            down vote









            try



            awk



             awk '$3=1 ; print ;' oldfile > newfile



            • $3 = 1 will set third field to 1

            sed (here GNU or busybox sed with its -i option for in-place editing)



            sed -i 's/[0-9.]*$/1/' file



            • [0-9.]*$ is a sequence from 0 to 9 and . up to the end of line.

            sed (golfed 4 bytes)



            sed -i 's/[^ ]*$/1/' file



            • [^ ]*$ any char other than space, until end of line.





            share|improve this answer














            try



            awk



             awk '$3=1 ; print ;' oldfile > newfile



            • $3 = 1 will set third field to 1

            sed (here GNU or busybox sed with its -i option for in-place editing)



            sed -i 's/[0-9.]*$/1/' file



            • [0-9.]*$ is a sequence from 0 to 9 and . up to the end of line.

            sed (golfed 4 bytes)



            sed -i 's/[^ ]*$/1/' file



            • [^ ]*$ any char other than space, until end of line.






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 1 '16 at 10:07









            Stéphane Chazelas

            284k53524862




            284k53524862










            answered Dec 1 '16 at 9:25









            Archemar

            19.1k93366




            19.1k93366







            • 3




              Let's codegolf: sed 's/[^ ]*$/1/' :->
              – Ipor Sircer
              Dec 1 '16 at 9:40










            • Thank you so much! :) The awk seems to work, except for the first line: 2 6 1 6 1 80 6 1 112 6 1 132 6 1 216 6 1 342 6 1 390 6 1 432 6 1 466 6 1 524 6 1 646 6 1 The first line prints the 2nd and 3rd value twice?
              – user203269
              Dec 1 '16 at 9:44











            • @user203269 awk version works fine for me (albeit with a formating issue)
              – Archemar
              Dec 1 '16 at 10:04






            • 3




              awk golfed: awk $3=1 (POSIX but would not work with the awk from the 70s as found in /bin on Solaris)
              – Stéphane Chazelas
              Dec 1 '16 at 10:10











            • that awk solution is really cool....can you explain pls
              – mazs
              Dec 1 '16 at 13:59













            • 3




              Let's codegolf: sed 's/[^ ]*$/1/' :->
              – Ipor Sircer
              Dec 1 '16 at 9:40










            • Thank you so much! :) The awk seems to work, except for the first line: 2 6 1 6 1 80 6 1 112 6 1 132 6 1 216 6 1 342 6 1 390 6 1 432 6 1 466 6 1 524 6 1 646 6 1 The first line prints the 2nd and 3rd value twice?
              – user203269
              Dec 1 '16 at 9:44











            • @user203269 awk version works fine for me (albeit with a formating issue)
              – Archemar
              Dec 1 '16 at 10:04






            • 3




              awk golfed: awk $3=1 (POSIX but would not work with the awk from the 70s as found in /bin on Solaris)
              – Stéphane Chazelas
              Dec 1 '16 at 10:10











            • that awk solution is really cool....can you explain pls
              – mazs
              Dec 1 '16 at 13:59








            3




            3




            Let's codegolf: sed 's/[^ ]*$/1/' :->
            – Ipor Sircer
            Dec 1 '16 at 9:40




            Let's codegolf: sed 's/[^ ]*$/1/' :->
            – Ipor Sircer
            Dec 1 '16 at 9:40












            Thank you so much! :) The awk seems to work, except for the first line: 2 6 1 6 1 80 6 1 112 6 1 132 6 1 216 6 1 342 6 1 390 6 1 432 6 1 466 6 1 524 6 1 646 6 1 The first line prints the 2nd and 3rd value twice?
            – user203269
            Dec 1 '16 at 9:44





            Thank you so much! :) The awk seems to work, except for the first line: 2 6 1 6 1 80 6 1 112 6 1 132 6 1 216 6 1 342 6 1 390 6 1 432 6 1 466 6 1 524 6 1 646 6 1 The first line prints the 2nd and 3rd value twice?
            – user203269
            Dec 1 '16 at 9:44













            @user203269 awk version works fine for me (albeit with a formating issue)
            – Archemar
            Dec 1 '16 at 10:04




            @user203269 awk version works fine for me (albeit with a formating issue)
            – Archemar
            Dec 1 '16 at 10:04




            3




            3




            awk golfed: awk $3=1 (POSIX but would not work with the awk from the 70s as found in /bin on Solaris)
            – Stéphane Chazelas
            Dec 1 '16 at 10:10





            awk golfed: awk $3=1 (POSIX but would not work with the awk from the 70s as found in /bin on Solaris)
            – Stéphane Chazelas
            Dec 1 '16 at 10:10













            that awk solution is really cool....can you explain pls
            – mazs
            Dec 1 '16 at 13:59





            that awk solution is really cool....can you explain pls
            – mazs
            Dec 1 '16 at 13:59











            up vote
            4
            down vote













            The lines in your expected output seem to end in two space characters and have fields separated by one tab and one space character.



            If that's indeed what you want, then you'd need:



            awk -v 'OFS=t ' '$3="1 "' < infile > outfile


            Or with sed:



            tab=$(printf 't')
            sed "
            s/[[:blank:]]1,/$tab /g
            s/[^[:blank:]]1,[[:blank:]]*$/1 /
            s/^[[:blank:]]*//" < infile > outfile





            share|improve this answer






















            • Why the spaces after 1?
              – 123
              Dec 1 '16 at 15:21











            • @123, like I said, in the OP's expected output, every line ends in two space characters.
              – Stéphane Chazelas
              Dec 1 '16 at 15:42










            • Misread thought you said fields were separated by two space and a tab. my bad.
              – 123
              Dec 1 '16 at 15:56










            • Hi Stephane, This awk does the first three values correctly, then it deletes column1 line 2 and continues without making changes..
              – user203269
              Dec 2 '16 at 12:32






            • 1




              @user203269, convert your file from MS-DOS to Unix first.
              – Stéphane Chazelas
              Dec 2 '16 at 12:50














            up vote
            4
            down vote













            The lines in your expected output seem to end in two space characters and have fields separated by one tab and one space character.



            If that's indeed what you want, then you'd need:



            awk -v 'OFS=t ' '$3="1 "' < infile > outfile


            Or with sed:



            tab=$(printf 't')
            sed "
            s/[[:blank:]]1,/$tab /g
            s/[^[:blank:]]1,[[:blank:]]*$/1 /
            s/^[[:blank:]]*//" < infile > outfile





            share|improve this answer






















            • Why the spaces after 1?
              – 123
              Dec 1 '16 at 15:21











            • @123, like I said, in the OP's expected output, every line ends in two space characters.
              – Stéphane Chazelas
              Dec 1 '16 at 15:42










            • Misread thought you said fields were separated by two space and a tab. my bad.
              – 123
              Dec 1 '16 at 15:56










            • Hi Stephane, This awk does the first three values correctly, then it deletes column1 line 2 and continues without making changes..
              – user203269
              Dec 2 '16 at 12:32






            • 1




              @user203269, convert your file from MS-DOS to Unix first.
              – Stéphane Chazelas
              Dec 2 '16 at 12:50












            up vote
            4
            down vote










            up vote
            4
            down vote









            The lines in your expected output seem to end in two space characters and have fields separated by one tab and one space character.



            If that's indeed what you want, then you'd need:



            awk -v 'OFS=t ' '$3="1 "' < infile > outfile


            Or with sed:



            tab=$(printf 't')
            sed "
            s/[[:blank:]]1,/$tab /g
            s/[^[:blank:]]1,[[:blank:]]*$/1 /
            s/^[[:blank:]]*//" < infile > outfile





            share|improve this answer














            The lines in your expected output seem to end in two space characters and have fields separated by one tab and one space character.



            If that's indeed what you want, then you'd need:



            awk -v 'OFS=t ' '$3="1 "' < infile > outfile


            Or with sed:



            tab=$(printf 't')
            sed "
            s/[[:blank:]]1,/$tab /g
            s/[^[:blank:]]1,[[:blank:]]*$/1 /
            s/^[[:blank:]]*//" < infile > outfile






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 2 '16 at 12:14

























            answered Dec 1 '16 at 10:22









            Stéphane Chazelas

            284k53524862




            284k53524862











            • Why the spaces after 1?
              – 123
              Dec 1 '16 at 15:21











            • @123, like I said, in the OP's expected output, every line ends in two space characters.
              – Stéphane Chazelas
              Dec 1 '16 at 15:42










            • Misread thought you said fields were separated by two space and a tab. my bad.
              – 123
              Dec 1 '16 at 15:56










            • Hi Stephane, This awk does the first three values correctly, then it deletes column1 line 2 and continues without making changes..
              – user203269
              Dec 2 '16 at 12:32






            • 1




              @user203269, convert your file from MS-DOS to Unix first.
              – Stéphane Chazelas
              Dec 2 '16 at 12:50
















            • Why the spaces after 1?
              – 123
              Dec 1 '16 at 15:21











            • @123, like I said, in the OP's expected output, every line ends in two space characters.
              – Stéphane Chazelas
              Dec 1 '16 at 15:42










            • Misread thought you said fields were separated by two space and a tab. my bad.
              – 123
              Dec 1 '16 at 15:56










            • Hi Stephane, This awk does the first three values correctly, then it deletes column1 line 2 and continues without making changes..
              – user203269
              Dec 2 '16 at 12:32






            • 1




              @user203269, convert your file from MS-DOS to Unix first.
              – Stéphane Chazelas
              Dec 2 '16 at 12:50















            Why the spaces after 1?
            – 123
            Dec 1 '16 at 15:21





            Why the spaces after 1?
            – 123
            Dec 1 '16 at 15:21













            @123, like I said, in the OP's expected output, every line ends in two space characters.
            – Stéphane Chazelas
            Dec 1 '16 at 15:42




            @123, like I said, in the OP's expected output, every line ends in two space characters.
            – Stéphane Chazelas
            Dec 1 '16 at 15:42












            Misread thought you said fields were separated by two space and a tab. my bad.
            – 123
            Dec 1 '16 at 15:56




            Misread thought you said fields were separated by two space and a tab. my bad.
            – 123
            Dec 1 '16 at 15:56












            Hi Stephane, This awk does the first three values correctly, then it deletes column1 line 2 and continues without making changes..
            – user203269
            Dec 2 '16 at 12:32




            Hi Stephane, This awk does the first three values correctly, then it deletes column1 line 2 and continues without making changes..
            – user203269
            Dec 2 '16 at 12:32




            1




            1




            @user203269, convert your file from MS-DOS to Unix first.
            – Stéphane Chazelas
            Dec 2 '16 at 12:50




            @user203269, convert your file from MS-DOS to Unix first.
            – Stéphane Chazelas
            Dec 2 '16 at 12:50










            up vote
            3
            down vote













            Simply with GNU sed, using -i to replace text directly in the file:



            sed -i 's:(.*s)(.*s)(.*):121:g' textfile


            The columns are matched by regex groups in the parenthesis, reusing them with 1 and 2 and then using a "1" to replace the last group.



            In this use case, the solution proposed using awk is nice and short as well.






            share|improve this answer


























              up vote
              3
              down vote













              Simply with GNU sed, using -i to replace text directly in the file:



              sed -i 's:(.*s)(.*s)(.*):121:g' textfile


              The columns are matched by regex groups in the parenthesis, reusing them with 1 and 2 and then using a "1" to replace the last group.



              In this use case, the solution proposed using awk is nice and short as well.






              share|improve this answer
























                up vote
                3
                down vote










                up vote
                3
                down vote









                Simply with GNU sed, using -i to replace text directly in the file:



                sed -i 's:(.*s)(.*s)(.*):121:g' textfile


                The columns are matched by regex groups in the parenthesis, reusing them with 1 and 2 and then using a "1" to replace the last group.



                In this use case, the solution proposed using awk is nice and short as well.






                share|improve this answer














                Simply with GNU sed, using -i to replace text directly in the file:



                sed -i 's:(.*s)(.*s)(.*):121:g' textfile


                The columns are matched by regex groups in the parenthesis, reusing them with 1 and 2 and then using a "1" to replace the last group.



                In this use case, the solution proposed using awk is nice and short as well.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Dec 2 '16 at 12:11









                Stéphane Chazelas

                284k53524862




                284k53524862










                answered Dec 1 '16 at 10:30









                mazs

                2,5151522




                2,5151522




















                    up vote
                    2
                    down vote













                    this will do the job:



                    cat textfiles | cut -d' ' -f-2 | sed 's/$/ 1/'





                    share|improve this answer




















                    • cat file.txt | cut -d' ' -f-2 | sed 's/$/ 1/' 646 6 0.5 1 prints out one line (the last line) in the terminal, but does not change the file.txt...
                      – user203269
                      Dec 2 '16 at 12:15














                    up vote
                    2
                    down vote













                    this will do the job:



                    cat textfiles | cut -d' ' -f-2 | sed 's/$/ 1/'





                    share|improve this answer




















                    • cat file.txt | cut -d' ' -f-2 | sed 's/$/ 1/' 646 6 0.5 1 prints out one line (the last line) in the terminal, but does not change the file.txt...
                      – user203269
                      Dec 2 '16 at 12:15












                    up vote
                    2
                    down vote










                    up vote
                    2
                    down vote









                    this will do the job:



                    cat textfiles | cut -d' ' -f-2 | sed 's/$/ 1/'





                    share|improve this answer












                    this will do the job:



                    cat textfiles | cut -d' ' -f-2 | sed 's/$/ 1/'






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Dec 1 '16 at 9:30









                    Wissam Roujoulah

                    2,833316




                    2,833316











                    • cat file.txt | cut -d' ' -f-2 | sed 's/$/ 1/' 646 6 0.5 1 prints out one line (the last line) in the terminal, but does not change the file.txt...
                      – user203269
                      Dec 2 '16 at 12:15
















                    • cat file.txt | cut -d' ' -f-2 | sed 's/$/ 1/' 646 6 0.5 1 prints out one line (the last line) in the terminal, but does not change the file.txt...
                      – user203269
                      Dec 2 '16 at 12:15















                    cat file.txt | cut -d' ' -f-2 | sed 's/$/ 1/' 646 6 0.5 1 prints out one line (the last line) in the terminal, but does not change the file.txt...
                    – user203269
                    Dec 2 '16 at 12:15




                    cat file.txt | cut -d' ' -f-2 | sed 's/$/ 1/' 646 6 0.5 1 prints out one line (the last line) in the terminal, but does not change the file.txt...
                    – user203269
                    Dec 2 '16 at 12:15










                    up vote
                    -1
                    down vote













                    cat filename | awk -F ' ' '$3=1; print $0' > filename





                    share|improve this answer






















                    • Could you please edit your post to include more context as to why you feel this is the solution?
                      – kemotep
                      Apr 11 at 22:15














                    up vote
                    -1
                    down vote













                    cat filename | awk -F ' ' '$3=1; print $0' > filename





                    share|improve this answer






















                    • Could you please edit your post to include more context as to why you feel this is the solution?
                      – kemotep
                      Apr 11 at 22:15












                    up vote
                    -1
                    down vote










                    up vote
                    -1
                    down vote









                    cat filename | awk -F ' ' '$3=1; print $0' > filename





                    share|improve this answer














                    cat filename | awk -F ' ' '$3=1; print $0' > filename






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Apr 11 at 22:22









                    Jeff Schaller

                    32.4k849110




                    32.4k849110










                    answered Apr 11 at 21:11









                    Thriller

                    11




                    11











                    • Could you please edit your post to include more context as to why you feel this is the solution?
                      – kemotep
                      Apr 11 at 22:15
















                    • Could you please edit your post to include more context as to why you feel this is the solution?
                      – kemotep
                      Apr 11 at 22:15















                    Could you please edit your post to include more context as to why you feel this is the solution?
                    – kemotep
                    Apr 11 at 22:15




                    Could you please edit your post to include more context as to why you feel this is the solution?
                    – kemotep
                    Apr 11 at 22:15

















                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f327280%2freplace-all-values-in-one-column-to-1%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?