How can I get the values in between single or double quotes?
Assuming a line like this:
foo bar ENV['PGHOST'] another bar
I would like to get the value PGHOST which is inside the square brackets next to the ENV. I assume that the line has only one such occurrence. So, this: foo bar ENV['PGHOST'] another bar ENV['FOO'] and foo is not an input.
However, the line might be coming in with " instead of ', like this:
foo bar ENV["PGHOST"] another bar
In other words, the value I want to get might be enclosed in either double or single quotes.
Not only that, the actual value might contain double or single quotes. If it is enclosed in single quotes, then it can contain a double quote. If it is enclosed in double quotes, then it can contain a single quote. So, these two lines are valid expected input:
foo bar ENV["PGH'OST"] another bar
foo bar ENV['PGH"OST'] another bar
If I get lines like the above, the result should be PGH'OST and PGH"OST respectively.
I know how I can get the value if it does not contain quotes in quotes. Here it is how I do it:
SED_COMMAND="s/^.*ENV\[['\"]\([^'\"]*\)['\"]\].*$/\1/"
echo $input | sed ${SED_COMMAND}
So, the above works fine for input being foo bar ENV['PGHOST'] another bar or foo bar ENV["PGHOST"] another bar for example.
But it does not work for foo bar ENV['PG"HOST'] another bar, neither for foo bar ENV["PG'HOST"] another bar.
Any help would be much appreciated. Note that I don't have to solve the problem with sed. Any other suggestion is acceptable.
Update. Note that I might have inputs like this:
foo bar ENV['PGHOST'] another bar "what a world" I 'live' in
foo bar ENV["PGHOST"] another bar "what a world" I 'live' in
i.e. double and/or single quotes might exist in parts of the input that are outside of the ENV key I want to extract.
Update Other example of valid input:
foo bar ENV['PGHOST'] another bar in NEW['YORK'] to visit
it can be a valid input. I need to get PGHOST.
And this is valid too:
foo bar ENV["PGH'OST"] another bar ["baz"]
which should return PGH'OST.
foo bar ENV['PGHOST'] another bar
I would like to get the value PGHOST which is inside the square brackets next to the ENV. I assume that the line has only one such occurrence. So, this: foo bar ENV['PGHOST'] another bar ENV['FOO'] and foo is not an input.
However, the line might be coming in with " instead of ', like this:
foo bar ENV["PGHOST"] another bar
In other words, the value I want to get might be enclosed in either double or single quotes.
Not only that, the actual value might contain double or single quotes. If it is enclosed in single quotes, then it can contain a double quote. If it is enclosed in double quotes, then it can contain a single quote. So, these two lines are valid expected input:
foo bar ENV["PGH'OST"] another bar
foo bar ENV['PGH"OST'] another bar
If I get lines like the above, the result should be PGH'OST and PGH"OST respectively.
I know how I can get the value if it does not contain quotes in quotes. Here it is how I do it:
SED_COMMAND="s/^.*ENV\[['\"]\([^'\"]*\)['\"]\].*$/\1/"
echo $input | sed ${SED_COMMAND}
So, the above works fine for input being foo bar ENV['PGHOST'] another bar or foo bar ENV["PGHOST"] another bar for example.
But it does not work for foo bar ENV['PG"HOST'] another bar, neither for foo bar ENV["PG'HOST"] another bar.
Any help would be much appreciated. Note that I don't have to solve the problem with sed. Any other suggestion is acceptable.
Update. Note that I might have inputs like this:
foo bar ENV['PGHOST'] another bar "what a world" I 'live' in
foo bar ENV["PGHOST"] another bar "what a world" I 'live' in
i.e. double and/or single quotes might exist in parts of the input that are outside of the ENV key I want to extract.
Update Other example of valid input:
foo bar ENV['PGHOST'] another bar in NEW['YORK'] to visit
it can be a valid input. I need to get PGHOST.
And this is valid too:
foo bar ENV["PGH'OST"] another bar ["baz"]
which should return PGH'OST.
Комментарии
Отправить комментарий