arrange files into columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting arrange files into columns
# 8  
Old 03-12-2011
Code:
[ctsgnb@shell ~/sand]$ cat tst
1
2
3
4
a
b
c
d
9
8
7
6
[ctsgnb@shell ~/sand]$ pr -3 -t tst
1                       a                       9
2                       b                       8
3                       c                       7
4                       d                       6
[ctsgnb@shell ~/sand]$


Last edited by radoulov; 03-13-2011 at 07:28 AM..
This User Gave Thanks to ctsgnb For This Post:
# 9  
Old 03-13-2011
Quote:
Originally Posted by ctsgnb
Code:
[ctsgnb@shell ~/sand]$ cat tst
1
2
3
4
a
b
c
d
9
8
7
6
[ctsgnb@shell ~/sand]$ pr -3 -t tst
1                       a                       9
2                       b                       8
3                       c                       7
4                       d                       6
[ctsgnb@shell ~/sand]$

I always forget about the pr command.
The OP should pre-calculate the number of columns though.
# 10  
Old 03-13-2011
Hi.

Observations on pr:

1) The OP wanted columns of 4,

2) One may be surprised at the ordering of data over a page break.

For example, here is pr with the OP-amended data (includes "5"), and with a shorter page definition:
Code:
#!/usr/bin/env bash

# @(#) s2	Demonstrate splitting file into groups of lines.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { printf " db, ";for i;do printf "%s" "$i";done; printf "\n"; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && . $C pr

FILE=${1-data1}
pl "  Data file $FILE:"

pl " Results of visually splitting file with pr:"
pr -3 -t $FILE

pl " Results of visually splitting file with pr, shorter page length:"
pr -3 -t -l 2 $FILE

exit 0

producing:
Code:
% ./s2

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.7 (lenny) 
GNU bash 3.2.39
pr (GNU coreutils) 6.10

-----
  Data file data1:

-----
 Results of visually splitting file with pr:
1			a			9
2			b			8
3			c			7
4			d			6
5

-----
 Results of visually splitting file with pr, shorter page length:
1			3			5
2			4			a
b			d			8
c			9			7
6

This suggests that for small problems, pr may be a very good answer, but requires some thought before-hand. To address observation one, one would need to try 4 columns, "-4", but that would produce one column of 4, and 3 of 3, so for this requirement , pr is probably not suitable. It looks like pr attempts to balance the content of the output columns visually.

I have not investigated whether pr can handle arbitrarily long page lengths, but I recall being puzzled by output when the output spanned more than one page.

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare 2 csv files by columns, then extract certain columns of matcing rows

Hi all, I'm pretty much a newbie to UNIX. I would appreciate any help with UNIX coding on comparing two large csv files (greater than 10 GB in size), and output a file with matching columns. I want to compare file1 and file2 by 'id' and 'chain' columns, then extract exact matching rows'... (5 Replies)
Discussion started by: bkane3
5 Replies

2. Shell Programming and Scripting

Shell scripting - need to arrange the columns from multiple file into a single file

Hi friends please help me on below, i have 5 files like below file1 is x 10 y 20 z 15 file2 is x 100 z 245 file3 is y 78 z 23 file4 is x 100 (3 Replies)
Discussion started by: siva kumar
3 Replies

3. Shell Programming and Scripting

Combine columns from many files but keep them aligned in columns-shorter left column issue

Hello everyone, I searched the forum looking for answers to this but I could not pinpoint exactly what I need as I keep having trouble. I have many files each having two columns and hundreds of rows. first column is a string (can have many words) and the second column is a number.The files are... (5 Replies)
Discussion started by: isildur1234
5 Replies

4. Shell Programming and Scripting

how to arrange datas in columns in shell script.

Hello All, I have datas in the below format. Mark 45 Steve 60 Johnson 79 Elizabeth 90 My requirement is to arrange the data in the order as shown in the attachment. My OS is Solaris. ... (6 Replies)
Discussion started by: vashece
6 Replies

5. Shell Programming and Scripting

Arrange output based on rows into columns

Hi All, I would like to ask help on how can i achieve below output. Inputfile: Oct11,apa1-daily,01:25:01 Oct11,apa2-daily,01:45:23 Oct12,apa1-daily,02:30:11 Oct12,apa2-daily,01:55:01 Oct13,apa1-off,01:43:34 Oct13,apa2-off,01:22:04 Desired output: Clients ... (3 Replies)
Discussion started by: mars101
3 Replies

6. UNIX for Dummies Questions & Answers

How to arrange the files according to time

hi friends, m new to unix environment.. i want to know that "how could we arrange the various files in our unix system according to time at which they are accessed, and are there are ways that we can arrange them in ascending or descending manner..." please help me out with full details...... (4 Replies)
Discussion started by: adityamitra
4 Replies

7. Shell Programming and Scripting

Arrange log files with AWK

Hello friends, I have too many log files to arrange. I use a simple script to create log files with below format and i forgot to create daily directory for them at the beginning. Because of this i should move all daily logs into a directory that i need to create. a part of "ls -l" output:... (1 Reply)
Discussion started by: EAGL€
1 Replies

8. Shell Programming and Scripting

Shell script to arrange files into several folders

Hello this is the script Im working on I have a picture collection that I rescued from a hard drive and there are thousands of pictures saved in one folder. What I need is to create several folders and put lets say around 200 pictures in each folder. At the end instead of having one huge... (8 Replies)
Discussion started by: kizofilax
8 Replies

9. Shell Programming and Scripting

gawk - reading two files & re arrange the columns

Hi, I am trying to read 2 files and writing to the 3rd file if I find the same elements in 2 files. my first file is 1 0 kb12124819 766409 1.586e-01 1 0 kb17160939 773886 8.674e-01 1 0 kb4475691 836671 8.142e-01 1 0 ... (2 Replies)
Discussion started by: ezhil01
2 Replies

10. Shell Programming and Scripting

re arrange the columns

Hi - can any one let me know how to re-arrange the columns of a comma seperated file. The problem here is that the colums some times have new lines and when the columns has new lines or extra comma's then it is enclosed in double quotes("). Can any one tell me how to re-arrange the columns now. ... (0 Replies)
Discussion started by: ahmedwaseem2000
0 Replies
Login or Register to Ask a Question