8

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?

16
awk '{print $1, $2, "1"}' inputfile
  • 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
11

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.
  • 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
5

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
  • 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
3

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.

2

this will do the job:

cat textfiles | cut -d' ' -f-2 | sed 's/$/ 1/'
  • 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
-1
cat filename | awk -F ' ' '{$3=1; print $0}' > filename
  • Could you please edit your post to include more context as to why you feel this is the solution? – kemotep Apr 11 '18 at 22:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.