Skip to main content
added 2 characters in body
Source Link
choroba
  • 49.4k
  • 7
  • 92
  • 118

Perl to the rescue!

perl -e '
    print for sort { (($a =~ m{.*/([^0-9]*)})[0] cmp ($b =~ m{.*/([^0-9]*)})[0])
                     ||
                     (($a =~ /-([0-9]+)/)[0] <=> ($b =~ /-([0-9]+)/)[0]) } <>
' -- /tmp/foo.txt
  • <> reads the lines of input
  • sort sorts the list based on the given code
  • m{.*/([^0-9]*)} extracts the basename up to a digit (if present)
  • cmp does the string comparison
  • If they are equal, the || or"or" applies the second comparison, where:
  • /-([0-9]+)/ extracts the number
  • <=> does the numeric comparison
  • The (...)[0] construct is needed as matching returns a list of matches (corresponding to $1, $2, etc). List context is needed to get the matches. We're interested in the first match only (as there's no other).

Perl to the rescue!

perl -e '
    print for sort { (($a =~ m{.*/([^0-9]*)})[0] cmp ($b =~ m{.*/([^0-9]*)})[0])
                     ||
                     (($a =~ /-([0-9]+)/)[0] <=> ($b =~ /-([0-9]+)/)[0]) } <>
' -- /tmp/foo.txt
  • <> reads the lines of input
  • sort sorts the list based on the given code
  • m{.*/([^0-9]*)} extracts the basename up to a digit (if present)
  • cmp does the string comparison
  • If they are equal, the || or applies the second comparison, where:
  • /-([0-9]+)/ extracts the number
  • <=> does the numeric comparison
  • The (...)[0] construct is needed as matching returns a list of matches (corresponding to $1, $2, etc). List context is needed to get the matches. We're interested in the first match only (as there's no other).

Perl to the rescue!

perl -e '
    print for sort { (($a =~ m{.*/([^0-9]*)})[0] cmp ($b =~ m{.*/([^0-9]*)})[0])
                     ||
                     (($a =~ /-([0-9]+)/)[0] <=> ($b =~ /-([0-9]+)/)[0]) } <>
' -- /tmp/foo.txt
  • <> reads the lines of input
  • sort sorts the list based on the given code
  • m{.*/([^0-9]*)} extracts the basename up to a digit (if present)
  • cmp does the string comparison
  • If they are equal, the || "or" applies the second comparison, where:
  • /-([0-9]+)/ extracts the number
  • <=> does the numeric comparison
  • The (...)[0] construct is needed as matching returns a list of matches (corresponding to $1, $2, etc). List context is needed to get the matches. We're interested in the first match only (as there's no other).
Source Link
choroba
  • 49.4k
  • 7
  • 92
  • 118

Perl to the rescue!

perl -e '
    print for sort { (($a =~ m{.*/([^0-9]*)})[0] cmp ($b =~ m{.*/([^0-9]*)})[0])
                     ||
                     (($a =~ /-([0-9]+)/)[0] <=> ($b =~ /-([0-9]+)/)[0]) } <>
' -- /tmp/foo.txt
  • <> reads the lines of input
  • sort sorts the list based on the given code
  • m{.*/([^0-9]*)} extracts the basename up to a digit (if present)
  • cmp does the string comparison
  • If they are equal, the || or applies the second comparison, where:
  • /-([0-9]+)/ extracts the number
  • <=> does the numeric comparison
  • The (...)[0] construct is needed as matching returns a list of matches (corresponding to $1, $2, etc). List context is needed to get the matches. We're interested in the first match only (as there's no other).