6

I have a long file with short lines (one word) f that I would like to inspect. It would fit on the screen if it wasn't for the newlines.

If the data was already arranged in columns I could just do column -t but I want my one column to be split into many.

Knowing the length of the file I could just do:

$ sed -n  '1,10p' f > f1
$ sed -n '11,20p' f > f2
$ sed -n '21,30p' f > f3
$ sed -n '31,40p' f > f4
$ sed -n '41,50p' f > f5
$ paste f[1-5]

I imagine one could also make a new directory, apply touch on every line, do ls, delete the directory.

But is there an easy way to do this without creating any files?

marked as duplicate by muru, Kusalananda Apr 12 at 14:38

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

10

Oh, nevermind! column does that:

column f displays newline separated items in file f in columns, which has the same effect as ls on a directory.

  • @SaggingRufus I've edited my answer to include the exact command. As for how I came up with it, I had used column -t in the past to prettyprint files that were already organized into columns and tried column without parameters more to see what happens than because I expected it to work. – user2740 Apr 11 at 13:45
4

Alternatively, there's the fmt utility (check your local man page):

$ seq 100 > f
$ fmt --width 50 file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
99 100

or pr:

$ pr -15 -w 50 -a -s' ' -t file
1  2  3  4  5  6  7  8  9  10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 10
2

With zsh:

$ seq 20 > file
$ print -rC5 -- ${(f)"$(<file)"}
1   5   9   13  17
2   6   10  14  18
3   7   11  15  19
4   8   12  16  20
$ print -raC5 -- ${(f)"$(<file)"}
1   2   3   4   5
6   7   8   9   10
11  12  13  14  15
16  17  18  19  20

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