简体   繁体   中英

Perl one-liner repeat substitution

I want to repeat this search until there are no matches available in the text:

perl -pi -e 's/(<langSet xml.lang=....>)\s*<tig>(.*?)<\/tig>\s*<tig>/\1<tig>\2<\/tig>\1<tig>/g' iate.dsl

Basically, I need a while-loop in a one-liner form, which will take lines looking a bit like this

<langSet...><tig>...</tig><tig>...</tig><tig>...</tig><tig>...</tig>

and return something like this

<langSet...><tig>...</tig><langSet...><tig>...</tig><langSet...><tig>...</tig><langSet...><tig>...</tig>

where the number of <tig> instances varies for each line.

for the first this is not a "search", this is a "substitution" and the second think is, that you don't need a while loop, cause the 'perl one liner' read out every single row.

But you can use an "if-statement" like -> 's/foo/bar/i if m/foo/i' or similar thinks, like ternare if-then-else but, it is not necessary. I would make a check first with the flag "-p" without "-i" (not "-pi"), to check the output first.

example:

 perl -p -e 's/(<langSet xml.lang=....>)\s*<tig>(.*?)<\/tig>\s*<tig>/\1<tig>
\2<\/tig>\1<tig>/g if m/(<langSet xml.lang=....>)\s*<tig>(.*?)<\/tig>
\s*<tig>/gi' iate.dsl

Good luck.

这工作:

perl -p -i -e 's/(<langSet xml.lang=....>)\s*<tig>(.*?)<\/tig>\s*<tig>/\1<tig>\2<\/tig>\1<tig>/g while m/(<langSet xml.lang=....>)\s*<tig>(.*?)<\/tig>\s*<tig>/gi' iate.dsl

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM