Skip to main content

All Questions

806 votes
13 answers
1.8m views

How do I grep for multiple patterns with pattern having a pipe character?

I want to find all lines in several files that match one of two patterns. I tried to find the patterns I'm looking for by typing grep (foo|bar) *.txt but the shell interprets the | as a pipe and ...
Dan's user avatar
  • 9,532
72 votes
11 answers
235k views

Checking if an input number is an integer

I'm trying to check if an input is an integer and I've gone over it a hundred times but don't see the error in this. Alas it does not work, it triggers the if statement for all inputs (numbers/letters)...
lonewarrior556's user avatar
13 votes
2 answers
5k views

Why do I need to escape regex characters in sed to be interpreted as regex characters?

It seems e.g. cat sed_data.txt | sed 's/\b[0-9]\{3\}\b/NUMBER/g' that I must escape characters to form a regular expression. In this case I had to escape braces in order to be interpreted as a number ...
Jim's user avatar
  • 10.2k
57 votes
2 answers
114k views

grep and escaping a dollar sign

I want to know which files have the string $Id$. grep \$Id\$ my_dir/mylist_of_files returns 0 occurrences. I discovered that I have to use grep \$Id$ my_dir/mylist_of_files Then I see that the $...
Luc M's user avatar
  • 4,185
10 votes
3 answers
17k views

shell test whether multiple lines string contains specified pattern in last line

I want to determine whether a multi-line string ends with a line containing specified pattern. These code failed, it doesn't match. s=`echo hello && echo world && echo OK` [[ "$s" =~ ...
gzc's user avatar
  • 325
10 votes
4 answers
6k views

Regular expression using \\ vs using \

Why does grep e\\.g\\. <<< "this is an e.g. wow" and grep e\.g\. <<< "this is an e.g. wow" do the same thing? If I add a third slash, it also has the same result. BUT, once I ...
Wyatt Grant's user avatar
10 votes
3 answers
51k views

Why can't find -regex match a newline?

Why does this fail? touch "$(printf "a\nb")"; find . -regex './.\n.' I also tried these, none of which work: find . -regextype posix-extended -regex '.\n.' find . -regextype posix-awk -regex '.\n.' ...
terdon's user avatar
  • 245k
6 votes
3 answers
2k views

Why does my grep expression need to use $'string' to match tab characters?

If you take this code: echo -e '\t\t\tString' | grep '^[\t]*String' the result is blank because it doesn't match, yet this: echo -e '\t\t\tString' | grep $'^[\t]*String' works. I swear that I must ...
Tal's user avatar
  • 2,182
17 votes
2 answers
24k views

Why do I have to escape a "dot" twice?

I know that we can escape a special character like *(){}$ with \ so as to be considered literals. For example \* or \$ But in case of . I have to do it twice, like \\. otherwise it is considered ...
Registered User's user avatar
13 votes
2 answers
13k views

Number of backslashes needed for escaping regex backslash on the command-line

I recently had trouble with some regex on the command-line, and found that for matching a backslash, different numbers of characters can be used. This number depends on the quoting used for the regex (...
daniel kullmann's user avatar
8 votes
3 answers
33k views

Escaping * with Regular Expressions and Grep

I have a file that has unique lines that start with 2 stars (**). However when I run a grep command for grep \*\* fileName I get all of the lines in the file. This is very unusual, and what I see ...
monksy's user avatar
  • 743
5 votes
1 answer
7k views

command: ls /etc | sort | grep d* is yielding no results but ls /etc | sort | grep p* lists entire directory

I am playing around with piping and grep tonight. I know that grep uses regex and that * means 0 or more occurrences of the preceding character. So the way I understand it is that if I do the ...
Brad Harris's user avatar
1 vote
0 answers
2k views

Why use a variable in a bash regex match inside [[? [duplicate]

There is not a simple answer to this question at this site yet. This question aims to give a simple and clear answer. It is usually recommended to use a variable ($regex) in this construct: if [[ $...
user avatar