Sort each column independently


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sort each column independently
# 8  
Old 03-26-2014
Well, this is going to sort the whole file. I want to sort the columns on their own.
# 9  
Old 03-26-2014
Try this:
Code:
#!/bin/ksh
IAm=${0##*/}
base="${IAm}_$$_"
trap 'rm -f "$base"*' EXIT
file=${1:-file}
nf=1
while [ 1 ]
do	tmpname=$(printf "%s%03d" "$base" $nf)
	cut -f $nf "$file" > "$tmpname"
	if ! grep -Fq '|' "$tmpname"
	then	rm -f "$tmpname"
		((nf--))
		break
	fi
	((nf++))
	sort -t '|' -k3,3n -o "$tmpname" "$tmpname"
done
if [ $nf -gt 0 ]
then	paste "$base"*
else	exit 1
fi

This was tested using the Korn shell, but should work with any shell that recognizes basic POSIX shell syntax and variable expansion requirements.

The input samples you provided did not include any tab characters, so this didn't work. But when I replaced the sequences of spaces between columns in your sample files with tabs, I got the following outputs, respectively:
Code:
0.269330|0.035118|0.526763|0.792274	0.000000|0.030234|0.000000|1631
3.981490|5.062725|17.190744|111	0.33555|19.471911|51.844968|1631

and:
Code:
43|33|20|21	34|21|50|80
12|40|30|60	21|32|60|70
2|3|40|50	54|23|70|56

This User Gave Thanks to Don Cragun For This Post:
# 10  
Old 03-26-2014
It worked after all. Thanks for your help Don Cragun
# 11  
Old 03-26-2014
How about cutting in two (with awk that takes white space), then sorting, then pasting?
Code:
paste <( <file awk '{print $1}' | sort -t '|' -k3,3n ) <( <file awk '{print $2}' | sort -t '|' -k3,3n )

# 12  
Old 03-26-2014
The problem was that I had too many columns
# 13  
Old 03-26-2014
If you can afford to keep the data in memory, the following approach should me more efficient:
Code:
awk '
{
	for (i=1; i<=NF; i++)
		a[NR,i] = $i
}

END {
	tmp_file = "tmp"
	sort_cmd = "sort -t\\| -k3,3n > " tmp_file

	# Filter each column through sort.
	for (i=1; i<=NF; i++) {
		for (j=1; j<=NR; j++) {
			print a[j,i] | sort_cmd
		}
		close(sort_cmd)
		j=0
		while (getline x < tmp_file == 1)
			a[++j,i] = x
		close(tmp_file)
	}

	# Print
	for (i=1; i<=NR; i++) {
		for (j=1; j<=NF; j++)
			printf "%s%s", (j>1 ? "\t" : ""), a[i,j]
		print ""
	}
}
' filename

Regards,
Alister
This User Gave Thanks to alister For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use sort to sort numerical column

How to sort the following output based on lowest to highest BE? The following sort does not work. $ sort -t. -k1,1n -k2,2n bfd.txt BE31.116 0s 0s DOWN DAMP BE31.116 0s 0s DOWN DAMP BE31.117 0s 0s ... (7 Replies)
Discussion started by: sand1234
7 Replies

2. UNIX for Dummies Questions & Answers

Custom sort on a column

Hello all, How do I achieve this? I have A, B and A/B in different variables in a file in col2. I want them to sort in such a way, that the variables appear together, and within a variable, the data is sorted in the order A,B and then A/B. If I sort on the second column, the order becomes A,... (6 Replies)
Discussion started by: senhia83
6 Replies

3. Shell Programming and Scripting

Sort on column

How to sort based on the 4 the column . The input data has a header and output needs to be sorted based on the 4th column rbcid. I tried below code but not getting results sort -u -t'|' -k4,4r file1 > file2 time|tourit|nofdays|rbcid|blank|type|value|nill|valuedesc|name... (6 Replies)
Discussion started by: samrat dutta
6 Replies

4. Shell Programming and Scripting

How to execute same script independently?

Hello All, I have one script for example a1.sh. I want to execute the same a1.sh script inside a1.sh script at the end with different parameter such that the second one will run independently. I don't want the second one to take any input from 1st a1.sh. I tried few things like background/exec... (7 Replies)
Discussion started by: Vikram_Tanwar12
7 Replies

5. UNIX for Dummies Questions & Answers

Sort command in one column and not effect to another column

If my data is numerical : 1 = 101 2 = 102 3 = 104 4 = 104 7 = 103 8 = 103 9 = 105 I need the result like below: 1 = 101 2 = 102 3 = 103 4 = 103 7 = 104 8 = 104 9 = 105 (4 Replies)
Discussion started by: GeodusT
4 Replies

6. Shell Programming and Scripting

Sort data As per first Column

hI I have file A NSU30504 5 6 G 6 NSU3050B T 7 9 J NSU30506 T I 8 9 NSU3050C H J K L Output: NSU3050B T 7 9 J NSU3050C H J K L NSU30504 5 6 G 6 NSU30506 T I 8 9Video tutorial on how to use code tags in The UNIX and Linux Forums. (13 Replies)
Discussion started by: pareshkp
13 Replies

7. UNIX for Dummies Questions & Answers

Sort on one column only

Hello, I am running on AIX.I have a question about sorting in UNIX. if my file is something like this: a c b d a b b c a a I want to sort on column 1 only. The following statement does not seem to work, it still considers the rest of the line in the sorting results: sort... (2 Replies)
Discussion started by: gio001
2 Replies

8. Shell Programming and Scripting

sort on second column only based on first column

I have an input file like this... AAAlkalines Energizer AAAlkalines Energizer AAAlkalines Energizer AAAlkalines Sunlight AAAlkalines Sunlight AAAlkalines Sunlight AAAlkalines Energizer AAAlkalines Energizer AAAlkalines Energizer AAASalines ... (7 Replies)
Discussion started by: malcomex999
7 Replies

9. Shell Programming and Scripting

Question about sort specific column and print other column at the same time !

Hi, This is my input file: ali 5 usa abc abu 4 uk bca alan 6 brazil bac pinky 10 utah sdc My desired output: pinky 10 utah sdc alan 6 brazil bac ali 5 usa abc abu 4 uk bca Based on the column two, I want to do the descending order and print out other related column at the... (3 Replies)
Discussion started by: patrick87
3 Replies

10. Shell Programming and Scripting

Sort a particular column in a file

Dear All, Good day. Here i am facing some problem like below. file contains 12345 0001 090112 14385 0001 090112 13255 0001 090112 11345 0001 090112 .... I want to sort ascending according to the first column. What will be the shell script. (4 Replies)
Discussion started by: saifurshaon
4 Replies
Login or Register to Ask a Question