我有一个带有双破折号的文件名列表:
New York--1984 and counting.txt
Timeless Wonders--Silver-Guardians.txt
Project Blue Gale--Hills of Green.txt
我需要删除双破折号和双破折号之前的所有字符:
1984 and counting.txt
Silver-Guardians.txt
Hills of Green.txt
这是使用SED的模式代码:
for f in *; do mv "$f" "$(sed 's/[^\--]*--//')"; done
RE错误:无效的字符范围
如果我更改它,那么它只会搜索一个破折号,它可以工作。那么如何搜索双破折号?提前致谢。
该错误是由于您的[^\--]
模式在\
(dec.92)和-
(dec.45 )之间定义了一个范围而造成的,这是不正确的。
我建议使用
for f in *; do mv "$f" "$(sed 's/^[^-]*--//' <<< "$f")"; done
该sed 's/^[^-]*--//'
命令将删除所有0个或更多字符,而不是-
从字符串开头到--
子字符串。
或者,您可以使用参数扩展${f#*--}
(如@tripleee所示)。它将从字符串的开头删除尽可能少的0个或更多字符(#*
)直到第一个--
。
观看在线演示:
s="New York--1984 and counting.txt"
echo "${s#*--}";
sed 's/^[^-]*--//' <<< "$s"
输出:
1984 and counting.txt
1984 and counting.txt
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句