0

I'm new to bash scripting, my task is to read a line one of my configuration file, this is the way....I want...

let's assume "sample.conf" is the file, in file there is a line.. like this,

webURL: "http://www.sampledomain:8080" 

What I want is , I need to get the value of webURL, that means "http://www.sampledomain:8080" then if the URL is not equal to "http://www.sampledomain:8080" this I need to changed it with the correct value. Hope u will help me, any help would be greatly appreciated.

Thank You.

2
  • There are several examples of changing values for key/value pairs in configuration files. Try searching a bit. Commented Apr 11, 2014 at 6:54
  • Read about the stream editor. Commented Apr 11, 2014 at 6:55

3 Answers 3

1

Use awk like this:

URL=$(awk -F\" '/^webURL/{print $2}' sample.conf)

echo $URL
http://www.sampledomain:8080

The $() means "put the result of running the enclosed command into the variable URL". The awk then sets the separator for fields to the double quote sign. It then looks in your file for a line that starts with "webURL" and when it finds it, it outputs everything between the second pair of double quotes, i.e. field 2 on the line.

EDITED to answer further question

If you want to parse out a subscribe_key value from a java file called thing.java, that looks like this:

subscribe_key = 'sub-f-xx-xx-xx-xx-xx'; 

you can use this:

key=$(awk -F\' '/^subscribe_key/{print $2}' thing.java)

echo $key
sub-f-xx-xx-xx-xx-xx

Note that I changed the double quote after -F to single quote to tell awk that fields are now separated by single quotes.

Note that if you have values that are marked by single quotes and double quotes IN THE SAME FILE you can tell awk to use either single quotes or double quotes as field separators like this:

value=$(awk -F"\'|\"" '/^subscribe_key/{print $2}' yourFile)
echo $value
sub-f-xx-xx-xx-xx-xx

value=$(awk -F"\'|\"" '/^webURL/{print $2}' yourFile)
echo $value
http://www.sampledomain:8080
Sign up to request clarification or add additional context in comments.

9 Comments

Hello, may I know about this "$2" ? what does it do..? THankx
The $2 prints the second field on the line, where fields are separated by double quote signs ("). The first field in your line (called $1) is webURL: and the second (called $2) is http://www.sampledomain:8080
Thank you.. Mark.. :) and some of my java scripts contains like this: subscribe_key = 'sub-f-xx-xx-xx-xx-xx'; , In here there is are single quotes, so may I know, how can I get the value of this subscribe_key? I tried with $2, but id did not worked, it showed nothing, so hope u will help... Thanks, Any help would be greatly appreciated.
I have updated my answer - please have another look.
Thank You so...much for your help & explanation. it was worked.... :)
|
0
url=`grep webURL sample.conf | sed -e 's/webURL: //'`

1 Comment

This does not make any sense. The question was about how to edit the file, while this would simply print those links. Even if the question was about printing the links you still got it wrong. The rule of thumb is that you should never pipe grep, sed, awk or perl because mostly these tools can do the work of each other. In that case that would be url=$(grep -Po 'webURL:\s*"\K[^"]*' sample.conf)
0

using sed

sed 's#\(webURL: \).*#\1 "http://www.sampledomain:8080"#' file

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.