i have a file where the hostnames and variables are in same line in below format, am able extract some part variables while otherlike subscriptions and handler is missing.
can you please correct me if grep is able to perform this ?
Noting that \w is not a "standard" feature in an extended regular expression and noting that on systems where \w is requesting a match to a word, there is no word between the = and the ' in subscriptions=' and that there is no word between the = and the " in handler=" ; what additional output were you hoping to get?
It can probably be done with grep if you clearly define EXACTLY what it is that you are trying to match with your ERE. It looks to me like the output you got was exactly what you requested. (And you still haven't said what output you are hoping to get from your sample input.)
We now have 5 examples of what you want, but we do not have any specification for what constitutes a valid string on the left side of the = nor a valid string on the right side of the = .
I repeat:
5 examples do not make a clear definition of exactly what you are trying to match and I don't want to waste my time writing code that will work with your current examples to then be told that I didn't correctly guess at your additional unstated real requirements.
only thing am trying to achieve is extracting variables, as all variables which are assigned with "=" operator. so am trying to get complete word around "=".
Thank you for you time and inputs. i will try to escape the characters like symbols like ',[ for now or possibly with awk and optimize it later.
Adding to the comments of Don: If you do want to use regexp patterns like \w, you could use the -P option of grep, which interprets the pattern as PCRE-regexp instead of extended regexp. Make sure that you read the grep man page before you use it, to understand its limitations.
Hi rakeshkumar...
OSX 10.13.0, default bash terminal.
If I am reading this thread correctly...
...to add to the other guys statements can you guarantee things like these will never occur:
Why is is strange? I don't see anything unusual here. With
var==
or the equivalent
var='='
you are assigning a string consisting of a single equal sign. With, say,
var=======
or, if you are in a funny mood
var=='='="="==
you are assigning a string of six equal signs. Nothing special here in bash or sh. Just in the case you want to do it in zsh, note that an unquoted equal sign has a special meaning, so in zsh you would have to write
As the OP hasn't given any reference to whether any variables are going to have other '=' in its string leading or otherwise then this could much more difficult than Don's solution.
I am not proficient enough on grep to solve it, but I see difficulties when they arrive.
For example: this for variable 'c' c=x=cat gives the result c=x . Although bash can accept it, doesn't mean a utility can without some serious thought. Who is to say that the OP's post doesn't have any number of '=' inside any one variable string.
As I don't know enough about the absolute subtleties of grep I will have to leave it to pros like Don et al to give their excellent advice...
will display x=cat, because this is what c is set to. Of course, given the way you are grepping the file (using the -o option), this will show only c=x. This is the consequence of the grep pattern you are using.
Don was VERY accurate when quoting what the OP's variables are to be like.
Here is another example and there are so, so many more after experimentation that UNLESS the OP can guarantee that his variables will only be alpha-numeric without any other '=' inside any of his basic variables then this could well be a daunting task. Who is to say that this cannot occur:
Last login: Sat Dec 23 16:12:07 on ttys000
AMIGA:amiga~> echo 'localhost subscriptions='["production", "masters", "fileserver", "cldb", "nfs"]' handler="genie"
> host1.localhost.com a=apple
> host2.localhost.com b=ball c=[ x+y=z ]' > /tmp/test
AMIGA:amiga~>
AMIGA:amiga~> cat /tmp/test
localhost subscriptions=[production, masters, fileserver, cldb, nfs] handler="genie"
host1.localhost.com a=apple
host2.localhost.com b=ball c=[ x+y=z ]
AMIGA:amiga~>
AMIGA:amiga~> grep -Eo '[[:alpha:]]+=([[:alpha:]]+|"[^"]*"'"|'[^']+')" /tmp/test
handler="genie"
a=apple
b=ball
y=z
AMIGA:amiga~> # Now WHERE is 'c'?
AMIGA:amiga~> _
Note: c=[ x+y=z ] ...
Where is 'c'? 'y' has suddenly appeared AS a variable. Who is to say that what I have done will not occur?
Don has given what the OP appeared to want exactly, but, and a big but, I have pointed out several pitfalls, not in Don's code but as a backup to it.
Don needs a much better description of what the OP considers a 'variable' AND I totally agree with him!