Extended grep not matching some patterns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extended grep not matching some patterns
# 1  
Old 12-21-2017
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 ?


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

# 2  
Old 12-21-2017
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?
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 12-21-2017
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
# 4  
Old 12-21-2017
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.)
# 5  
Old 12-22-2017
i need to get matches around "=" , am trying for output like below

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

# 6  
Old 12-22-2017
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:
Quote:
It can probably be done with grep if you clearly define EXACTLY what it is that you are trying to match with your ERE.
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.
# 7  
Old 12-22-2017
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.
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