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

learn more… | top users | synonyms (1)

25
votes
1answer
957 views

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 ...
56
votes
4answers
24k views

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

Can any one tell me the technical difference between grep and egrep, and fgrep and provide a suitable example? When do I need to use grep over egrep and vice versa?
4
votes
1answer
876 views

Renaming files to have lower case extensions with 'rename'

I'm trying to currently rename a large set of files and have been using quite kludgy methods to do so, such as: rename 's:(.*)\.MOV:$1.mov:g' *.MOV rename 's:(.*)\.JPG:$1.jpg:g' *.JPG What I'd ...
28
votes
4answers
13k views

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 ...
28
votes
4answers
56k views

How do I grep for multiple patterns?

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 ...
10
votes
2answers
4k views

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 ...
11
votes
4answers
2k views

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?
6
votes
1answer
522 views

Why are capital letters included in a range of lower-case letters in an awk regex?

$ echo ABC | awk '$0 ~ /^[a-b]/' ABC $ echo ABC | awk '$0 ~ /^[a-a]/' $ echo ABC | awk '$0 ~ /^a/' $ You see. /[a-b]/ captures A, but /[a-a]/ or /a/ doesn't. Why?
5
votes
1answer
121 views

What is the meaning of this test expression []? [duplicate]

I've seen someone use command: ps -ef | grep [h]ttpd and Output is: apache 25125 31006 0 21:54 ? 00:00:00 /usr/sbin/httpd apache 26869 31006 0 22:04 ? 00:00:00 ...
3
votes
3answers
2k views

In a regular expression, which characters need escaping?

In general, which characters in a regular expression need escaping? For example, the following is not syntactically correct: echo '[]' | grep '[]' grep: Unmatched [ or [^ This, however, is ...
6
votes
3answers
24k views

How to use regex with AWK for string replacement?

Suppose there is some text from a file: (bookmarks ("Chapter 1 Introduction 1" "#1" ("1.1 Problem Statement and Basic Definitions 23" "#2") ("Exercises 31" "#30") ("Notes and References 42" "#34")) ) ...
4
votes
2answers
218 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 ...
4
votes
1answer
589 views

Match word containing characters beyond a-zA-Z

To match a word one can use \v(\w+) From the vim help :h \w: \w word character: [0-9A-Za-z_] This works exactly as described in the manual. However, I want to match words that contain ...
3
votes
2answers
7k views

sed one-liner to delete everything between a pair of brackets?

I am working with some text that is full of stuff between brackets [] that I don't want. Since I can delete the brackets myself, I don't need the one-liner to do that for me, but I do need a one-liner ...
2
votes
2answers
3k views

Sed: replacing digits

I am trying to write some install scripts for when I install/upgrade Ubuntu. I'm having trouble with fixing logrotate.d: /var/log/ufw.log { rotate 7 weekly missingok notifempty compress delaycompress ...
1
vote
4answers
603 views

Replace text between brackets

I'm using awk '{ gsub(/BAR|WIBBLE/, "FOO"); print }' to replace text in data like: SOMETHING [BAR, WIBBLE] SOMETHING [BAR] This gives the desired result of: SOMETHING [FOO, FOO] SOMETHING [FOO] ...
1
vote
4answers
514 views

Stripping all vowels but the first from a set of strings

I have a string comprised of multiple substrings, separated by underscores. For example: AbcdAEfd_hEgdgE_AbAAAAA. I need to remove all vowels except the first from each substring. So: AbcdAEfd -> ...
20
votes
6answers
3k views

Regex for all 10 letter words, with unique letters

I am trying to write a regex that will display all words that are 10 characters long, and none of the letters are repeating. So far, I have got grep --colour -Eow '(\w{10})' Which is the very ...
14
votes
9answers
2k views

There must be a better way to replace single newlines only?

I am in the habit of writing one line per sentence because I typically compile things to LaTex, or am writing in some other format where line breaks get ignored. I use a blank line to indicate the ...
10
votes
4answers
34k views

Finding text between two specific characters or strings

Say I have lines like this: *[234]* *[23]* *[1453]* where * represents any string (except a string of the form [number]). How can I parse these lines with a command line utility and extract the ...
2
votes
2answers
6k views

Find all lines in a file with a certain character at a certain position

I have a lab question asking me to use grep with a regex pattern to match every word where the third letter from the beginning of the line is an "a" and save it with a redirect. How can I do that?
9
votes
2answers
660 views

What is the definition of a regular expression?

I recently got into a friendly argument with Ghoti about what constitutes a regular expression in the comments to my answer to this question. I claimed that the following is a regular expression: ...
7
votes
1answer
359 views

Why do some regex commands have opposite intepretations of '\' with various characters?

Take, for example, this command: find . -regex ".*\.\(cpp\|h\)" This will find all the .h and .cpp files in your directory. The period character '.' in regular expressions usually means "any ...
6
votes
2answers
1k views

Print specific Exif image data values with exiv2

How do I print the image Exif date with a tool like exiv2? My goal is to write the image year and month into separate variables. Do I really have to parse the output with regex or is there a ...
5
votes
1answer
177 views

Execute a command within 'sed'

I have a list of random memory addresses saved in a text file, and I need to sed them out, decode them, and save them back to my file. However, the decoding operation I'm trying to perform is handled ...
4
votes
1answer
63 views

Regex this question format into LaTeX

1. Lorem ipsun la la la? 1. Sopor 2. Stupor 3. Torpor 2. A patient has Lorem? 1. Sopor 2. Stupor 3. Somnolentia 4. La 5. Coma 3. Doesn't Response to strong external irritants is short. Tendon, ...
3
votes
1answer
1k views

Get all regex matches between two patterns and print them to file

I've got a file with a bunch of long lines. I'd like to grab every group between two patterns and print them to a new file, one match per line. I could manage to do this with Python, but I'd prefer ...
4
votes
4answers
759 views

vim search numbers containing specific number of digits

I need to find specific length numbers in a big document. I tried to use regex for this. For example, If I need to search for numbers with exactly 2 digits, I use /d/d (i.e. /d twice followed by a ...
4
votes
2answers
383 views

Find and regex

What am I doing wrong with this find expression? ; touch ook ooks ; find . -name 'ook' -or -name 'ooks' -type f ./ook ./ooks ; find . -name 'ook[s]?' -type f [returns nothing] ; echo $? 0
4
votes
1answer
765 views

Is there a tool in linux that allows multi-line regex expressions?

I'm wanting to find the results of a multi-line regular expression in linux. I tried grep, but like most linux utilities it's line based. Is there something similar that allows me to search across ...
2
votes
1answer
41 views

Context of the matching regular expression

I'm using egrep with the -o option in order to just get the matching part of the line, e.g. cat /usr/share/dict/words | egrep -o '(aa|ii)' Now I'd like to see some context of the match, i.e. a few ...
2
votes
4answers
3k views

How can I find all files in a folder that contain a match of a regular expression in the file name?

I'd like to find all of the files in my home folder on Linux (Ubuntu, in this case) that contain a match a particular regular expression. Is there a simple Unix command that I can use in order to do ...
2
votes
1answer
310 views

How to join vCards lines

vCard uses a special way to split long lines: At 75 characters, insert a DOS newline and a space. Joining therefore means to replace all occurrences of the sequence "CR, LF, space" with an empty ...
2
votes
1answer
290 views

Trouble with grep -o regex

I'm trying to use a grep command with the --only-matching flag, but it's not behaving as I would expect it to. This command: echo "1/2/3/4/5" | grep -oE "^([^/]+/){0,2}" Gives this output: 1/2/ ...
1
vote
1answer
1k views

Difficulty making a regular expression to find at least 2 occurrences of a character in a file

I have a file containing random codes. Each code has ten characters in it, and I am trying to grep codes in the file that have at least 2 occurrences of a character. I am doing this: grep DD* ...
0
votes
2answers
113 views

Regex and patterns on a ksh command line

I'm wondering what's the difference between using * and .* in a regex string. I guess, * stands for "0 to n characters" but I don't see what .* stands for. For example, what is the difference ...
0
votes
1answer
107 views

How to ask for specific parameters with grep?

I need to look in /usr/share/dict/words for a 5-letter word that starts with d or D, followed by a lowercase vowel, and ends with s. I have tried everything but to no avail.