如何在bash中捕获和修改正则表达式识别的数字。

huangapple go评论71阅读模式
英文:

How to capture and modify digits recognized by regex in bash

问题

基本上,我试图将下面每个文件的_XtoX中的每个数字增加20

新的文件名应该如下所示:

我想出了以下代码片段:

我基本上识别出了"_"*to*/中的模式,但我相当确定*不是捕获这些值并递增的合理方式。这方面有什么替代解决方案吗?

英文:

Basically, I'm trying to increment each digit in _XtoX of each file below by 20

err_5JUP_N2_UUCCGU_1to2  err_5JUP_N2_UUCCGU_3to4  err_5JUP_N2_UUCCGU_5to6  err_5JUP_N2_UUCCGU_7to8  err_5JUP_N2_UUCCGU_9to10

such that the new file names should look like such:

err_5JUP_N2_UUCCGU_21to22  err_5JUP_N2_UUCCGU_23to24  err_5JUP_N2_UUCCGU_25to26  err_5JUP_N2_UUCCGU_27to28  err_5JUP_N2_UUCCGU_29to30

I came up with the following snippet of code:

for FILE in err*
do
    mv $FILE ${FILE/$"_"*to*/"_$((*+20))to$((*+20))}
done 

I essentially identified the pattern with "_"*to*/, but I'm pretty sure the * is not a reasonable way to capture the values and increment them. What is an alternative solution to this?

答案1

得分: 2

尝试使用这个Shellcheck清理的代码:

#! /bin/bash -p

shopt -s extglob nullglob

for errfile in err*_+([0-9])to+([0-9]); do
    n1ton2=${errfile##*_}
    n1=${n1ton2%to*}
    n2=${n1ton2#*to}
    new_errfile=${errfile%_*}_$((n1+20))to$((n2+20))
    echo mv -v -- "$errfile" "$new_errfile"
done
  • 如果您确定代码将按预期工作,请删除echo
  • shopt -s ... 启用了代码所需的一些Bash设置:
    • extglob 启用了"扩展glob"(包括类似于+([0-9])的模式)。请查看glob - Greg's Wiki中的extglob部分。
    • nullglob 使得当没有匹配项时,glob会扩展为空(否则它们会扩展为glob模式本身,这在程序中几乎从不有用)。
  • 请查看BashFAQ/100 (我如何在bash中进行字符串操作?)以了解${var##pat}, ${var%pat}${var#pat}的解释。
  • 请注意,最好避免使用全大写变量名称(例如FILE),因为存在与Shell编程中使用的大量特殊全大写变量可能发生冲突的风险。请参阅Correct Bash and shell script variable capitalization。这就是我使用errfile而不是FILE的原因。
英文:

Try this Shellcheck-clean code:

#! /bin/bash -p

shopt -s extglob nullglob

for errfile in err*_+([0-9])to+([0-9]); do
    n1ton2=${errfile##*_}
    n1=${n1ton2%to*}
    n2=${n1ton2#*to}
    new_errfile=${errfile%_*}_$((n1+20))to$((n2+20))
    echo mv -v -- "$errfile" "$new_errfile"
done
  • Remove the echo if you are happy that the code will do what you want.
  • shopt -s ... enables some Bash settings that are required by the code:
    • extglob enables "extended globbing" (including patterns like +([0-9])). See the extglob section in glob - Greg's Wiki.
    • nullglob makes globs expand to nothing when nothing matches (otherwise they expand to the glob pattern itself, which is almost never useful in programs).
  • See Removing part of a string (BashFAQ/100 (How do I do string manipulation in bash?)) for explanations of ${var##pat}, ${var%pat}, and ${var#pat}.
  • Note that ALL_UPPERCASE variable names (like FILE) are best avoided because there is a danger of clashes with the large number of special ALL_UPPERCASE variables that are used in shell programming. See Correct Bash and shell script variable capitalization. That's why I used errfile instead of FILE.

huangapple
  • 本文由 发表于 2023年6月12日 23:20:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76458082.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定