AWK - replace a word if it does not start with a special character

I am trying to translate this line:

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

to this line:

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

but I do not make it work.

i has a complex awk script where I run the replace statement inside the loop.

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

I do not know how to replace a word in awk that does not start with special characters.

(?! {) ModuleEins (?!}) <- This idea, I can’t work inside awk.

+4
source share
5 answers

This is a fragile solution, but it definitely answers your question.

Note that I

  • changed sub (...) to gsub
  • deleted ',' after print
  • changed the search target to regex /[^{]ModuleEins[^}]/
  • and added '&' to capture the first char reg exp, which with [^{] is where fragility 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) 

Hope this helps.

PS as you, it seems, a new user, if you get an answer that helps you remember to mark it as accepted and / or give it + (or -) as a useful answer.

+1
source

Thanks for the help!

@shellter Sub pattern [^{]ModuleEins[^}] will not work for me because [^{] is a character other than { . If I have "(ModuleEins=value)" , then the result will be "${ModuleEins}value)" , not "(${ModuleEins}=value)" . This is wrong for me.

I tried the idea from glenn jackman inside my awk script and make it work:

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

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

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

ps: sorry, I still can not vote -.-

+1
source

Perl regular expressions are better than awk:

  perl -pe 's/(?<!\${)ModuleEins/\${$&}/g' 
0
source
 sed -e 's/(ModuleEins/(${ModuleEins}/g' -e 's/| ModuleEins/| ${ModuleEins}/g' 
0
source

The following solution is brute force, but easy to understand and fairly stable ... First, gsub will change $ ModuleEins to ModuleEins, and then change all ModuleEins. Using "\" to escape certain characters is required because the first gsub parameter is an extended regular expression. In this mini-language, the characters "$", "{" and "}" are the default metacharacters and are interpreted by gsub with a 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) 

Implementing SED may be the best way to keep it short:

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

Note that in the above example, escaping rules are different because AWK uses extended regular expressions for the search pattern, and SED uses basic / traditional regular expressions. The difference between the two languages ​​of regular expressions is related to the acceleration of metacharacters, and this is described in the egrep (3) manual (search for "Basic and advanced regular expressions").

0
source

All Articles