Replace all values in one column to 1

41,915

Solution 1

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

Solution 2

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.

Solution 3

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

Solution 4

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

sed -i 's:\(.*\s\)\(.*\s\)\(.*\):\1\21: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.

Solution 5

this will do the job:

cat textfiles | cut -d' ' -f-2 | sed 's/$/ 1/'
Share:
41,915

Related videos on Youtube

user203269
Author by

user203269

Updated on September 18, 2022

Comments

  • user203269
    user203269 over 1 year

    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?

  • Ipor Sircer
    Ipor Sircer over 7 years
    Let's codegolf: sed 's/[^ ]*$/1/' :->
  • user203269
    user203269 over 7 years
    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?
  • Арсений Черенков
    Арсений Черенков over 7 years
    @user203269 awk version works fine for me (albeit with a formating issue)
  • Stéphane Chazelas
    Stéphane Chazelas over 7 years
    awk golfed: awk \$3=1 (POSIX but would not work with the awk from the 70s as found in /bin on Solaris)
  • magor
    magor over 7 years
    that awk solution is really cool....can you explain pls
  • Stéphane Chazelas
    Stéphane Chazelas over 7 years
    @123, like I said, in the OP's expected output, every line ends in two space characters.
  • 123
    123 over 7 years
    Misread thought you said fields were separated by two space and a tab. my bad.
  • user203269
    user203269 over 7 years
    This command prints out the first line in the textfile corrrectly in terminal, but does not make changes in the file...
  • user203269
    user203269 over 7 years
    sed -i 's/[0-9.]*$/1/' File.txt gives this error: sed: 1: "File.txt": invalid command code L
  • Stéphane Chazelas
    Stéphane Chazelas over 7 years
    @user203269, on BSDs, use -i '' instead of -i
  • user203269
    user203269 over 7 years
    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
    user203269 over 7 years
    i '' : It does not give the error , but it does not make any changes to the textfile...
  • user203269
    user203269 over 7 years
    Hi Stephane, This awk does the first three values correctly, then it deletes column1 line 2 and continues without making changes..
  • Stéphane Chazelas
    Stéphane Chazelas over 7 years
    @user203269, convert your file from MS-DOS to Unix first.
  • user203269
    user203269 over 7 years
    Thank you Stephane! How do I do that? :)
  • user1700494
    user1700494 over 7 years
    redirect the output to another file awk '{print $1, $2, "1"}' inputfile > newfile
  • user203269
    user203269 over 7 years
    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 :)
  • kemotep
    kemotep about 6 years
    Could you please edit your post to include more context as to why you feel this is the solution?