从文件名

时间:2016-02-18 10:53:42

标签: bash sorting

我是bash的新手,想知道你们是否可以提供帮助 - 我有一个名为的文件列表 amp1_X_1 amp1_X_2 ... amp43_X_3 并且必须提取出现在 X 两侧的数字的最大值。 我一直在寻找各种链接来帮助解决我的问题,但是当我尝试调整示例以适应我的目的时,我会遇到各种错误,例如“未找到命令”或%%运算符仍未评估。 How do I Select Highest Number From Series of <string>_# File Names in Bash Script extract numbers from file names

例如,我试过像

这样的东西
max=-1
for file in amp*_X_1
do
  pattern=_X_*
  num=${file}%%${pattern}
  num=${num}##amp
  echo "num is $num"
  [[ $num -gt $max ]] && max=$num    
done
echo "max is: $max"

(主要归功于第一个链接中的ghostdog74),无论如何只能用于一组数字,但它会以%%未评估的方式返回。我是否错过了一些愚蠢的事情/是否有更好的方法来做到这一点? 谢谢!

1 个答案:

答案 0 :(得分:0)

您的脚本中存在一些错误:

1)for file in amp*_X_1

如果您想使用特定文件夹中的文件,则必须使用lsfind等命令 即。

for file in $(ls -1 <your file>)
for file in ($find . -name "<your file>")

等。

2)num=${file}%%${pattern}

使用num=${file%%$pattern}

在下面

,您可以找到您编辑的脚本:

max=-1
for file in $(find . -name "amp*")
do
  echo "file[$file]"
  pattern=_X_*
  num=${file%%$pattern} 
  num=$(echo $num|sed "s/^\.\///g" | sed "s/amp//g")
  echo "num is $num"
  [[ $num -gt $max ]] && max=$num    
done
echo "max is: $max" 

输出:

sh-4.3$ bash -f main.sh                                                                                                                                                                                                                                
file[./amp1_X_1]                                                                                                                                                                                                                                       
num is 1                                                                                                                                                                                                                                               
file[./amp3_X_2]                                                                                                                                                                                                                                       
num is 3                                                                                                                                                                                                                                               
file[./amp3_X_1]                                                                                                                                                                                                                                       
num is 3                                                                                                                                                                                                                                               
file[./amp43_X_1]                                                                                                                                                                                                                                      
num is 43                                                                                                                                                                                                                                              
max is: 43 

现在这个脚本正在寻找X左侧数字之间的最大值。 从你的问题不清楚你的预期结果是什么.. 1)在X侧的所有数字之间找到最大值? 2)在左侧X侧的所有数字之间找到最大值? 3)在右侧X的所有数字之间找到最大值?

如果是1,则必须添加另一个变量,获取值并进行检查。

类似

   max=-1
for file in $(find . -name "amp*")
do
  echo "file[$file]"
  pattern=_X_*

  num=${file%%$pattern} 
  num=$(echo $num|sed "s/^\.\///g" | sed "s/amp//g")
  echo "num is $num"

  pattern2=*_X_
  num2=${file##$pattern2} 
  echo "num2 is $num2"  


  [[ $num -gt $max ]] && max1=$num    
  [[ $num2 -gt $max ]] && max2=$num2
  if [[ $max1 -gt $max2 ]]; then
        echo "max is: $max1" 
  else
        echo "max is: $max2"
  fi

done
#echo "max is: $max"