Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SORT from start char pos to end char pos within record 1

Status
Not open for further replies.

Calator

Programmer
Feb 12, 2001
262
AU
Hi,

I need to sort a file from a start character position to an end character position within record, eg:

sort using a key specified as character positions 5-12 within the record

Can this be achieved with the Unix sort command? The entire record should be regarded as one field irrespective of embedded blanks (ie sort should not attempt to split my record into fields based on existing blanks!). I'm afraid that:

sort -k 1.5,1.12 infile

may not work ok

anyone already faced this issue and able to share a tested solution? Thanks
 
Code:
`-t SEPARATOR'
`--field-separator=SEPARATOR'
     Use character SEPARATOR as the field separator when finding the
     sort keys in each line.
Pick a character which isn't in your file, and use that as a separator. sort should then view each line as a single record.
 
You can always use gawk for a start.
Code:
function selectrangeSort() {
awk -v v1=$1 -v v2=$2 '{printf "%s\n", substr($0,v1,v2)}' $3 
}

More extravagantly:
function selectrangeSort() {
awk -v v1=$1 -v v2=$2 ' {array[i++] = substr($0,v1,v2)}
END { asort(array) ; for (m=1 ; m <= i ; m++) {print array[m]}}' $3
}
 
Try this:

sort +0.5 infile




----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top