如何在AWK中用括号括起所有空格填充的单词?

Vil*_*age 2 regex bash awk

我有这样一个文件:

all <div class="first">these</div> <div class="second">words</div> <div class="second">are</div> <div class="second">marked</div> <div class="second">but</div> these words are not.
<div class="first">this</div> is <div class="second">another</div> <div class="second">example</div> with <div class="second">some</div> unmarked words.
Run Code Online (Sandbox Code Playgroud)

我需要在前后都有空格的所有单词周围放置大括号,例如,输出将是:

all <div class="first">these</div> <div class="second">words</div> <div class="second">are</div> <div class="second">marked</div> <div class="second">but</div> {these} {words} {are} not.
<div class="first">this</div> {is} <div class="second">another</div> <div class="second">example</div> {with} <div class="second">some</div> {unmarked} words.
Run Code Online (Sandbox Code Playgroud)
  • all 因为之前没有空间,所以没有给予支撑.
  • not.并words.没有被替换,因为之后没有空格.

我尝试了许多不同的东西awk,但没有什么工作正常.这是我能得到的最接近的:

awk '{ gsub(/.[[:blank:]][[:alpha:]][[:blank:]]*/, "{&}"); }1'
Run Code Online (Sandbox Code Playgroud)
  • 字只能包含这些字母:a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,和ü和上壳体当量.
  • 单词不能包含上面未列出的任何其他符号.例如,如果1,á并且<出现在两个空格中的某个位置,那么它不被视为匹配.

hwn*_*wnd 5

除非有另一种方法可以做到这一点,你需要使用前瞻和回顾后未在支持的断言awk或sed.使用Perl,您可以执行以下操作.

perl -pe 's/(?<= )([a-zA-ZüÜ]+)(?= )/{\1}/g' file
Run Code Online (Sandbox Code Playgroud)