1

I have a text file that consists of data in this form:

int array[0]
int array[1001]
int array[1002]
int array[1003]
int array[2]
int array[2001]
int array[2002]
...

In other words, the numbers appear to be listed in some sort of lexicographic ordering. I want to sort the file so its contents are as follows:

int array[0]
int array[1]
int array[2]
int array[3]
int array[4]
int array[5]
int array[6]
...

How would I perform this sorting transform? sort does not appear to be sufficient for the task.

2

You can sort it numerically on the [ delimiter:

sort -t\[ -nk2,2 <<\IN
int array[0]
int array[1001]
int array[1002]
int array[1003]
int array[2]
int array[2001]
int array[2002]
IN 

OUTPUT

int array[0]
int array[2]
int array[1001]
int array[1002]
int array[1003]
int array[2001]
int array[2002]
1

For situations where you have text and numbers, an alternative is the --version-sort

sort -V

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.