Score:0

How to add another column

ng flag

I am trying to turn a file like this to something at the bottom. I am unsure of how to use awk or paste to go about this.

0.0000000000  0.0000000000  0.0000000000 
0.7804643317  0.5703578412   0.0000000000 
-0.7804643317  0.5703578412   0.0000000000 

I would be dealing with a changing number of rows for the file I'm trying to transform.

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
cn flag
Ray
I think you meant "the number of rows would stay at three, but the number of columns would change"?
Score:4
in flag

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
Score:2
cn flag

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
Score:0
mm flag

Added help from: https://unix.stackexchange.com/questions/11801/replace-all-white-spaces-with-commas-in-a-text-file

$ cat text.file | sed 's/[[:blank:]]\+/\ 0\ /g'

 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 prettify with column:

$ cat text.file | sed 's/[[:blank:]]\+/\ 0\ /g' | column -t -e

 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

Beware this accounts for empty spaces at the end of the string. Might want to filter with a prior sed.

 $ cat text.file | sed 's/[[:blank:]]\+$//g' | sed 's/[[:blank:]]\+/\ 0\ /g' | column -t -e

 0.0000000000   0  0.0000000000  0  0.0000000000 
 0.7804643317   0  0.5703578412  0  0.0000000000
 -0.7804643317  0  0.5703578412  0  0.0000000000
terdon avatar
cn flag
Note that you don't need cat there. You can give the file to sed as an argument. You also don't need to escape the spaces.
bac0n avatar
cn flag
You probably want to use something like `[[: blank:]]\+\|$` (although there are spaces at the end of the example, newline is probably more common).
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.