How to add another column

Solution 1:

I'd use sed:

sed -i -E 's/([^ ]+)/\1 0/g' file

or if the file is tab-delimited:

sed -i -E 's/([^\t]+)/\1\t0/g' file
  • -i edit file inplace.
  • s/pattern/replacement/ means substitute pattern with a replacement string.

OR awk

awk -i inplace '{for (f=1;f<=NF; f++){$f=$f" 0"}}1' file

or

awk -i inplace '{for (f=1;f<=NF; f++){$f=$f"\t0"}}1' file

However, most implementations do not support -i inplace. Then, you need to write your output to a temp file and overwrite the original file with that:

awk '{for (f=1;f<=NF; f++){$f=$f" 0"}}1' file > file.tmp && mv file.tmp file

Solution 2:

A Perl approach:

$ perl -ane 'print "$_ 0 " for @F; print "\n"' file
0.0000000000 0 0.0000000000 0 0.0000000000 0 
0.7804643317 0 0.5703578412 0 0.0000000000 0 
-0.7804643317 0 0.5703578412 0 0.0000000000 0 

Or, to edit the original file:

perl -i -ane 'print "$_ 0 " for @F; print "\n"' file