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.

Hitesh
  • 3,449
  • 8
  • 39
  • 57
Madura Dissanayake
  • 8,309
  • 5
  • 25
  • 34

3 Answers3

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
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Hello, may I know about this "$2" ? what does it do..? THankx – Madura Dissanayake Apr 11 '14 at 10:20
  • 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` – Mark Setchell Apr 11 '14 at 10:39
  • 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. – Madura Dissanayake Apr 16 '14 at 05:32
  • I have updated my answer - please have another look. – Mark Setchell Apr 16 '14 at 06:50
  • Thank You so...much for your help & explanation. it was worked.... :) – Madura Dissanayake Apr 16 '14 at 07:48
  • Hello Mark... I have another problem like this. In a text(which is called sampleText.txt) file has the webURL = "http://localhost.com" like this I want to change this value using a bash script. But I tried with this my script, bt it did not worked, here is my script, _________ #!/bin/bash URL=$(awk -F\" '/^webURL/{print $2}' sampleText.txt) echo $URL if [ "$URL" != "www.google.com" ]; then shopt -s globstar for file in sampleText.txt do sed -i.bak 's/$URL/www.google.com/g' $file done fi _______________________ I hope you will help me. Any help would be greatly appreciated, Thankx... :) – Madura Dissanayake Apr 16 '14 at 09:18
  • It's really hard to read questions when formatted as comments :-) Just submit a new question to StackOverflow like you did the last one, and I will look out for it - and so will all the other guys - that way you will get a proper answer. – Mark Setchell Apr 16 '14 at 09:21
  • sure Mark...i'll post it as a new question Thank you... :) – Madura Dissanayake Apr 16 '14 at 09:55
  • Hello Mark, here is the link for new question.. http://stackoverflow.com/questions/23106168/how-to-replace-a-word-through-a-variable-after-searching Thank you..... :) – Madura Dissanayake Apr 16 '14 at 10:05
0
url=`grep webURL sample.conf | sed -e 's/webURL: //'`
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • 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)`` – Aleks-Daniel Jakimenko-A. Jul 02 '14 at 05:48
0

using sed

sed 's#\(webURL: \).*#\1 "http://www.sampledomain:8080"#' file
BMW
  • 42,880
  • 12
  • 99
  • 116