How does one escape special characters in the cscope "Find this egrep pattern" ?
For instance I want to find everywhere in a huge codebase where I have the statement
print("\n[<something>.....
I need to find every line where a print format specifier string has a newline immediately followed by a left bracket character.
How do I feed this to the egrep pattern search?
I tried:
Find this egrep pattern: \\n[
Find this egrep pattern: \\n\[
got :
Egrep Syntax error in this pattern: \n[
Could not find the egrep pattern: \\n\[
I use cscope version 15.8b, using the pattern \\n\[ will match the string you want.
P.S., the "egrep" in cscope might confuse us. As the bugzilla page I googled, it is a little implementation of "extended" regular expression, its behavior might be not the same with egrep util of Linux command line.
Place the special characters to be searched within [ ] brackets.
Example:
Find this egrep pattern:
print[("\]n
Related
So here's my issue. I need to develop a small bash script that can grep a file containing account names (let's call it file.txt). The contents would be something like this:
accounttest
account2
account
accountbtest
account.test
Matching an exact line SHOULD be easy but apparently it's really not.
I tried:
grep "^account$" file.txt
The output is:
account
So in this situation the output is OK, only "account" is displayed.
But if I try:
grep "^account.test$" file.txt
The output is:
accountbtest
account.test
So the next obvious solution that comes to mind, in order to stop interpreting the dot character as "any character", is using fgrep, right?
fgrep account.test file.txt
The output, as expected, is correct this time:
account.test
But what if I try now:
fgrep account file.txt
Output:
accounttest
account2
account
accountbtest
account.test
This time the output is completely wrong, because I can't use the beginning/end line characters with fgrep.
So my question is, how can I properly grep a whole line, including the beginning and end of line special characters, while also matching exactly the "." character?
EDIT: Please note that I do know that the "." character needs to be escaped, but in my situation, escaping is not an option, because of further processing that needs to be done to the account name, which would make things too complicated.
The . is a special character in regex notation which needs to be escaped to match it as a literal string when passing to grep, so do
grep "^account\.test$" file.txt
Or if you cannot afford to modify the search string use the -F flag in grep to treat it as literal string and not do any extra processing in it
grep -Fx 'account.test' file.txt
From man grep
-F, --fixed-strings
Interpret PATTERN as a list of fixed strings (instead of regular expressions), separated by newlines, any of which is to be matched.
-x, --line-regexp
Select only those matches that exactly match the whole line. For a regular expression pattern, this is like parenthesizing the pattern and then surrounding it with ^ and $.
fgrep is the same as grep -F. grep also has the -x option which matches against whole lines only. You can combine these to get what you want:
grep -Fx account.test file.txt
I'm trying to find something in a file with a pattern using the '*' but is not working, any idea how to do this?
this is what I'm trying to do:
grep "files*.txt" $myTestFile
is not returning anything, it's suppose that "*" should be all.
By default, grep doesn't support extended regular express, but grep -E or egrep do.
egrep "files.*\.txt" $myTestFile
or
grep -E "files.*\.txt" $myTestFile
In addition, three variant programs egrep, fgrep and rgrep are available. egrep is the same as grep -E. fgrep is the same as grep -F. rgrep is the same as grep -r. Direct
invocation as either egrep or fgrep is deprecated, but is provided to allow historical applications that rely on them to run unmodified.
Matcher Selection
-E, --extended-regexp
Interpret PATTERN as an extended regular expression (ERE, see below). (-E is specified by POSIX.)
-F, --fixed-strings
Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched. (-F is specified by POSIX.)
-G, --basic-regexp
Interpret PATTERN as a basic regular expression (BRE, see below). This is the default.
-P, --perl-regexp
Interpret PATTERN as a Perl regular expression (PCRE, see below). This is highly experimental and `grep -P` may warn of unimplemented features.
If you only want to match the exact string files*.txt, that would be:
# match exactly "files*.txt"
grep -e "files[*][.]txt" "$myTestFile"
...or, more simply put using fgrep to match only the exact string given:
# match exactly "files*.txt"
fgrep -e 'files*.txt' "$myTestFile"
[*] defines a character class with only a single character -- the * -- contained, and thus matches only that one character. Backslash-based escaping is also possible, but can have different meanings in different contexts and thus is less reliable.
If you want to match any line that contains files, and later .txt, then:
# match any line containing "files" and later ".txt"
grep -e "files.*[.]txt" "$myTestFile"
.* matches zero-or-more characters, and is thus the regex equivalent to the glob-pattern *. Likewise, whereas in a glob pattern . matches only itself, in a regex . can match any character, so the . in .txt needs to be escaped, as in [.]txt, to prevent it from matching anything else.
Can anyone explain the following command?
What happens if the same code is executed using grep?
sed 's/^.*-S[[:space:]]*\([^[:space:]]*\).*$/\1/'
As a sed command, it replaces something like -S paradox (possibly with text on either side the material), with paradox. (There must be a space after paradox for exactly the string paradox to be printed; if there are non-space characters immediately after the word, then those are included in the output too, up to the first space.) For example, the input line someprog -x painter -S paradox file2 file93 yields paradox.
If you apply the expression to grep, then the ^ and the $ lose their special meanings, and it looks for a line such as:
schemas/^semicolon-S(comma)$/(comma)/gratitude
In the grep context, the \( and \) do remember a pattern (and in the example line, that pattern corresponds to (comma) — to confuse you, and me). The \1 then refers to the previously remembered string, the second (comma) in the example. You could drop all the parentheses in the sample line and it would be selected. If your version of grep supports the -o option to output only the text that matches, you can see more clearly which parts of the sample line match the regex.
I am trying to grep for a line present in file:
devnode "^sda[1-9]?$"
The command that I am using is :
grep 'devnode \"\^sda\[1-9\]\?\$\"' $file
But it doesn't seems to work. I have made sure to use $ like "grep '\$'" as reported in some solutions but it doesn't work.
Can anyone help me on this one, its frustrating.
Thanks
grep 'devnode "^sda\[1-9\]?$"' file
Grep understands two different versions of regular expression syntax: "basic" and "extended."
grep 'foo' file / use Basic Regular Expressions
grep -E 'foo' file / use Extended regular expressions.
If use Basic Regular Expressions, ?, +, {, |, ( and ) have no special meaning.
If use Extended Regular Expressions, like this.
grep -E 'devnode "\^sda\[1-9\]\?\$"' file
grep 'devnode "^sda\[1-9]?\$"' $file
In POSIX BASIC REGEXP – https://www.mirbsd.org/man7/re_format – some chars only become special with the backslash, instead of without it.
I have a problem.Please give me a solution.
I have to run a command as I given below, which will list all the files that contain the string given "abcde1234".
find /path/to/dir/ * | xargs grep abcde1234
But here it will display the files which contain the string "abcde1234567" also.But I nee only files which contain the word "abcde1234". What modification shall I need in the command ??
When I need something like that, I use the \< and \> which mean word boundary. Like this:
grep '\<abcde1234\>'
The symbols \< and \> respectively match the empty string at the beginning and end of a word.
But that's me. The correct way might be to use the -w switch instead (which I tend to forget about):
-w, --word-regexp
Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it
must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the underscore.
One more thing: instead of find + xargs you can use just find with -exec. Or actually just grep with -r:
grep -w -r abcde1234 /path/to/dir/
$ grep abcde1234 *
This will grep the string abcde1234 in current directory, with the file name in which the string is.
Ex:
abc.log: abcde1234 found
Hi I got the answer for this.By attaching $ with word to be searched, it will display the files that contain only that word.
The command will be like this.
find /path/to/dir/ * | xargs grep abcde1234$
Thanks.