Visit Our UNIX and Linux User Community


Vertical sorting of columns


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Vertical sorting of columns
# 8  
Old 08-07-2009
Hi.

Here is a perl solution on OS X:
Code:
#!/usr/bin/env bash

# @(#) s2	Demonstrate sort items on a line, perl

echo
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) perl
set -o nounset
echo

FILE=${1-data1}

echo " Data file $FILE:"
cat $FILE

echo
echo " Results:"
perl -n -e 'print join(" ",sort({$a<=>$b} split)),"\n";' $FILE

exit 0

producing:
Code:
$ ./s2

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OSX 10.3.9
GNU bash 2.05b.0
perl 5.8.1

 Data file data1:
a b d f c e
1 4 10 16 7 13
2 5 11 17 8 14
3 6 12 18 9 15

 Results:
a b d f c e
1 4 7 10 13 16
2 5 8 11 14 17
3 6 9 12 15 18

Note that this is a numeric sort, which seems to be the bulk of the problem. Adding an alpha sort for the first line to perl would make it too long for a convenient one-line command. You could strip and sort the first line separately if that's really important.

I also wrote a version with gawk, but my OS X does not have gawk. However, if yours does, here is the code:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate sort items on a line, GNU awk, "gawk"

echo
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) awk
set -o nounset
echo

FILE=${1-data1}

echo " Data file $FILE:"
cat $FILE

echo
echo " Results:"
awk '
	{ split($0,a) ; n = asort(a)
	  for (i=1 ; i <= n ; i++ ) {
	    printf("%s%s", a[i],FS)
	  }
	print ""
	}
' $FILE

exit 0

on Debian GNU/Linux this produces:
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 
GNU bash 3.2.39
GNU Awk 3.1.5

 Data file data1:
a b d f c e
1 4 10 16 7 13
2 5 11 17 8 14
3 6 12 18 9 15

 Results:
a b c d e f 
1 4 7 10 13 16 
2 5 8 11 14 17 
3 6 9 12 15 18

The gawk intepreter is useful because it contains a sort builtin, asort, so that one need not fire off a separate process for each line.

Best wishes ... cheers, drl
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #327
Difficulty: Easy
The Windows 7 operating system has seven main editions.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sorting by columns

Hi, I have a tab delimited columnar file where I want to remove lines wherever two particular columns match. so for this file, I want to toss the lines where columns 1 and 2 match: a a 1 3 a b 2 4 b b 3 5 because there are matches column 1 and 2 in lines 1 and 3, I would like a script to... (2 Replies)
Discussion started by: mikey11415
2 Replies

2. UNIX for Dummies Questions & Answers

Sorting rows to columns

Dear all, I need your help to sort out a file with more then 15, 000 rows, input file has following format : AT4560 GO:1289GO:8915GO:9243GO:5739GO:6757GO:9245GO:9507output should be like: AT4560 GO:1289 AT4560 GO:8915 AT4560 GO:9243 AT4560 GO:5739 AT4560 GO:6757 AT4560 GO:9245... (5 Replies)
Discussion started by: AAWT
5 Replies

3. Shell Programming and Scripting

CSV Sorting on only particular columns

Hello! So ive been presented with this comma-delimited file: I need a print to look as below " lastname, phone_number, zip for every person with a last name starting with the letter H, I only with a 650-area code phone number. output should be sorted by reverse ZIP code " I only have... (5 Replies)
Discussion started by: strangemachine
5 Replies

4. Shell Programming and Scripting

Help in sorting multiple columns

Hello all, I am using printf to print the sorted o/p in my script.I am trying to sort in following way but doesn't work. printf "%13s %2s UDP %15s:%s Program %4s HD: %23s HD: %23s %10s %s %s %3s days %3s hours\n" $encoder $i "${ipaddr}" ${portno} ${progno} ${inres} ${outres} ${inrate}... (4 Replies)
Discussion started by: ramman
4 Replies

5. UNIX for Dummies Questions & Answers

Sorting by Multiple Columns

Hi I have a text file that has four columns (Logonid,First Name,Last Name,Status) Logonid First Name Last Name Status abc2 Fred Mercury Inactive abc1 John Deacon Active abc3 Roger Taylor Active abc4 Brian ... (2 Replies)
Discussion started by: theref
2 Replies

6. Shell Programming and Scripting

sorting and adding columns

i have a file with two columns, and i want to uniquely sort the values in fist column and add the corresponding values in the second columns eg file a contents tom 200 john 300 sow 500 tom 800 james 50 sow 300 output shpould be in file b as tom 1000 john 300 sow 800 james 50 (0 Replies)
Discussion started by: dealerso
0 Replies

7. UNIX for Dummies Questions & Answers

Sorting by multiple columns

I have a space delimited text file that I would like to sort by multiple columns. First I want to sort by column 1, then by column 2. Both columns are numerical. Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

8. Shell Programming and Scripting

Sorting multiple columns

Hi, We have a requirement of need to sort a file based on fields 1,3 and 4. I tried with sort command however it is not giving expected output, can we achieve any other way? Please let me know ASAP. File a e w a a b a a a a d g a a h h c d a e a a a w Output a b a a a a a w a a d... (4 Replies)
Discussion started by: Nagapandi
4 Replies

9. UNIX for Advanced & Expert Users

sorting of varchar columns

Hi , I need to sort a file based on multiple columns All the columns are of varchar type can any one give me the command to sort for varchar columns? Thanks (3 Replies)
Discussion started by: laxmi131
3 Replies

10. Shell Programming and Scripting

Sorting based on columns

Hi, I want a list of entries in 3 space delimited columns. I want to sort entries based on the very first column. Rows can't be changed. For example: If I have... Abc Abc Acc Bca Bda Bdd Cab Cab Cbc Dbc Dca Dda Abc Abc Acc the output should be... Abc Abc Acc Abc Abc Acc Bca... (7 Replies)
Discussion started by: MobileUser
7 Replies

Featured Tech Videos