说我有这个:
str="@test/string"
echo $str
@test/string
echo ${str#@}
test/string按预期工作,但是
echo ${str//\//-}
ksh: ${str//\//-}: bad substitution失败了。(预期@test-string)
替换这样的字符的正确方法是什么?
echo $KSH_VERSION
KSH version @(#)PD KSH v5.2.14 99/07/13.2发布于 2023-05-19 17:24:10
pdksh shell是OpenBSD上新用户的默认shell,它没有您提到的非标准参数替换。
要用一些新字符串替换某些模式的所有不重叠匹配,可以使用sed:
str=$( printf '%s\n' "$str" | sed 's/PATTERN/REPLACEMENT/g' )请注意,这使用PATTERN作为POSIX基本正则表达式,REPLACEMENT必须转义&、\1和其他sed将以特殊方式解释的字符串,并且命令替换从字符串的末尾删除所有尾随的换行符。
示例:
$ str=@title/section1/section2
$ str=$( printf '%s\n' "$str" | sed 's/\//-/g' )
$ printf '%s\n' "$str"
@title-section1-section2在这种情况下,当只替换单个字符时,可以用sed替换y/\//-/,或者用tr命令tr / -替换整个sed命令。
您还可以选择编写shell循环:
while true; do
case $str in
*PATTERN*)
str=${str%%PATTERN*}REPLACEMENT${str#*PATTERN}
;;
*)
break
esac
done在这段代码中,PATTERN是一个shell模式(“文件名全局模式”)。代码重复使用替换字符串替换字符串中模式的第一个匹配,直到没有更多匹配。由于循环的存在,不能保证以后的迭代不会在先前插入的替换字符串中执行替换。
示例:
$ str=@title/section1/section2
$ while true; do case $str in (*/*) str=${str%%/*}-${str#*/} ;; (*) break; esac; done
$ printf '%s\n' "$str"
@title-section1-section2发布于 2023-05-19 12:26:32
肯定是一种更好的方法,但是:
$(echo "$str" | tr '/' '-')https://unix.stackexchange.com/questions/746349
复制相似问题