虽然很明显,一种使用方法sed
不是完成这项工作的最佳工具。我已经评论了代码以查看会发生什么,因为它似乎有点困惑:
sed -n '
## Insert a newline just before each "__". This is the most
## important instruction of all the script. The game is that
## newline character is the only want that sed cannot find in
## a line of data, so use it to know where there will be "__"
## to change. For each part changed the script will save it
## in hold space, but due to constraints of those (only two
## spaces) I will have to play deleting and recovering data
## several times between both.
s/__/\n&/g
## Save in hold space all data until first newline.
## So it means, just before the first "__" of the line.
h ; s/\n.*$// ; x
## Remove that part just saved in hold space.
s/^[^\n]*\n//
## Set a label to jump it later.
:a
## This is end condition. When not found any newline
## in the pattern space means that there are no more "__" to
## process, so get all data saved in hold space, print
## it and leave hold space empty ready for next line of
## the input file.
/^[^\n]\+$/ {
g
p
x
s/^.*$//
x
b
}
## This part of code will process next two input lines.
## First one has the first pair of "__" and second one has
## the end pair, so substitute to each respective curly
## braces.
s/__/{{/
## Once the substitution has been done, save it adding to
## hold space.
## I add all the line but only want to keep until first newline.
## I delete two of them because "H" command adds it one by itself.
H ; x ; s/\n// ; s/\n.*$// ; x
## Delete part just processed and saved in hold space.
s/^[^\n]*\n//
## Repeat same process for end pair of "__"
s/__/}}/
H ; x ; s/\n// ; s/\n.*$// ; x
s/^[^\n]*\n//
## Goto label "a"
ba
' infile
粘贴并从命令行运行它,只要它产生两行:
whatever{{MATCH_THIS}}whateverwhatever{{AND_THIS}}whateverwhatever
exten => s,n,ExecIf($[${amacode} == 1]?Set(rateparams_view={{INCOMING_RATEPARAMS_VIEW}}):Set(rateparams_view={{OUTGOING_RATEPARAMS_VIEW}}))