Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a text file that looks like his:

#c1     c2              c3              c4              c5              c6      c7      c8      c9      c10     c11     c12     c13
4599    995,274 2523,658        264,1417170     -33,6000        -0,3600 0,0202  0,0069  0,0227  10,1041 0,0169  0,2817  0,0239
6173    1552,290        1595,696        264,0603912     -33,4880        0,4374  0,0287  1,8123  0,0385  10,1289 0,0292  2,8445  0,0366
6456    1658,630        1529,292        264,0449377     -33,4800        0,0109  0,0234  1,0955  0,0296  10,5032 0,0223  1,7168  0,0293
8932    2750,029        1374,395        263,8866195     -33,4615        -0,3185 0,0249  0,9196  0,0302  10,5858 0,0221  1,3886  0,0296
4095    820,716 495,315 264,1658451     -33,3546        -0,4388 0,0216  0,6497  0,0252  10,6026 0,0188  1,1796  0,0258
4737    1045,289        1395,983        264,1338240     -33,4636        0,5900  0,0231  0,8853  0,0273  10,6501 0,0204  1,1661  0,0270

ie: not aligned at certain random lines (the file is ~10000 lines long) I need it to look like this:

#c1     c2              c3              c4              c5              c6      c7      c8      c9      c10     c11     c12     c13
4599    995,274         2523,658        264,1417170     -33,6000        -0,3600 0,0202  0,0069  0,0227  10,1041 0,0169  0,2817  0,0239
6173    1552,290        1595,696        264,0603912     -33,4880        0,4374  0,0287  1,8123  0,0385  10,1289 0,0292  2,8445  0,0366
6456    1658,630        1529,292        264,0449377     -33,4800        0,0109  0,0234  1,0955  0,0296  10,5032 0,0223  1,7168  0,0293
8932    2750,029        1374,395        263,8866195     -33,4615        -0,3185 0,0249  0,9196  0,0302  10,5858 0,0221  1,3886  0,0296
4095    820,716         495,315         264,1658451     -33,3546        -0,4388 0,0216  0,6497  0,0252  10,6026 0,0188  1,1796  0,0258
4737    1045,289        1395,983        264,1338240     -33,4636        0,5900  0,0231  0,8853  0,0273  10,6501 0,0204  1,1661  0,0270

So basically I'm looking for a script/command line utility to automatically align the columns of the file.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

Try doing this in a :

To test on STDOUT :

column -t file.txt

To modify the file :

column -t file.txt > new_file.txt && mv new_file.txt file.txt

As you can see, that's all you need. It saves you a lot of time playing with complicated printf tricks.

share|improve this answer
    
This command did nothing, it merely displayed the file in the terminal and ended. What am I missing here? –  Gabriel Feb 19 '13 at 20:13
    
Which OS are you using ? (put it in your original POST) –  sputnick Feb 19 '13 at 20:16
    
I'm using Ubuntu 12.10. –  Gabriel Feb 19 '13 at 20:16
    
Just tried it again, nothing. Should that command be modifying the original file or creating a new aligned one? –  Gabriel Feb 19 '13 at 20:21
    
See my edit to edit the file inplace (my previous version was just to display in a terminal) –  sputnick Feb 19 '13 at 20:26

You are looking for the aptly named column tool.

For what you want you have to pipe your data to column like this

cat yourfile | column -s " " -t

Define the delimiter with -s

-s " "

Tell colume to make a table with -t

share|improve this answer
    
What does this "some data\nfoo bar" mean? –  Gabriel Feb 19 '13 at 20:12
    
That's not what OP asked ;) –  sputnick Feb 19 '13 at 20:13
    
@sputnick, I fixed it. sorry –  Silverrocker Feb 19 '13 at 20:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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