开发者

sed regex, work on substrings and not on whole line

开发者 https://www.devze.com 2023-01-19 13:20 出处:网络
suppose to have the following string: commandone{alpha} commandtwo{beta} {gamma}commandthree commandtwo{开发者_Go百科delta}

suppose to have the following string:

commandone{alpha} commandtwo{beta} {gamma}commandthree commandtwo{开发者_Go百科delta}

and I want to abtain:

commandone{alpha} beta {gamma}commandthree delta

well I'm triying with regex and sed, I can easily find if is it present the pattern I look for /commandtwo{.*}/ but not erasing commandtwo and the following two brakets. A way would be to work only the substrings "commandtwo{beta}" and "commandtwo{delta}" but I don't know even if is it possible or noto using sed.

I'm looking for a regex to perform the action I've described. thank in advance


This seems to work:

sed 's/commandtwo{\([^}]*\)}/\1/g' inputfile

Example:

$ echo 'commandone{alpha} commandtwo{beta} {gamma}commandthree commandtwo{delta}'|sed 's/commandtwo{\([^}]*\)}/\1/g'
commandone{alpha} beta {gamma}commandthree delta


use awk

$ s="commandone{alpha} commandtwo{beta} {gamma}commandthree commandtwo{delta}"
$ echo "$s" | awk '{for(i=1;i<=NF;i++) if ($i~/commandtwo/){ gsub(/commandtwo|{|}/," ",$i)} }1'
commandone{alpha}   beta  {gamma}commandthree   delta


assuming you want to strip all references to commandtwo from your code (but not it's arguments) probably somthing like /commandtwo{(.*?)}/\1

0

精彩评论

暂无评论...
验证码 换一张
取 消