[root@localhost test]# perl -e 'undef $/;while (<>) {s/\<|[a-zA-Z]|\>|\//one/gs;print}' file
oneoneoneoneoneoneoneoneoneoneoneone
oneoneoneoneoneoneoneoneoneoneoneone
oneoneoneoneoneoneoneoneoneoneoneone
我只需要一次打印
[root @ localhost test] #cat file.input
<tag>X</tag>
<tag>Y</tag>
<tag>Z</tag>
[root @ localhost test] #cat file.output
one
[root @ localhost test]#
答案 0 :(得分:0)
以下是一些解释:
< # Opening tag
( \w+ ) # (1), tag name
\s* # Whitespaces: tab,space,newlines, etc..
> [^>]*? </ \1 \s* > # Some text then Closing tag
(?: # Cluster group
\s* # Whitespaces: tab,space,newlines, etc..
< # Repeat as above, with tag name
\1 # ""
\s* # ""
> [^>]*? </ \1 \s* > # ""
)* # End cluster, do 0 to many times