Questions tagged [regular-expression]

Regular expressions are a means of matching a pattern of characters within a string.

Regular expressions (regex or regexp for short) are patterns that describe a set of strings based on simple syntactic criteria.

Tools such as , and involve regular expression matching as a key feature. Most programming languages include a regular expression library, and most text editors provide regular expression search and replacement.

Shell for file names (also called glob patterns) are a different, less powerful syntax (though ksh, bash and zsh have more powerful extensions). Bash, ksh93 and zsh's =~ operator offer regex matching.

External reference

Further reading

2730 questions
797
votes
13 answers

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 complains when bar isn't an executable. How can I…
Dan
  • 9,372
  • 5
  • 25
  • 39
557
votes
8 answers

Can grep output only specified groupings that match?

Say I have a file: # file: 'test.txt' foobar bash 1 bash foobar happy foobar I only want to know what words appear after "foobar", so I can use this regex: "foobar \(\w\+\)" The parenthesis indicate that I have a special interest in the word right…
Cory Klein
  • 18,391
  • 26
  • 81
  • 93
431
votes
18 answers

How can I use sed to replace a multi-line string?

I've noticed that, if I add \n to a pattern for substituting using sed, it does not match. Example: $ cat > alpha.txt This is a test Please do not be alarmed $ sed -i'.original' 's/a test\nPlease do not/not a test\nBe/' alpha.txt $ diff…
Belmin Fernandez
  • 9,347
  • 15
  • 46
  • 50
309
votes
5 answers

What is the difference between `grep`, `egrep`, and `fgrep`?

Can anyone tell me the technical differences between grep, egrep, and fgrep and provide suitable examples? When do I need to use grep over egrep and vice versa?
Vishwanath Dalvi
  • 4,346
  • 5
  • 20
  • 17
270
votes
5 answers

How to use find command to search for multiple extensions

I can get all jpg images by using: find . -name "*.jpg" But how can I add png files to the results as well?
wong2
  • 3,493
  • 3
  • 17
  • 8
219
votes
7 answers

Using sed to find and replace complex string (preferrably with regex)

I have a file with the following contents: and I need to make a script that changes the "name" in the first line to "something", the…
Harry Muscle
  • 2,437
  • 2
  • 13
  • 8
163
votes
9 answers

How to run grep with multiple AND patterns?

I would like to get the multi pattern match with implicit AND between patterns, i.e. equivalent to running several greps in a sequence: grep pattern1 | grep pattern2 | ... So how to convert it to something like? grep pattern1 & pattern2 &…
greenoldman
  • 6,086
  • 16
  • 54
  • 65
111
votes
1 answer

Why does my regular expression work in X but not in Y?

I wrote a regular expression which works well in a certain program (grep, sed, awk, perl, python, ruby, ksh, bash, zsh, find, emacs, vi, vim, gedit, …). But when I use it in a different program (or on a different unix variant), it stops matching.…
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
84
votes
2 answers

Grep 'OR' regex problem

I am trying to use grep with a regex to find lines in a file that match 1 of 2 possible strings. Here is my grep: $ grep "^ID.*(ETS|FBS)" my_file.txt The above grep returns no results. However if I execute either: $ grep "^ID.*ETS" my_file.txt …
dr.bunsen
  • 1,759
  • 3
  • 14
  • 11
78
votes
4 answers

grep lines starting with "1" in Ubuntu

I try to search for lines that start with "1" using ls -1 | grep ^1* but it returns lines that do not start with 1. What I am missing here?
Tim
  • 98,580
  • 191
  • 570
  • 977
73
votes
5 answers

Why does '\r' (and not '\n') work to replace with a newline in 'vim'?

In vim editor, I want to replace a newline character (\n) with two new line characters (\n\n) using vim command mode. Input file content: This is my first line. This is second line. Command that I tried: :%s/\n/\n\n/g But it replaces the string…
Raghvendra
  • 1,002
  • 1
  • 7
  • 13
72
votes
7 answers

Find files in multiple folder names

I am trying to list all the files from dir1, dir2, dir3 and dir4 which might be anywhere in as a sub directory of my cwd using the find command. I tried the following with no success: find . -type f -regextype posix-egrep -regex…
Aaron
  • 1,497
  • 4
  • 14
  • 15
72
votes
2 answers

Find files with certain extensions

How can I use find to find all files that have a .xls or .csv extension? I have seen a -regex option but I don't know how to use it.
MCS
70
votes
11 answers

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) read scale if ! [[ "$scale" =~ "^[0-9]+$" ]] …
lonewarrior556
  • 2,033
  • 4
  • 16
  • 13
67
votes
6 answers

Replacing Multiple blank lines with a single blank line in vim / sed

Question more or less says it all. I'm aware that /^$/d will remove all blank lines, but I can't see how to say 'replace two or more blank lines with a single blank line' Any ideas?
Andrew Bolster
  • 955
  • 2
  • 7
  • 9
1
2 3
99 100