Extended grep not matching some patterns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extended grep not matching some patterns
# 8  
Old 12-22-2017
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:
Quote:
Match and print a string of the form:
Code:
variable=string

where variable is a string of one or more alphabetic characters and string is a string of one or more alphabetic characters, zero or more non-single-quote characters on a single line surrounded by single-quote characters, or zero or more non-double-quote characters on a single line surrounded by double-quote characters.
With the above specification, the following seems to work with your sample input:
Code:
grep -Eo '[[:alpha:]]+=([[:alpha:]]+|"[^"]*"'"|'[^']+')" /tmp/test

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

This User Gave Thanks to Don Cragun For This Post:
# 9  
Old 12-22-2017
Thank you SmilieSmilie
# 10  
Old 12-22-2017
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.
# 11  
Old 12-22-2017
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:
Code:
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...
# 12  
Old 12-22-2017
Quote:
Originally Posted by wisecracker
Bizarre as it seems strange things CAN happen...
Why is is strange? I don't see anything unusual here. With

Code:
var==

or the equivalent

Code:
var='='

you are assigning a string consisting of a single equal sign. With, say,

Code:
var=======

or, if you are in a funny mood

Code:
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

Code:
var='======'

# 13  
Old 12-22-2017
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...
Code:
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...
# 14  
Old 12-23-2017
Quote:
Originally Posted by wisecracker
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...
Code:
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~> _

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.
Well, first of all,

Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete patterns matching

Delete patterns matching OS version: RHEL 7.3 Shell : Bash I have a file like below (pattern.txt). I need to delete all lines starting with the following words (words separated by comma below) and ) character. LOGGING, NOCOMPRESS, TABLESPACE , PCTFREE, INITRANS, MAXTRANS, STORAGE,... (3 Replies)
Discussion started by: John K
3 Replies

2. Shell Programming and Scripting

Finding matching patterns in two files

Hi, I have requirement to find the matching patterns of two files in Unix. One file is the log file and the other is the error list file. If any pattern in the log file matches the list of errors in the error list file, then I would need to find the counts of the match. For example, ... (5 Replies)
Discussion started by: Bobby_2000
5 Replies

3. UNIX for Dummies Questions & Answers

Matching two patterns in the consecutive lines

Hi Experts I need to match 2 patterns consecutively and display 25 lines after that. 1st one - Error 2nd one - End string ( comes along with the pattern one) 3rd one - error Logic grep "ERROR OCCURRED :" trace.log | awk -v "ES=:" -v "SS=java.lang.NullPointerException" '{ if($NF ~... (8 Replies)
Discussion started by: senthil.ak
8 Replies

4. UNIX for Dummies Questions & Answers

Search and extract matching patterns

%%%%% (9 Replies)
Discussion started by: lucasvs
9 Replies

5. UNIX for Dummies Questions & Answers

Find records with matching patterns

Hi, I need to find records with a search string from a file. Search strings are provided in a file. For eg. search_String.txt file is like below chicago mexico newark sanhose and the file from where the records need to be fetched is given below src_file:... (1 Reply)
Discussion started by: sbhuvana20
1 Replies

6. Shell Programming and Scripting

print lines between 2 matching patterns

Hi Guys, I have file like below, I want to print all lines between test1231233 to its 10 occurrence(till line 41) test1231233 qwe qwe qweq123 test1231233 qwe qwe qweq23 test1231233 qwe qwe qweq123 test1231233 qwe qwe qweq123131 (3 Replies)
Discussion started by: jagnikam
3 Replies

7. Shell Programming and Scripting

Matching patterns

I have a file name in $f. If $f has "-" at the beginning, or "=", or does not have extension ".ry" or ".xt" or ".dat" then cerr would not be empty. Tried the following but having some problems. set cerr = `echo $f | awk '/^-|=|!.ry|!.xt|!.dat/'` (4 Replies)
Discussion started by: kristinu
4 Replies

8. Shell Programming and Scripting

AWK: matching patterns in 2 different files

In a directory, there are two different file extensions (*.txt and *.xyz) having similar names of numerical strings (*). The (*.txt) contains 5000 multiple files and the (*.xyz) also contains 5000 multiple files. Each of the files has around 4000 rows and 8 columns, with several unique string... (5 Replies)
Discussion started by: asanjuan
5 Replies

9. Shell Programming and Scripting

removing certain paragraphs for matching patterns

Hi, I have a log file which might have certain paragraphs. Switch not possible Error code 1234 Process number 678 Log not available Error code 567 Process number 874 ..... ...... ...... Now I create an exception file like this. cat text.exp Error code 1234 Process number 874 (7 Replies)
Discussion started by: kaushys
7 Replies

10. UNIX for Dummies Questions & Answers

matching 3 patterns in shell script

IN a file I need to check for 3 patterns if all the 3 patterns are in the file. I need to send out an email. All this needs to be done in korn shell script. Please advise. (1 Reply)
Discussion started by: saibsk
1 Replies
Login or Register to Ask a Question