1

I have a file looks like this

0 1 2 3 4 1 0 0
1 2 3 5 0 1 0 0

I want to add more (multiple) space between the columns and get something like this

0      1      2      3      4
1      2      3      5      0   

I tried something with column -t, but still it does not give the space that I want.

  • Try col -x? It uses spaces rather than tabs. – DopeGhoti May 15 '17 at 23:10
  • 2
    what happened to the last few columns? 1 0 0 from line 1 and 1 0 0 from line 2? – Jeff Schaller May 15 '17 at 23:14
  • @DopeGhoti, may as well throw that in an answer. – Wildcard May 15 '17 at 23:57
  • Have you tried the -o flag? eg column -o ' ' -t file.txt – DarkHeart May 16 '17 at 6:02
  • Do any of the provided answers solve your problem? If so, don't forget to mark one with the checkmark; thank you! – Jeff Schaller May 21 '17 at 11:24
2

With coreutils, you could convert spaces to tabs with tr, and then convert tabs back to multiple spaces with expand:

$ tr ' ' '\t' < file | expand
0       1       2       3       4       1       0       0
1       2       3       5       0       1       0       0

The default tab stops are 8 spaces, but you can adjust that e.g.

$ tr ' ' '\t' < file | expand -t 10
0         1         2         3         4         1         0         0
1         2         3         5         0         1         0         0
1

Some more ideas that will work with your sample data:

$ a="0 1 2 3 4 1 0 0"

$ echo "$a"
0 1 2 3 4 1 0 0

$ echo "${a// /                 }"
0                 1                 2                 3                 4                 1                 0                 0

$ echo "$a" |sed 's/ /             /g'
0             1             2             3             4             1             0             0

$ echo "${a// /$'\t'}"
0   1   2   3   4   1   0   0

$ echo "$a" |sed 's/ /\t\t/g'
0       1       2       3       4       1       0       0
0

to format the columns evenly, you could use awk:

awk '{ for(i=1;i <= NF; i++) { printf "%-7s", $i } print "" }' < input > output
0
sed -E 's/\S+\s/&     /g' your_monospace.file

awk -v OFS="     " '{$1=$1}1' your_monospaced.file

perl -pale '$_ = join $" x 5, @F' your_monospaced.file
0

Try col -x? It uses spaces rather than tabs.

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.