开发者

AWK - replace a word if it not begin with special symbol

I try to translate this line:

(ModuleEins = WertA | ${ModuleEins} = Wer开发者_如何学编程tB | ModuleEins = WertB)

to this line:

(${ModuleEins}= WertA | ${ModuleEins}= WertB | ${ModuleEins}= WertB)

but i don't get it to work.

i have a complex awk script where i run a replacement statement inside a loop.

e.g. awk '{ sub( "ModuleEins", "${ModuleEins}", $0 ); print, $0 }'

i have no idea how to replace in awk a word which not begin with special characters.

(?!{)ModuleEins(?!}) <- This idea i don't get to work inside awk.


This is a brittle solution but exactly answers your question.

Note that I

  1. changed sub(...) to gsub
  2. removed the ',' after print
  3. changed the search target to a regular expression /[^{]ModuleEins[^}]/
  4. and added a '&' to capture the first char of the reg exp, which with the [^{] is where the brittleness comes in.

code

print -- '(ModuleEins = WertA | ${ModuleEins} = WertB | ModuleEins = WertB)' \
| awk '{ gsub( /[^{]ModuleEins[^}]/, "&${ModuleEins}", $0 ); print $0 }'

output

(ModuleEins ${ModuleEins}= WertA | ${ModuleEins} = WertB | ModuleEins ${ModuleEins}= WertB)

I hope this helps.

P.S. as you appear to be a new user, if you get an answer that helps you please remember to mark it as accepted, and/or give it a + (or -) as a useful answer.


Thanks for help!

@shellter The sub pattern [^{]ModuleEins[^}] would not work for me, because [^{] is a symbol except {. If i have "(ModuleEins=value)" than the result would be "${ModuleEins}value)" and not "(${ModuleEins}=value)". This is for me wrong.

i tried the idea from glenn jackman inside my awk script and get it to work:

gsub( "\\$", "\\$", $0 )

"echo \""$0"\" | perl -pe 's/(?<!{)"part[i]"/\\${"part[i]"}/g'" |& getline $0

gsub( "\\\\\\$", "$", $0 )

ps: sorry i can't vote yet -.-


Perl regular expressions are better than awk's here:

 perl -pe 's/(?<!\${)ModuleEins/\${$&}/g'


sed -e 's/(ModuleEins/(${ModuleEins}/g' -e 's/| ModuleEins/| ${ModuleEins}/g'


The following solution is brute-force, but easy to understand and pretty robust... First gsub to change "${ModuleEins}" to "ModuleEins", then change all the "ModuleEins". Using "\" to escape certain characters is required because the first parameter to gsub is an Extended Regular Expression. In this mini-language, the characters "$", "{" and "}" are meta-characters by default and are interpreted by gsub with special meaning.

$ x='(ModuleEins = WertA | ${ModuleEins} = WertB | ModuleEins = WertB)'
$ echo $x | awk '{ gsub(/\$\{ModuleEins\}/, "ModuleEins"); gsub(/ModuleEins/, "${ModuleEins}") } 1'
(${ModuleEins} = WertA | ${ModuleEins} = WertB | ${ModuleEins} = WertB)

A SED implementation might be the better way to go for its conciseness:

$ echo $x | sed 's/\${ModuleEins}/ModuleEins/g; s/ModuleEins/${ModuleEins}/g'
(${ModuleEins} = WertA | ${ModuleEins} = WertB | ${ModuleEins} = WertB)

Note that the escaping rules are different in the above as AWK uses Extended Regular Expressions for the search pattern and SED uses Basic/Traditional Regular Expressions. The difference between the two regular expressions language have to do with escaping of meta-characters, and such is described in the egrep(3) manual (search for "Basic vs Extended Regular Expressions").

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜