【问题标题】:Regex doesn't work using loop to go through filenames正则表达式无法使用循环遍历文件名
【发布时间】:2014-11-25 11:36:42
【问题描述】:

我已经为此工作了一段时间,但我似乎无法破解它。我在一个文件夹中有一些文件

testing.test.S09E01.720p.HDTV.x264-TWIST 
dmscript.sh

在我使用下载管理器设置它以在下载剧集后运行之前,我已经在其中测试了脚本。

脚本有以下代码。

#!/bin/sh
#THIS SCRIPT WILL DETERMINE TV SHOWS AND EPISODES AND MOVE THEM TO THE CORRECT
#FOLDER WHICH WILL ALLOW EITHER SICKBEARD OR COUCHPOTATO TO RENAME AND MOVE THEM

    regex="([Ss]?([0-9]{1,2})[-|.]?[x|Ee|]?(\d{2})|(0?\d{1})(\d{2}))"

    #where the tv shows will be copied to
    tvdir="/volume1/public/NZB/Complete/tv/processed/"

    #this was a debug variable to allow me to see where the code fails
    num="1"

    #will change the * to full path of folder later, for now this works because script is running from
    #inside folder
    for filename in *
    do
            if [[ "$filename =~ $regex ]]
            then
            #display the current filename and the variable number
            echo "$filename $num"
            #commented on the following code so when the script works I will just uncomment
            #mv $filename $tvdir
            #change variable to 2, this is to see whether the if test will fail and skip the file
            #that doesn't conform to the regex
            num="2"
            else
            echo "nothing of use"

    done

但是,一旦我运行代码,我就会得到这个

testing.test.S09E01.720p.HDTV.x264-TWIST 1
dmscript.sh 2

显然出了点问题,因为我只想让它显示上面列表中的第一个而忽略另一个。

我从 http://regex101.com/r/qZ2eO9/1 得到了正则表达式规则,我忽略了最后的 /gim,因为我不确定这是否可以在 shell 中工作,只是坚持使用 Ss 和 Ee,所以它不区分大小写

【问题讨论】:

  • 为什么不简化并在其他一切正常时返回复杂的 reg-expr。我会做case $f in *[Ss][0-9][Ee][0-9]* ) do stuff with $f ;; * ) echo $f didn_t match ;; esac(或类似的)。您可以“或”(使用|)对多个测试进行字符化,例如case $f in *[Ss][0-9][Ee][0-9]*|*[Ss][0-9][0-9][Ee][0-9][0-9]* ...。祝你好运。
  • 我不认为\d 在这样的shell 中是有效的。改用[[:digit:]](或类似[0-9] 的类,你已经用过一次了)。此外,您在 OP 中的 sn-p 中有错字。您错过了"$filename 上的报价。
  • 这些都不起作用,当我实现 case esac 时,什么也没有出现。 '我尝试了各种正则表达式组合,似乎没有任何效果
  • 您的 /bin/sh 可能不理解正则表达式。更改为 /bin/bash

标签: regex shell


【解决方案1】:

一些修改:

(1) 将表达式中的\d 修正为[0-9]。

(2)修复if [[ "$filename =~ $regex ]]行中的引号"

(3) 在 if 块末尾添加fi

现在脚本应该可以工作了。

regex="([Ss]?([0-9]{1,2})[-|.]?[x|Ee|]?([0-9]{2})|(0?[0-9]{1})([0-9]{2}))"

#where the tv shows will be copied to
tvdir="/volume1/public/NZB/Complete/tv/processed/"

#this was a debug variable to allow me to see where the code fails
num="1"

#will change the * to full path of folder later, for now this works because script is running from
#inside folder
for filename in *
do  
        if [[ $filename =~ $regex ]]
        then
            #display the current filename and the variable number
            echo "$filename $num"
            #commented on the following code so when the script works I will just uncomment
            #mv $filename $tvdir
            #change variable to 2, this is to see whether the if test will fail and skip the file
            #that doesn't conform to the regex
            num="2"
        else
            echo "nothing of use"
        fi  
done

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-26
    • 1970-01-01
    • 2021-09-09
    • 1970-01-01
    • 2012-06-01
    • 1970-01-01
    相关资源
    最近更新 更多