sed used as parsing tool problem in bash
Hi.
I have a config file looking like this my.config Code:
Section "One" Please note, if a variable is commented (#). it should be ignored get_params.sh Code:
#!/bin/bash when I call Code:
./get_param.sh my.config One VAR_A Code:
Orange Can anyone see why? Thanks a million. Indy |
Quote:
Because of that, the "grep" command does not work as you seem to expect: Quote:
BTW after: /^ *#/d, this: s/#.*// is not needed as all lines starting with '#' with or without leading spaces will already be deleted by the first part (/^ *#/d). This shouldn't effect functioning of the script though, just as a side note. I think it is easier done in one single sed command: Code:
RESULT=`sed -n -e "/Section[ \t]*\"$2\"/,/EndSection/s/[ \t]*[^#]\($3\)[ \t]*=[ \t]*\([^ \t]*\)[ \t]*/\1=\2/p" $1` |
For extracting parts of certain records, you can have embedded regions.
Code:
sed '/Section "One"/,/EndSection/ { One thing to think of it you want to use your config script to set variables is to remove the spaces around the `=' and `eval' the result. |
Thanks lads,
Hko, I just replace your code and it works. However, there is one small thing. I know the spaces between cause problems but there are so many config files that I don't want to go and change all of them. The problem that still remains is... when a variable is set like this: Code:
VAR_A = Apple Orange Banana Code:
VAR_A=AppleOrange Banana. Code:
VAR_A=Apple Orange Banana. Indy |
I was in a hurry and didn't test it. I did want to point out that you can have subregions.
It should use the -n option. I also misread the data and used "VAR A" in my example and not "VAR_A": Code:
$ sed -n '/Section "Three"/,/EndSection/{ Here is the second thing I mentioned: Code:
$ sed -n '/Section "Three"/,/EndSection/{ This is a method I used in an ogg2mp3 script. I used ogginfo and a sed filter to produce a number of "tag=value" lines which I eval'ed to set these variables. Then I used the variables in lame to set the tags of the mp3 file that had been in the ogg file I downloaded. |
All times are GMT -5. The time now is 10:49 PM. |