How to sort alphanumeric numbers in the shell

The input file has the following data:

abc90
cd 18
bc14de
100def

      

The output should be:

bc14de
cd 18
abc90
100def

      

Is there any command sort

for sorting only by number embedded in alphanumeric data?

I tried this but it doesn't work the way I want:

# sort -g FileName

      

+3


source to share


1 answer


You can use:

awk -v OFS='\t' '{rec=$0; gsub(/[^[:digit:]]+/, "", rec); print rec, $0}' file
    | sort -nk1 | cut -d $'\t' -f2-
bc14de
cd 18
abc90
100def

      



  • awk

    used to add first column of input with numeric characters only with gsub

  • sort -nk1

    used to sort input numerically in the first column
  • cut

    finally used to truncate the first column
+4


source







All Articles