Reputation: 59
I have a config file xyz.conf which contains some fields and I want to do little change in between it according to the requirement of user. (i.e ERROR,INFO,DEBUG,WARN) How can I do that using shell script?
Example I have following lines
match => ["log_EventType", "ERROR"]
add_tag => [ "loglevelError" ]
if "loglevelError" in [tags] {
I want to changes these lines in
match => ["log_EventType", "DEBUG"]
add_tag => [ "loglevelDebug" ]
if "loglevelDebug" in [tags] {
What i have tried so far is: match => ["log_EventType", "INFO"]
For this line I created a script
file=xyz.conf
err=$1
sed -i 's/"ERROR.*/"'$err'/' $file
and simply I run command ./script.sh INFO
But in Output I get
match => ["log_EventType", "INFO
It does not return closing bracket after the word INFO
Upvotes: 1
Views: 96
Reputation: 1385
This script solves your problem letting users specify any error level and have it replaced throughout the conf file regardless of which error level that was in the file in the first place:
#!/bin/bash
file="xyz.conf"
err="${1,,}"
sed -i 's/\(match => \["log_EventType", "\)[^"]*/\1'${err^^}'/g; s/\("loglevel\)[^"]*/\1'${err^}'/g' $file
Examples:
$ ./script.sh error
$ ./script.sh debug
Note: ${1,,}
, ${err^^}
and ${err^}
will take care of upper/lower case but might not work for bash versions before 4.
Upvotes: 1
Reputation: 64563
Obviously, it is simply:
sed -i 's/ERROR/DEBUG/; s/loglevelerror/logleveldebug/' filename
It does not return closing bracket after the word INFO in your case,
because you change not ERROR
, but ERROR.*
. It means that you want to substistute the whole line after the word ERROR
and not only this word.
But I think that you want to handle some additional checks/conditions, that you didn't mention here. Please do it so we can improve our answers.
Upvotes: 1