linux - Bash: How can I replace what follows the equals sign consistently while skipping specific instances of key=value? -
within configuration file wish consistently replace whatever follows equals sign of specific "key=xxxx" skip instances of "key=specificvalue". value follows equal sign can anything, except "specificvalue".
how can within bash shell script?
the file name caconfig.txt. line need modify starts commonname followed whitespace , equal sign. want replace contents right passed argument $1.
commonname = supplied commonname = supplied commonname = walmart-sam.local.lan i need skip first 2 lines, , modify whatever happens in third line. i'd rather not count on order of lines.
i use awk:
awk -v a="$1" '/^commonname/ && $3 != "supplied" { $3=a } { print }' caconfig.txt for line starting "commonname", swaps third column $1 if original value isn't "supplied".
instead of { print } print every line, can put 1:
awk -v a="$1" '/^commonname/ && $3 != "supplied" { $3 = } 1' caconfig.txt if want modify file, can either use indirection:
awk 'commands' > tmp && mv tmp caconfig.txt or if have version of gawk >= 4.1.0, can use inplace module:
awk -i inplace 'commands' caconfig.txt
Comments
Post a Comment