views:

29

answers:

3

suppose to have the following string:

commandone{alpha} commandtwo{beta} {gamma}commandthree commandtwo{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

A: 

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
ghostdog74
A: 

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

Martijn
The non-greedy operator works in Perl, but not in `sed`.
Dennis Williamson
+2  A: 

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
Dennis Williamson