Skip navigation.
Home
Your source for Perl tips, howto's, faq and tutorials
( categories: )

Hello you have a great website here. I need help big time. I Need to sort a 2 col file. I am new to perl and this site, I want to thank you in advance for any help on this problem.... Col 1 is a group name, Col 2 is X,Y coordinates need to sort within each group by X value ascending, then for each Value of X sort the Y values are sorted alternating between asc & descending sort on change of X value.
Starting data =====End result in new file


Start File Result in new file
'aaa' 1,2 'aaa' 1,2
'aaa' 2,1 'aaa' 2,3
'aaa' 2,3 'aaa' 2,1
'aaa' 3,1 'aaa' 3,1
'aaa' 3,2 'aaa' 3,2
'aaa' 4,1 'aaa' 4,5
'aaa' 4,5 'aaa' 4,1
'bbb' 2,2 'bbb' 2,1
'bbb' 2,5 'bbb' 2,2
'bbb' 2,1 'bbb' 2,5
'bbb' 4,3 'bbb' 4,6
'bbb' 4,6 'bbb' 4,3
'bbb' 4,1 'bbb' 4,2
'bbb' 4,2 'bbb' 4,1
'ccc' 3,3 'ccc' 1,1
'ccc' 3,6 'ccc' 1,3
'ccc' 1,3 'ccc' 2,4
'ccc' 1,1 'ccc' 2,2
'ccc' 6,4 'ccc' 3,3
'ccc' 6,6 'ccc' 3,6
'ccc' 2,2 'ccc' 6,6
'ccc' 2,4 'ccc' 6,4


Using cut command for columns

#! /bin/ksh -f

usage="Usage: rcut -d -f "
while getopts ":d:f:" opt; do
case $opt in
d ) delim=$OPTARG ;;
f ) excludeColumns=$OPTARG ;;
\? ) echo $usage
exit 1 ;;
esac
done
shift $(($OPTIND -1))

if [ -z $delim ] || [ -z $excludeColumns ]; then
echo $usage
exit 1
fi

#Don't have to sort, KornShell does it for you!
set -sA names `echo $excludeColumns | sed -e 's/,/ /g'`

myStr=""
last=0
from=0
to=0
for i in "${names[@]}";do
((from=$last+1))
((to=$i-1))
((last=$i))
if [ $i -gt $from ]; then
myStr=$myStr$from-$to,
fi
done
((last=$last+1))
myStr=$myStr$last-

cut -d${delim} -f$myStr $1

WOW..that looks complicated

Wow thank you !!!
Can you explain some of the key areas, so that I may learn from this, example.

Also if the input file is In.txt and the output is Out.txt, how do I actually run the program on the file In.txt to get the result into Out.txt file.

thank you rbinkley