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 am trying to get the input of a file to generate me a list with rows and columns of that numbers in sorted order. This will be use to generate a printable document later, so i am able to replace the steps currently used manually in Excel.

Raw data are integers between 1 and 20000. suppose either:

1 2 3 4 5 6 7 8 ...

or

1
2
3
4
5

The List is sorted by numeric value and seperated by whitespaces. Optionally i can seperate the values by \n or anything else if that helps me get me to my goal

The column amount should be 7, but most likely i will be able to change that myself later on.

My wanted output:

1 40 80
2 41 81 
3 42 82 
4 43 83

What i tried while hoping to figure it out during reading some unix.se questions, other suggestions on SF, manpage of column, fml etc.

for value in $(cat /tmp/list); do      printf "%-8s\n" "${value}"; done|column -x

output (simplified, tab character between):

1 2 3 4
5 6 7 8
...

Next try was a suggestion on Serverfault:

cat /tmp/list |fmt |column -t

output:

1    2    3    4    5 
6    7    8    9    10

I would like to understand where my issue with understanding during the process is, not just the raw solution (which would help though).

share|improve this question
2  
Can you provide a sample of the input file in your question? –  stevieb Aug 11 at 16:19
    
Most solutions are going to turn every n lines into a row of n columns, because that doesn't require any memory. What you want requires reading the whole input to know what should be in the last column of the first row. Thus, tools like pr are what you need. –  Peter Cordes Aug 11 at 17:17
1  
Very similar to Use paste with row-major input –  don_crissti Aug 11 at 22:37
    
@don_crissti correct, seems it is the same i want. –  Dennis Nolte Aug 12 at 7:14

2 Answers 2

up vote 4 down vote accepted
echo {1..20000} | tr " " "\n" | sort -n | pr -T3 -s" " -l 6667

Output:

1 6668 13335
2 6669 13336
3 6670 13337
4 6671 13338
5 6672 13339
6 6673 13340
7 6674 13341
8 6675 13342
9 6676 13343
10 6677 13344
11 6678 13345
.
.
.

To test with your file:

tr " " "\n" < file | sort -n | pr -T3 -s" " -l 6667

It does not matter if the 20000 numbers are all in a row or in a column.

share|improve this answer
    
looks nice, will test this tomorrow, thanks for the "pr" tool –  Dennis Nolte Aug 11 at 18:32
    
pr -7 -s' ' -t -l 80 most likely will be the one i use. –  Dennis Nolte Aug 12 at 7:17

Supposing your input consists of newline-separated numbers, such as:

1
2
3
...

it is a matter of a simple sed script to put them into columns:

$ sed -n '{N;N;N;N;N;N;s/\n/\t/g;p}'

When the script encounters a line, it reads 6 more lines (N;N;N;N;N;N) into the buffer, replaces each newline within the buffer for a tab (s/\n/\t/g) and prints the buffer out (p). The result:

1    2    3    4    5    6    7
8    9    10   11   12   13   14
...

Note: The number of lines in the input needs to be a multiple of seven, any remaining entries will be dropped.

share|improve this answer
    
Thank you for your answer. However it seems i was unclear in how the output has to be formatted. I need the integers as following: f.e. row 1: 1 100 200 300 400 500 600 700 Row 2: 2 101 201 301 401 501 601 this is the problem i am facing right now. Adding the output based on f.e. 7 values in one row could be a matter of a simple for loop with an echo. –  Dennis Nolte Aug 11 at 18:31

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.