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" 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 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 |
Why not use single quotes and forgo the headache??
|
Quote:
I also noticed that grep behavior changes if I have \ written on command line and when contained in variables |
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. |
Quote:
Can I work with single quotes and still expand variables ? |
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? |
Quote:
Kevin Barry |
Quote:
Code:
search='\' |
Quote:
|
Try this code.
Code:
echo "my\name" | grep 'my\\name' |
Why don't we take a look at the documentation, and learn just how the shell processes backslashes?
man bash: Code:
QUOTING 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. |
Quote:
One last question, maybe a silly one, do all the commands do their own processing too for backslashes ? |
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\)' |
All times are GMT -5. The time now is 08:56 AM. |