Printing in columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printing in columns
# 8  
Old 05-15-2012
In this particular case:
Code:
awk 'END{printf "\n%s\n",n} getline p{n=n (n?ORS:x) p}1' ORS='\t' infile

General case perhaps:
Code:
xargs -n 1 < infile | awk '{n=(NR-1)%r; R[n]=R[n] (R[n]?"\t":x) $1} END{for(i=0;i<r;i++)print R[i]}' r=2

r is the number of rows...

Last edited by Scrutinizer; 05-15-2012 at 12:22 PM..
# 9  
Old 05-15-2012
Hi.

Although command column might not be on some UNIX boxes, I'd rather have a limited general facility like column, rather than a plethora of single-shot solutions for lots of similar, but different situations.

The perl module Array::columnize allows one to create a work-alike. We use one such for the cases when column might not exist on a platform, like so:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate columnize with standard GNU/Linux column, local my-columns.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
edges() { local _f _n _l;: ${1?"edges: need file"}; _f=$1;_l=$(wc -l $_f);
  head -${_n:=3} $_f ; pe "--- ( $_l: lines total )" ; tail -$_n $_f ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C column my-columns divepm

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Results, standard Linux column:"
column -c60 $FILE

pl " Results, work-alike for Linux column, \"my-columns\":"
my-columns -w 60 $FILE

pl " Characteristics of my-columns:"
v1=$( command -v my-columns )
file "$v1" | sed 's/^.*: //' 
pe "Lines: $( wc -l < $( command -v my-columns ) )"
pe "Modules used by my-columns:"
divepm -q -i $v1

exit 0

producing:
Code:
% ./s1

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.8 (lenny) 
bash GNU bash 3.2.39
column - ( /usr/bin/column, 2007-11-20 )
my-columns (local) 1.2
divepm (local) 1.2

-----
 Input data file data1:
apples
oranges
cats
dogs
sky
monkey
giraffe
cups
spoons
tv
cable

-----
 Results, standard Linux column:
apples	cats	sky	giraffe	spoons	cable
oranges	dogs	monkey	cups	tv

-----
 Results, work-alike for Linux column, "my-columns":
apples   cats  sky     giraffe  spoons  cable
oranges  dogs  monkey  cups     tv    

-----
 Characteristics of my-columns:
a perl script text executable
Lines: 67
Modules used by my-columns:
 1.06	warnings
 1.04	strict
 1.04	English
 1.08	Carp
 2.37	Getopt::Long
 0.4.2	Array::Columnize
 1.11	feature

I encourage other organizations to do the same, perhaps contrinuting to The Comprehensive Perl Archive Network - www.cpan.org and possibly to PPT: Unix Reconstruction Project

Best wishes ... cheers, drl
# 10  
Old 05-15-2012
try xargs

Code:
xargs -n 10 < filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing multiple columns from a file

Hi, I need just to print the values of second and fourth column from a file # cat dispaly id Name Std Specialist 1 sss X mathematics 2 uyt IX geography 3 vcd X English i tried with some NF command.. I think am wrong.. Is there anyother way to print my requirement (3 Replies)
Discussion started by: Priya Amaresh
3 Replies

2. UNIX for Dummies Questions & Answers

Printing all the values in the middle of two columns

Hi, I have a tab delimited text file with three columns: Input: 1 25734 25737 1 32719 32724 1 59339 59342 1 59512 59513 1 621740 621745 For each row of the text file I want to print out all the values between the second and third columns, including them. The... (3 Replies)
Discussion started by: evelibertine
3 Replies

3. UNIX for Dummies Questions & Answers

Printing columns with header

Hi Gurus, I want to extract certain columns from file 2 and combine with file 1. I am using the following script to extract the columns. $ awk 'FNR>1{print $2, $9, FILENAME}' *.lim > out1 However, this script does not print the titles of the columns 2 and 9. Can somebody help me in... (1 Reply)
Discussion started by: Unilearn
1 Replies

4. Shell Programming and Scripting

Printing Fixed Width Columns

Hi everyone, I have been working on a pretty laborious shellscript (with bash) the last couple weeks that parses my firewall policies (from a Juniper) for me and creates a nifty little columned output. It does so using awk on a line by line basis to pull out the appropriate pieces of each... (4 Replies)
Discussion started by: cixelsyd
4 Replies

5. Shell Programming and Scripting

Comparing Columns and printing the difference from a particular file

Gurus, I have one file which is having multiple columns and also this file is not always contain the exact columns; sometimes it contains 5 columns or 12 columns. Now, I need to find the difference from that particular file. Here is the sample file: param1 | 10 | 20 | 30 | param2 | 10 |... (6 Replies)
Discussion started by: buzzusa
6 Replies

6. Shell Programming and Scripting

Printing columns

Im using awk to print columns. Basically I have a file with like 500 columns and I want to print the 200th-300th column and ignore the rest... how would I do it without putting $200, $201 .... $300 thanks (6 Replies)
Discussion started by: kylle345
6 Replies

7. UNIX for Dummies Questions & Answers

Perl, printing a string into columns

How can I use Perl to a take a string of 10 characters and print the last five characters of the string in columns 1-5 and the first five in columns 6-10? Result: 0123456789 5 0 6 1 7 2 8 3 9 4 (5 Replies)
Discussion started by: doubleminus
5 Replies

8. UNIX for Dummies Questions & Answers

awk printing all columns after (but including) $n

I am piping an "ls -l" to awk so that all it returns is the file size, date, and file name. The problem is that some files may have spaces in the name so awk is only printing the first word in the file name. I won't know how many space-delimited words are in the filename, so what I want to do is... (2 Replies)
Discussion started by: cassj
2 Replies

9. Shell Programming and Scripting

Printing Columns in Unix

ok. this is a bit of a difficult question but i've been trying to figure this out for quite some time but couldn't. how do I print columns on the screen? like take for instant. using the ls and the file command, how do i print it so i can have the filenames on the left hand side and the... (3 Replies)
Discussion started by: Terrible
3 Replies

10. Shell Programming and Scripting

printing columns

Here's what I wrote: #!/bin/sh d1=`grep Dialtone dialtone | awk '{print $2, $3, $4, $5, $6, $7, $8, $9}'` d2=`grep pstsys dialtone | awk '{print $12}'` echo "$d1 $d2" I expected the result to be this: Dialtone on host 1 slot 13 port 1, pstsys05 Dialtone on host 1 slot 13 port 1,... (3 Replies)
Discussion started by: cdunavent
3 Replies
Login or Register to Ask a Question