1

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

1
  • I have edit the question you can read it their. Commented Aug 11, 2016 at 8:03

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I tried this but getting this error ./script.sh: 5: ./script.sh: Bad substitution
It worked just changed #!/bin/sh to #!/bin/bash Thanks you so much.
Great solution #!/bin/bash. Will add to answer.
But in log level I don not want whole word in lower case. First word should be capital e.g Info, Debug
Oh man, that is new information. If you change ${err,,} to ${err^} you will get what you want (providing you call script with lower case error levels).
|
1

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.

3 Comments

I want is that user can change whatever kind of logs he wants either error,debug,info or warn but just banging my head could not find anyway.
@MaheenNasir: Ok, then just do like I wrote
Thanks. @Igor Chubin

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.