Understanding pattern matching used in a grep command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Understanding pattern matching used in a grep command
# 1  
Old 03-06-2012
Understanding pattern matching used in a grep command

I have the following code. I want to remove the --sort=num/num/... and am
using grep to exclude it as shown below:

I have a bit of problem figuring out the use of [-]- at the front

Code:
echo "--sort=4/5/6"  | grep -ivE '[-]-((sort|group)=[0-9]+/[0-9]+(/[0-9]+)*)$'

Now suppose I want to remove --quiet

I can do

Code:
echo "--sort=4/5/6"  | grep -ivE '[-]-(quiet)'

or

Code:
 echo "--sort=4/5/6"  | grep -ivE '(--quiet)'

They both seem to work find but cannot figure out the different. I think using

Code:
  echo "--sort=4/5/6"  | grep -ivE '(--quiet)'

will suffice.

I am also thinking of using

Code:
echo "--sort=4/5/6"  | grep -ivE '(--(sort|group)=[0-9]+/[0-9]+(/[0-9]+)*)$'

instead of using

Code:
echo "--sort=4/5/6"  | grep -ivE '[-]-((sort|group)=[0-9]+/[0-9]+(/[0-9]+)*)$'

# 2  
Old 03-06-2012
The reason [-]- is used is to prevent grep from interpreting your pattern as a command line flag. You could also use this:

Code:
echo "--sort=4/5/6"  | grep -ivE -- '--quiet'

The -- outside of your pattern is a flag to cause grep to stop looking for command line flags.
This User Gave Thanks to agama For This Post:
# 3  
Old 03-06-2012
Which is the most popular?
[-]- or --

---------- Post updated at 08:53 PM ---------- Previous update was at 08:47 PM ----------

I tried to look up the [-]- or -- declaration for grep and cannot find a reference for it.
# 4  
Old 03-06-2012
Quote:
Originally Posted by kristinu
Which is the most popular?
[-]- or --
Placing -- ahead of the pattern is the defacto standard way to indicate end of command line flags. Most commands that expect dashed command line flags recognise the double dash with trailing whitespace to mean the end of the list and to assume the next token is the first positional parameter.

Using the [-]- obviously works, but makes the intent of the pattern less obvious.

I was going to quote the grep man page, but it makes no mention of this.
This User Gave Thanks to agama For This Post:
# 5  
Old 03-06-2012
The thing is that if I bracket --help, grep does not run the --help command line option.
Code:
echo "--help" | grep -iE '(--help)'

But doing
Code:
echo "--help" | grep -iE '--help'

does run the help option of grep.

Using the brackets did not require use of [-]- or -- in front of the pattern.

Correct me if I am wrong on the following:

In the regular expression [-]-help, the - inside the square brackets matches a character set which contains a single character ( - in this case) and the literal string "-help".
# 6  
Old 03-06-2012
Code:
echo "--help" | grep -iE '--help'

grep sees '--help' as an option.

If I were putting this into a script I'd code it this way:

Code:
echo "--help" | grep -iE --    '--help'

Extra spaces aren't needed, just set the pattern off a bit. The first pair of dashes end greps search for flags, and thus it treats --help as the pattern.

---------- Post updated at 21:36 ---------- Previous update was at 21:34 ----------

Quote:
Correct me if I am wrong on the following:

In the regular expression [-]-help, the - inside the square brackets matches a character set which contains a single character ( - in this case) and the literal string "-help".
You are correct.

Last edited by agama; 03-06-2012 at 11:37 PM.. Reason: removed extra jibberish
# 7  
Old 03-06-2012
Yes, [-] is any one character from the list -
i.e. the list is only a single hyphen.

A very similar trick is also quite commonly used to avoid grep finding it's self when searching ps listings:
Code:
$ ps -ef | grep "xntpd"
   root  528  1394   0   Mar 12      - 15:00 /usr/sbin/xntpd 
chubler 1882 15279   0 12:37:00 pts/20  0:00 grep xntpd
 
$ ps -ef | grep "[x]ntpd"
   root  528  1394   0   Mar 12      - 15:00 /usr/sbin/xntpd

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Expect pattern matching in the command output

This is the command output need to be matched: Telnet console listening to port 42365. (the port number changes every time) Code to test it: ======================================= #!/tools/AGRtools/bin/expect exp_internal 1 set timeout 10 spawn bash set bashId $spawn_id ... (4 Replies)
Discussion started by: marsala
4 Replies

2. UNIX for Beginners Questions & Answers

Grep file starting from pattern matching line

I have a file with a list of references towards the end and want to apply a grep for some string. text .... @unnumbered References @sp 1 @paragraphindent 0 2017. @strong{Chalenski, D.A.}; Wang, K.; Tatanova, Maria; Lopez, Jorge L.; Hatchell, P.; Dutta, P.; @strong{Small airgun... (1 Reply)
Discussion started by: kristinu
1 Replies

3. Shell Programming and Scripting

UNIX command to get the latest file and also matching pattern

we want to fetch the latest file in a given directory and also the file name should match the below pattern Example file name ->hrdata-2015-10-13-16-45-26.xml(2015-10-13-16-45-26- it is not current timestamp, we just need to check for the pattern) We expect the file will have the... (3 Replies)
Discussion started by: vishwanath001
3 Replies

4. Shell Programming and Scripting

Grep log file to get line above matching pattern

Hi, I have a log file that looks like this "delete" : { "_type" : "cl", "_id" : "1000600000000562636", "_version" : 1, "status" : 200, "found" : false } }, { "delete" : { "_type" : "cl", "_id" : "1000600000000562643", ... (4 Replies)
Discussion started by: wahi80
4 Replies

5. UNIX for Dummies Questions & Answers

Grep -v lines starting with pattern 1 and not matching pattern 2

Hi all! Thanks for taking the time to view this! I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern. Example: Drink a soda Eat a banana Eat multiple bananas Drink an apple juice Eat an apple Eat multiple apples I... (8 Replies)
Discussion started by: demmel
8 Replies

6. UNIX for Dummies Questions & Answers

Issues while pattern matching using grep

Hi, I have a file f1 wi the following data f1.txt ======== Report ID Report Name ----------------------------------------------------------------- Post Requests : 2 Post successes : 2 ============================================= I need to search for the... (2 Replies)
Discussion started by: RP09
2 Replies

7. UNIX for Dummies Questions & Answers

grep only word matching the pattern

Hi gurus, A file contains many words in format "ABC.XXXX.XXXX.X.GET.LOG" (X->varying). Now my shell script want this list (only words in formatABC.XXXX.XXXX.X.GET.LOG ) to continue the process. Pls help me. Thanks, Poova. (8 Replies)
Discussion started by: poova
8 Replies

8. AIX

matching pattern in 'ps' command

Hi all, I have two instance of jboss (jboss and jboss2). In a shell script that I am writing, I need to grep for each instance of jboss. If i use ps -ef|grep 'jboss', it will also return the process for my second instance of jboss, which is jboss2. Can someone tell me how can i specify the... (1 Reply)
Discussion started by: haroon_a
1 Replies

9. Shell Programming and Scripting

using command line arguments as columns for pattern matching using awk

Hi, I wish to use a column, as inputted by a user from command line, for pattern matching. awk file: { if($1 ~ /^8/) { print $0> "temp2.csv" } } something like this, but i want '$1' to be any column as selected by the user from command line. ... (1 Reply)
Discussion started by: invinclible0009
1 Replies

10. Shell Programming and Scripting

perl pattern matching vs. grep

I originally had a shell script that did a grep 10 times to pull out the number of times a certain pattern occured in a file: ie... aOccurances=`grep aPattern file|wc -l` bOccurances=`grep bPattern file|wc -l` ... ... ... fOccurances=`grep fPattern file|wc -l` As the file got bigger with... (0 Replies)
Discussion started by: junkmail426
0 Replies
Login or Register to Ask a Question

Featured Tech Videos