尝试在bash中创建自定义脚本:
GENDER=M
T='<p>He§She is a very handsome§beautiful man§woman</p>'
[[ $GENDER = M ]] && sed 's:§\S*::g' <<< "$T" || sed 's:\S*§::g' <<< "$T"
Output: <p>He is a very handsome man
Desired: <p>He is a very handsome man</p>同样:
GENDER=F
T='<p>He§She is a very handsome§beautiful man§woman</p>'
[[ $GENDER = M ]] && sed 's:§\S*::g' <<< "$T" || sed 's:\S*§::g' <<< "$T"
Output: She is a very beautiful woman</p>
Desired: <p>She is a very beautiful woman</p>有什么想法吗?谢谢。
发布于 2020-08-23 22:01:53
这里的问题是,当您不希望\S匹配<和>字符时,它却将其匹配。
从使用\S切换到POSIX字符类不仅使您的代码与更多版本的sed兼容,还允许您控制向这些类添加额外的字符,在本例中为<和>:
selectWords() {
case $1 in
M) sed 's:§[^[:space:]<>]*::g' <<<"$2" ;;
*) sed 's:[^[:space:]<>]*§::g' <<<"$2" ;;
esac
}
selectWords M '<p>He§She is a very handsome§beautiful man§woman</p>'
selectWords F '<p>He§She is a very handsome§beautiful man§woman</p>'<p>He is a very handsome man</p>
<p>She is a very beautiful woman</p>https://stackoverflow.com/questions/63547705
复制相似问题