LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-08-2015, 12:39 PM   #1
rookee
LQ Newbie
 
Registered: Jun 2014
Posts: 28

Rep: Reputation: Disabled
Question on shell scripting: grep with backslash


Hello Sirs,

I have a question about using backslash with grep. For searching -5 pattern in the below file.

[root@localhost script-practice]# cat phone.list
Smith, Terry 7-7989
Adams, Fran 2-3876
StClair, Pat 4-6122
Brown, Robin 1-3745
Stair, Chris 5-5972
Benson, Sam 4-5587

In the shell programming book by Ray Swartz, it is mentioned that to perform a grep operation for -5 pattern search we need to use \(backslash) before - to pass it to the grep as a character pattern to search but not as an option. It is mentioned that we straight away can't use grep \-5 phone.list instead we need to use grep \\-5 phone.list or grep '\-5' phone.list. Why do we have to use the second \ when single \ already takes care of quoting the following character - and removing its special meaning and treats it as a normal character. Please explain. Thanks in advance.
 
Old 06-08-2015, 01:27 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,776

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
The command line is first processed by the shell. The result of an unquoted \- will just be -, and that is all that will be passed to grep, which will treat it as an option flag. The double \\ or a quoted '\' will result in a single \ being passed to grep.

But, this is really the wrong way to indicate that "-5" is not a flag option. The right way is
Code:
grep -- -5 phone list
The "--" is the standard way to indicate that no more option arguments follow, and that nothing that follows should be treated as an option.
 
2 members found this post helpful.
Old 06-08-2015, 02:17 PM   #3
rookee
LQ Newbie
 
Registered: Jun 2014
Posts: 28

Original Poster
Rep: Reputation: Disabled
Thank you very much for the explanation rknichols.
 
Old 06-08-2015, 03:04 PM   #4
joec@home
Member
 
Registered: Sep 2009
Location: Galveston Tx
Posts: 291

Rep: Reputation: 70
A practical example of this, say you are search for an instance of code in a script:

awk '{print $1}' input

The "'" "{" "}" symbols would try to be interpreted by grep as part of the grep regex functions. So you would use the "" symbol to escape them:

Incorrect
grep "awk '{print $1}' input" myfile

Correct
grep "awk \'\{print $1\}\' input" myfile

Last edited by joec@home; 06-08-2015 at 03:06 PM.
 
1 members found this post helpful.
Old 06-10-2015, 05:44 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Use single quotes to protect regex from the shell thus
Code:
grep '\-5' yourfile

Stair, Chris 5-5972
Benson, Sam 4-5587
 
2 members found this post helpful.
Old 06-10-2015, 06:46 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
you can also try [], that is sometimes more readable:
grep '[-]5'
 
2 members found this post helpful.
Old 06-10-2015, 01:30 PM   #7
joec@home
Member
 
Registered: Sep 2009
Location: Galveston Tx
Posts: 291

Rep: Reputation: 70
Quote:
Originally Posted by chrism01 View Post
Use single quotes to protect regex from the shell thus
Code:
grep '\-5' yourfile

Stair, Chris 5-5972
Benson, Sam 4-5587
Just to add to this comment the difference between single and double quote.

'123 $A$B$C xyz' verses "123 $A$B$C xyz"

In the single quotes the variable will get passed literally as $A$B$C, where as in the double quote the variables will get processed showing what information is stored and then passed.

Code:
[root@localhost ~]# A="TEST1"
[root@localhost ~]# B="TEST2"
[root@localhost ~]# C="TEST3"
[root@localhost ~]# echo '123 $A$B$C xyz'
123 $A$B$C xyz
[root@localhost ~]# echo "123 $A$B$C xyz"
123 TEST1TEST2TEST3 xyz
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] shell scripting - Grep a character class containing '-' Ramurd Programming 6 08-25-2014 09:19 AM
[SOLVED] Number of backslash character to use in grep luvshines Programming 12 10-03-2011 11:57 PM
LXer: Using Grep To Streamline Your Shell And Command Line Scripting LXer Syndicated Linux News 0 08-08-2008 07:11 AM
grep: Trailing backslash fakie_flip Linux - Software 9 12-17-2007 09:17 AM
line addressing with grep (shell scripting) j2dizzo Linux - General 13 03-03-2004 09:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration