在bash上对版本字符串进行排序 [英] Sort version strings on bash

查看:74
本文介绍了在bash上对版本字符串进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

STRINGS.txt的示例内容:

Example content of STRINGS.txt:

    3.0.3
    3.0.11.2
    3.0.11.1
    3.0.11
    3.0.16
    3.0.15.1
    3.0.15
    3.0.14
    3.0.10.3
    3.0.10.2
    3.0.10.1
    3.0.13.1
    3.0.10
    3.0.13
    3.0.9
    3.0.12
    3.0.8
    3.0.7.2
    3.0.7.1
    3.0.7
    3.0.9.2
    3.0.9.1
    3.0.2
    3.0.8.1
    3.0.6.1
    3.0.6
    3.0.5
    3.0.1
    3.0.0

是否可以仅使用bash对所有这些版本字符串进行排序,而最新版本位于顶部?

Is it possible to sort all those version strings only with bash, that the latest is on the top?

推荐答案

这是可行的,但工作量却很愚蠢.如果您使用GNU排序:

It's possible, but a silly amount of work. If you have GNU sort:

sort -V -r <STRINGS.txt

...将完全满足您的要求.

...will do exactly what you're asking for.

现在,如果您真的是没有外部工具的意思,那么您就遇到了麻烦. Freenode的#bash IRC频道上的BlastHardcheese已在本机bash中编写了以下quicksort算法,出于可读性的考虑,我对其进行了修改,以使compare函数具有可替换性,并使用Bash 4.3 namevars使其能够与可配置的变量名(当然,后者的更改意味着需要一个非常新的bash版本):

Now, if you really mean with no external tools, then you're getting into some trouble. BlastHardcheese on Freenode's #bash IRC channel has written the following quicksort algorithm in native bash, which I've modified for readability, to factor out the compare function to be replacible, and to use Bash 4.3 namevars to be able to work with a configurable variable name (of course, this latter change means that a very new version of bash is required):

# this needs to be replaced for this particular case
compare(){
  (( $1 >= $2 ))
}

swap(){
  declare -n a=$1
  local t
  t=${a[$2]}
  a[$2]=${a[$3]}
  a[$3]=$t
}

partition(){
  declare -n a=$1
  local c p x
  p=${a[$4]}
  c=$2
  swap "$1" "$3" "$4"
  for((x=$2;x<$3;x++)); do
    if ! compare "${a[x]}" "$p"; then
      swap "$1" "$x" "$c"
      ((c++))
    fi
  done
  swap "$1" "$2" "$c"
  n=$c
}

quicksort(){
  declare -n a=$1
  (( "$2" >= "$3" )) && return
  local i n
  i=$((($2+$3)/2))
  partition "$1" "$2" "$3" "$i"
  quicksort "$1" "$2" "$((n-1))"
  quicksort "$1" "$((n+1))" "$3"
}

...实现您自己的比较功能,然后就可以采用了.

...implement your own comparison function, and this is then adoptable.

仅处理您在此处显示的情况:

To handle only the cases you've shown here:

# we want to return 0 if the first version is equal or later than the second
version_compare(){
  local -a first second

  # Let's start with trivial cases:
  if [[ $1 = "$2" ]] || [[ $1 = "$2".* ]]; then : "$1 >= $2"; return 0; fi

  IFS=. read -r -a first <<<"$1"
  IFS=. read -r -a second <<<"$2"

  local k
  for k in "${!first[@]}"; do
    local a=${first[$k]} b=${second[$k]}
    : "Evaluating field $k ($a vs $b)"
    if [[ ! $b ]]; then
      # ie. first=1.1.1, second=1.1; though this should have been handled above
      : "$1 >= $2"; return 0;
    fi
    if (( $b > $a )); then
      : "$1 < $2"; return 1;
    fi
  done

  : "$1 >= $2"; return 0;
}
compare() {
  version_compare "$2" "$1" # reverse sort order
}

假设bash 4是做文件IO:

To do the file IO, assuming bash 4:

readarray -t versions <STRINGS.txt
quicksort versions 0 "$(( ${#versions[@]} - 1 ))"
printf '%s\n' "${versions[@]}"

这篇关于在bash上对版本字符串进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆