Extended grep not matching some patterns

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 ?

cat /tmp/test
localhost subscriptions='["production", "masters", "fileserver", "cldb", "nfs"]' handler="genie"
host1.localhost.com a=apple
host2.localhost.com b=ball c=cat

raavula@qaphdm001:~/deployment/ansible$ grep -Eo "\w+=\w+" /tmp/test
a=apple
b=ball
c=cat

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?

What operating system are you using?

What shell are you using?

Is this a homework assignment?

1 Like

thank you @Don Cragun

am using ubuntu with bash shell.

this is not assignment, i'm trying to write a wrapper script around where my application not able to detect variables declared in inventory as above.

alternatively i can do using sed or awk but i want to keep it simple with grep so trying to do with it

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.)

i need to get matches around "=" , am trying for output like below

my expected output

subscriptions='["production", "masters", "fileserver", "cldb", "nfs"]'
handler="genie"
a=apple
b=ball 
c=cat

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.

That is not a even close to a clear specification.

We don't know what constitutes a variable name in the language you're trying to match.

We don't know if quotes can be escaped.

We don't know if quoted strings can extend beyond line boundaries.

Here is a clear specification of something that will match the examples you have provided:

With the above specification, the following seems to work with your sample input:

grep -Eo '[[:alpha:]]+=([[:alpha:]]+|"[^"]*"'"|'[^']+')" /tmp/test

producing the output:

subscriptions='["production", "masters", "fileserver", "cldb", "nfs"]'
handler="genie"
a=apple
b=ball
c=cat
1 Like

Thank you :):b:

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:

Last login: Fri Dec 22 11:01:36 on ttys000
AMIGA:amiga~> var==
AMIGA:amiga~> echo $var
=
AMIGA:amiga~> echo "$var"
=
AMIGA:amiga~> var='='
AMIGA:amiga~> echo $var
=
AMIGA:amiga~> echo "$var"
=
AMIGA:amiga~> var====
AMIGA:amiga~> echo $var
===
AMIGA:amiga~> echo "$var"
===
AMIGA:amiga~> _

Note var== and var==== ...
Bizarre as it seems strange things CAN happen...

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

var='======'

Hi rovf...
Longhand OSX 10.12.1, default bash terminal.
As Don has suggeted with his code it works with the given sample and fine it is.

However...

Last login: Fri Dec 22 20:42:33 on ttys000
AMIGA:barrywalker~> echo 'localhost subscriptions='["production", "masters", "fileserver", "cldb", "nfs"]' handler="genie"
> host1.localhost.com a=apple
> host2.localhost.com b=ball c=cat' > /tmp/test
AMIGA:barrywalker~> 
AMIGA:barrywalker~> cat /tmp/test
localhost subscriptions=[production, masters, fileserver, cldb, nfs] handler="genie"
host1.localhost.com a=apple
host2.localhost.com b=ball c=cat
AMIGA:barrywalker~> 
AMIGA:barrywalker~> grep -Eo '[[:alpha:]]+=([[:alpha:]]+|"[^"]*"'"|'[^']+')" /tmp/test
handler="genie"
a=apple
b=ball
c=cat
AMIGA:barrywalker~> 
AMIGA:barrywalker~> echo 'localhost subscriptions='["production", "masters", "fileserver", "cldb", "nfs"]' handler="genie"
> host1.localhost.com a=apple
> host2.localhost.com b=ball c==' > /tmp/test
AMIGA:barrywalker~> 
AMIGA:barrywalker~> cat /tmp/test
localhost subscriptions=[production, masters, fileserver, cldb, nfs] handler="genie"
host1.localhost.com a=apple
host2.localhost.com b=ball c==
AMIGA:barrywalker~> 
AMIGA:barrywalker~> grep -Eo '[[:alpha:]]+=([[:alpha:]]+|"[^"]*"'"|'[^']+')" /tmp/test
handler="genie"
a=apple
b=ball
AMIGA:barrywalker~> 
AMIGA:barrywalker~> # WHERE is the 'c' variable?
AMIGA:barrywalker~> _

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...

Well, first of all,

c=x=cat
klachlsuppe:~: echo $c

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.

Hi rovf...

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!