在Bash中,如何将数字列表转换为数字范围?

时间:2012-12-04 17:38:18

标签: arrays bash numbers range

目前,我有一个命令的数字排序输出:

  

18,19,62,161,162,163,165

我想将这些数字列表压缩成单个数字或数字范围列表

  

18-19,62,161-163,165

我想过尝试在bash中对数组进行排序并读取下一个数字以查看它是否为+1 ...我有一个基本相同的PHP函数,但我无法将其转换为击:

foreach ($missing as $key => $tag) {
    $next = $missing[$key+1];
    if (!isset($first)) {
        $first = $tag;
    }
    if($next != $tag + 1) {
        if($first == $tag) {
            echo '<tr><td>'.$tag.'</td></tr>';
        } else {
            echo '<tr><td>'.$first.'-'.$tag.'</td></tr>';
        }
        unset($first);
    }
}

我认为bash中可能有一个单行程可以做到这一点,但我的Google搜索功能正在缩短......

更新 谢谢@Karoly Horvath快速回答我用来完成我的项目。我肯定会对任何更简单的解决方案感兴趣。

2 个答案:

答案 0 :(得分:5)

是的,shell执行变量替换,如果未设置prev,则该行变为:

if [ -ne $n+1] 

这是一个工作版本:

numbers="18,19,62,161,162,163,165"

echo $numbers, | sed "s/,/\n/g" | while read num; do
    if [[ -z $first ]]; then
        first=$num; last=$num; continue;
    fi
    if [[ num -ne $((last + 1)) ]]; then
        if [[ first -eq last ]]; then echo $first; else echo $first-$last; fi
        first=$num; last=$num
    else
        : $((last++))
    fi
done | paste -sd ","

18-19,62,161-163,165

答案 1 :(得分:0)

具有功能:

#!/bin/bash

list2range() {
  set -- ${@//,/ }       # convert string to parameters

  local first a b string IFS
  local -a array
  local endofrange=0

  while [[ $# -ge 1 ]]; do  
    a=$1; shift; b=$1

    if [[ $a+1 -eq $b ]]; then
      if [[ $endofrange -eq 0 ]]; then
        first=$a
        endofrange=1
      fi
    else
      if [[ $endofrange -eq 1 ]]; then
        array+=($first-$a)
      else
        array+=($a)
      fi
      endofrange=0
    fi
  done

  IFS=","; echo "${array[*]}"
}

list2range 18,19,62,161,162,163,165

输出:

18-19,62,161-163,165
相关问题