【问题标题】:bash: how to extract text from beginning of a string to the first number? [duplicate]bash:如何从字符串的开头提取文本到第一个数字? [复制]
【发布时间】:2013-12-18 13:26:33
【问题描述】:

我有一堆这样命名的文件:

text 01 (blabla) other text
text 02 (whatever) other text
.
.
text 025 (etc) other tex

some text 1 (20031020) other text
some text 2 (20031022) other text
.
.
some text 10 (20031025) other text

some new text 01 other text
.
.
.
some new text 200 other text

我想从文件名中只提取第一个数字之前的单词,所以从上面的例子中我想获得:

text
some text
some new text

我想这样做是为了根据文件名移动所属文件夹中的每个文件(或者如果文件夹不存在,则创建该文件夹)。

我想用 bash 做到这一点,我知道它可以使用正则表达式来完成,但我不知道如何,我只看到了字段由已知字符分隔的示例,而在这种情况下限制是后跟任意数字的空格。

【问题讨论】:

  • 您只想要您的示例所示的唯一字符串吗?

标签: regex bash filenames


【解决方案1】:

使用${variable%%pattern}(此删除后缀模式)。

$ filename='text 01 (blabla) other text'
$ echo ${filename%%[0-9]*}
text

【讨论】:

    猜你喜欢
    • 2018-12-13
    • 1970-01-01
    • 1970-01-01
    • 2013-07-30
    • 2020-02-13
    • 2014-10-30
    • 2021-04-01
    • 1970-01-01
    • 2022-01-23
    相关资源
    最近更新 更多