LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-08-2011, 01:07 AM   #1
luvshines
Member
 
Registered: Apr 2009
Posts: 65

Rep: Reputation: 16
Question Number of backslash character to use in grep


And old problem but still confuses me.

Have not been able to find any good documentation about the number of backslash characters to be used in grep command

If I try this, it fails
Code:
echo "my\name" | grep "my\name"
Though maybe grep needed a \\ since documentation says so but that too fails.
Adding another \, ie, using \\\ succeeds.
Then keeps on succeeding upto 6 backslashes, \\\\\\ and from 7th onwards starts failing

Things start getting even worse if \ appears at the end
Code:
echo "my\\" | grep "my\\"
This starts working with 4 backslashes only

This has confused me totally. With single quotes, it works differently, which is expected but still not clear how many to use.

Is it because of the piping of echo and grep that the behaviour changes ?
Any proper documentation which explains how this works
 
Old 09-08-2011, 01:19 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,690

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
Why not use single quotes and forgo the headache??
 
Old 09-08-2011, 03:28 AM   #3
luvshines
Member
 
Registered: Apr 2009
Posts: 65

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by grail View Post
Why not use single quotes and forgo the headache??
The word/line to be grepped in my scripts are generally contained in variables which don't expand in single quotes '$var'

I also noticed that grep behavior changes if I have \ written on command line and when contained in variables
 
Old 09-08-2011, 05:33 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,690

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
Have a look about 2/3 down this page :- http://tldp.org/LDP/abs/html/escapingsection.html#ESCP

I think the explanation is quite clear.
 
Old 09-11-2011, 03:35 PM   #5
luvshines
Member
 
Registered: Apr 2009
Posts: 65

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by grail View Post
Have a look about 2/3 down this page :- http://tldp.org/LDP/abs/html/escapingsection.html#ESCP

I think the explanation is quite clear.
, the link itself says that the behaviour of backslashes is inconsistent. Do I have to live with this ?

Can I work with single quotes and still expand variables ?
 
Old 09-11-2011, 04:19 PM   #6
8-bit
Member
 
Registered: Jan 2009
Location: Southern Oregon under a rock.
Distribution: Puppy 431 SCSI, Lucid 520, Slacko, Win 7
Posts: 131

Rep: Reputation: 49
In your example, remember that the back-slash is a delimiter for grep.
So your example of echo 'my\\' | grep 'my\\' will not work.
Why?
Because you have not used a delimiter for the second back-slash in grep for the input string.
In other words, echo 'my\\' | grep 'my (delimiter then back-slash) only handles the first back-slash.
A second (delimiter then back-slash) is needed in grep to handle the second back-slash in your input string.
A delimiter is needed in grep to handle EACH reserved character in the input string.

Clear as mud?
 
Old 09-11-2011, 04:56 PM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by luvshines View Post
, the link itself says that the behaviour of backslashes is inconsistent. Do I have to live with this ?

Can I work with single quotes and still expand variables ?
Single-quote the pattern except where variable expansion is needed. For that, take advantage of adjacent strings being concatenated, e.g. 'part1'"$var"'part2'.
Kevin Barry
 
1 members found this post helpful.
Old 09-24-2011, 11:01 AM   #8
luvshines
Member
 
Registered: Apr 2009
Posts: 65

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by ta0kira View Post
Single-quote the pattern except where variable expansion is needed. For that, take advantage of adjacent strings being concatenated, e.g. 'part1'"$var"'part2'.
Kevin Barry
That won't help either
Code:
search='\'
echo "\\" | grep $search
grep: Trailing backslash

echo "\\" | grep "$search"
grep: Trailing backslash

echo "\\" | grep ''$search''
grep: Trailing backslash
 
Old 09-24-2011, 11:04 AM   #9
luvshines
Member
 
Registered: Apr 2009
Posts: 65

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by 8-bit View Post
In your example, remember that the back-slash is a delimiter for grep.
So your example of echo 'my\\' | grep 'my\\' will not work.
Why?
Because you have not used a delimiter for the second back-slash in grep for the input string.
In other words, echo 'my\\' | grep 'my (delimiter then back-slash) only handles the first back-slash.
A second (delimiter then back-slash) is needed in grep to handle the second back-slash in your input string.
A delimiter is needed in grep to handle EACH reserved character in the input string.

Clear as mud?
In the example I quoted, I used double-quotes echo "my\\" which will in effect give a single quote and going by your logic, grep 'my\\' should have worked since there is only one backslash in input string
 
Old 09-25-2011, 01:16 PM   #10
8-bit
Member
 
Registered: Jan 2009
Location: Southern Oregon under a rock.
Distribution: Puppy 431 SCSI, Lucid 520, Slacko, Win 7
Posts: 131

Rep: Reputation: 49
Try this code.
Code:
echo "my\name" | grep 'my\\name'
I am actually shooting in the dark here since I am trying to guess if you are searching for "dir/file" in a file containing file name paths. Your input string is the same. The only difference here is the addition of a delimiter and single quotes to the grep search.
 
Old 09-27-2011, 05:19 PM   #11
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
Why don't we take a look at the documentation, and learn just how the shell processes backslashes?

man bash:
Code:
QUOTING
	.....

	Enclosing characters in double quotes preserves the literal
	value of all characters within the quotes, with the exception
	of $, `, \, and, when history expansion is enabled, !.  The
	characters $ and ` retain their special meaning within double
	quotes.  The backslash retains its special meaning only when
	followed by one of the following characters: $, `, ", \, or
	<newline>.  A double quote may be quoted within double quotes
	by preceding it with a backslash.  If enabled, history expansion
	will be performed unless an ! appearing in double quotes is
	escaped using a backslash. The backslash preceding the ! is not
	removed.
So if a string is double quoted, then \$, \`, \", \\, and \<newline> will all be converted to their literal equivalents. All other backslash combinations will be passed on literally (as opposed to not quoting the string, where all backslashes are processed).

Note especially that a single backslash at the end of the string will escape the following closing quote-mark, and break shell syntax.

The final processed string is then passed on to grep, which also does it's own backslash parsing.

Hopefully now you can figure out how many backslashes are needed to protect grep's reserved characters from interpretation. Using echo "string\with\backslashes" will print the string as grep (or any other command) would see it.

Finally, don't forget that backslash processing is also done when you declare the value of a variable, so you have to take that step into consideration as well. Using single quotes when setting the variable, to preserve the whole string literally, is probably what you'll usually want to do.
 
Old 10-02-2011, 02:03 PM   #12
luvshines
Member
 
Registered: Apr 2009
Posts: 65

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by David the H. View Post

The final processed string is then passed on to grep, which also does it's own backslash parsing.
Ahh!! I think I was missing this. String processing done twice, grep doing its own too. I think echo was not doing it, unless I use echo -e

One last question, maybe a silly one, do all the commands do their own processing too for backslashes ?
 
Old 10-04-2011, 12:57 AM   #13
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
Your shell simply parses the line according to its syntax, then executes the resulting command and arguments. What happens next is up to the command in question.

If it's a command that parses complex expressions of some kind, then it will certainly use some kind of escaping to handle any characters that it considers reserved syntax. Regular expressions syntax generally also relies on the backslash, for example, for its escaping, so any command that parses regex will use them.


By the way, make a note that the regular expressions used in grep and sed have two different modes. In basic regex mode, certain characters are "off" by default, and backslashing them actually turns them on. Whereas if you enable the extended regex mode, the inverse occurs. These two expressions are equivalent, for example:

Code:
$ echo -e 'foo\nbar\nbaz' | grep '\(foo\|bar\)'
foo
bar

$ echo -e 'foo\nbar\nbaz' | grep -E '(foo|bar)'
foo
bar
See the "basic vs extended regex" section in the grep man page for more.
 
  


Reply

Tags
grep


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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] Using grep with the accent character present eddyq Linux - Newbie 4 07-08-2011 05:46 PM
[SOLVED] Grep until certain character or pattern appears ohijames Programming 7 06-28-2010 09:38 PM
Weird backslash character in NFS path names mocambo Linux - Networking 2 09-05-2009 08:54 PM
converting Dos link backslash to Unix like backslash barunparichha Linux - Software 1 05-14-2009 10:54 AM
grep: Trailing backslash fakie_flip Linux - Software 9 12-17-2007 10:17 AM


All times are GMT -5. The time now is 08:05 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration