The answer for me was to properly escape and quote as necessary special shell characters such as & before assuming the issue was with my grep regex that involved the alternation operator.įor example, the command I executed on my local machine was: get | grep "fizz\|buzz" | identical to pipe operator) in the grep regex at all. |) and not the alternation operator (i.e. The error I faced turned out to be with the previous pipe operator (i.e. I found this question while googling for a particular problem I was having involving a piped command to a grep command that used the alternation operator in a regex, so I thought that I would contribute my more specialized answer. I was suprised when this did not work: grep 'gene|exon' AT5G60410.gff I wanted to extract all lines that are of type "gene" or type "exon", specified in the third column. I am having some trouble extracting specific lines from this using grep. In ERE's $Id$ would never match anything because you can't have characters after the end of a line, so \$Id\$ would be the only way to go.The following is a sample of a large file named AT5G60410.gff: Chr5 TAIR10 gene 24294890 24301147. if you decided to use egrep (or grep -E), the $ character is always special. ¹ Note also that if you ever switch to Extended Regular Expressions (ERE), e.g. As explained in (1.), this regex matches a literal $Id only at the end of a line because the first $ is literal while the second is a special anchor character. The reason \$Id\$ does not work is because after shell quote removal (the more correct way of saying shell quoting) is applied, the regex that grep sees is $Id$. The following should do what you want: grep '\$Id\$' my_dir/my_file So, applying this back to your original example, let's throw the correct regex ( \$Id\$) inside single quotes. The single quote is the strongest form of quoting the shell has, so as long as your regex does not contain single quotes, you no longer have to worry about shell quoting and can focus on pure BRE syntax. Instead, the simplest way to do this is to first put your entire regex inside single quotes as in 'regex'. This is why you often see messes involving double backslashes, but I do not recommend using backslashes for shell quoting regular expressions because it is not very readable. The problem is many characters that regular expressions use are special to the shell as well, and on top of that the regex escape character, the backslash, is also a shell quoting character. There are two completely unrelated escaping/quoting mechanisms at work here: shell quoting and regex backslash quoting. Escaping the first $ also works: \$Id\$, and I prefer this since it looks more consistent.¹ To make the second $ match a literal $ you'll have to backslash escape it, i.e. The first one is a normal character and the second is an anchor that matches the end of the line. The consequence of this is that the 2 instances of $ in $Id$ are not equal. Grep uses Basic Regular Expressions (BRE), and $ is a special character in BRE's only at the end of an expression. Use the fgrepĬommand to avoid the necessity of using escape characters such as theīut I don't understand why grep \$Id works and why grep \\$Id\\$ doesn't. Literal character rather than an expression character. The \ (single backslash) character tells the grepĬommand to treat the following character (in this example the $) as a To force the shell to pass a \$ (single backslash, dollar sign) to the The \\ (double backslash) characters are necessary in order To search for a $ (dollar sign) in the file named test2, enter: It doesn't matter if the second $ is the last character or not.īefore posting my question, I used google. How could I match the second $ and why doesn't \$Id\$ work. Then I see that the $Id is colored in the output, i.e. I want to know which files have the string $Id$.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |