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

learn more… | top users | synonyms (1)

47
votes
4answers
17k 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?
33
votes
2answers
24k views

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.
24
votes
4answers
9k 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 ...
24
votes
1answer
858 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 ...
21
votes
4answers
39k 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 ...
20
votes
6answers
2k 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 ...
17
votes
4answers
8k views

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?
15
votes
3answers
9k views

How to diff files ignoring comments (lines starting with #)?

I've two configuration files, the original from the package manager and a customized one modified by myself. I've added some comments to describe behavior. How can I run diff on the configuration ...
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 ...
13
votes
5answers
743 views

How to remove the (1) from filenames using the find command

I recently converted all of my FLAC files to a lower sampling rate of 44.1 kHz and bit depth of 24 bits (because iPhone/iPod don't support anything above that) using XLD on my Mac OS 10.7 (Lion). ...
12
votes
6answers
2k views

Software for visually building regular expressions?

I frequently need to create regular expressions for use in scripts (mostly when used in sed), however, I find, sometimes the regular expressions get very complicated and difficult for me to understand ...
11
votes
6answers
417 views

How can I grep a directory based on the contents of two successive lines?

How can I grep a directory for lines that contain "Foo", but only get matches when the next line also contains "Bar"?
11
votes
4answers
741 views

How do I choose specific files in a different directory using bash?

I want to list (or delete, or do some other operation) on certain files in a directory, like this: $ ls /opt/somedir/ aa bb cc aa.txt bb.txt cc.txt $ ls /opt/somedir/(aa|bb|cc) ## pseudo-bash ...
11
votes
3answers
2k views

Online grep exercises?

Are there any online grep tutorials that would feature some online tool for performing exercises? For example, having some predefined text sample and being told what patterns to find and so forth? It ...
10
votes
6answers
4k views

How can you combine all lines that end with a backslash character?

Using a common command line tool like sed or awk, is it possible to join all lines that end with a given character, like a backslash? For example, given the file: foo bar \ bash \ baz dude \ happy ...
10
votes
2answers
2k 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 ...
9
votes
2answers
618 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: ...
9
votes
6answers
747 views

Text Manipulation Across multiple lines

I Have a file that has text like this: AAAA BBBB CCCC DDDD 1234 5678 9012 3456 EEEE 7890 etc... And i want to match up the Alphabetic lines with the Numeric lines so they are like this: ...
9
votes
4answers
26k 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 ...
9
votes
2answers
2k views

Regular expression in bash script

This is my first time bash scripting so I'm probably making an easy mistake. Basically, I'm trying to write a script that gets the groups of a user, and if they are in a certain group, it will log ...
8
votes
6answers
1k views

Delete lines beginning with #

How do I delete lines beginning with a #, given that there can be whitespace on the left and right of the #? # Master socket provides access to userdb information. It's typically
8
votes
4answers
11k views

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

I've noticed that, if I add \n to a patter for a substitute 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 ...
8
votes
4answers
1k 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?
7
votes
3answers
5k views

grep: Trailing backslash

I want to do a grep for \resources\. How do I do this? I've tried: grep \resources\ grep \\resources\\ grep "\resources\" grep "\\resources\\" None of these work.
7
votes
1answer
350 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
3answers
5k views

What does \? mean in a regular expression?

The following command is used to search for a 7-digit phone number: grep "[[:digit:]]\{3\}[ -]\?[[:digit:]]\{4\}" file What does \? stand for?
6
votes
2answers
2k views

How to use regex as field separator in awk?

I'm trying to use regex as a field seperator in awk. From my reading this seems possible but I can't get the syntax right. rpm -qa | awk '{ 'FS == [0-9]' ; print $1 }' awk: cmd. line:1: { FS awk: ...
6
votes
4answers
808 views

How can I “grep” patterns across multiple lines?

It seems I am misusing grep/egrep. I was trying to search for strings in multiple line and could not find a match while I know that what I'm looking for should match. Originally I thought that my ...
6
votes
3answers
21k 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")) ) ...
6
votes
2answers
186 views

Why two almost idetical grep commands return different output: w/o and with filename

I have 2 almost identical greps: [Alex@localhost tmp]$ grep /bin/bash /etc/passwd root:x:0:0:root:/root:/bin/bash AlexL:x:500:500::/home/AlexL:/bin/bash user1:x:501:501:user1 ...
6
votes
1answer
3k views

What is the .gitignore pattern equivalent of the regex (Big|Small)(State|City)-[0-9]*\.csv

I have a regex I stuck in my .gitignore similar to: (Big|Small)(State|City)-[0-9]*\.csv It didn't work so I tested it against RegexLab.NET. I then found the gitignore man page which led me to ...
6
votes
1answer
783 views

Why isn't sed greedy in this simple case?

$echo "foo 65 bar" | sed -n -e 's/.*\([0-9]\+\).*/\1/p' 5 Why is the output not 65? Shouldn't sed greedily match the [0-9]\+ part? How do I tell sed to match all of 65?
6
votes
1answer
472 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?
6
votes
1answer
507 views

How do you save a complex regex for multiple reuse in sed?

In using sed, I often create rather complicated and intricate regexes that I need to match twice in a file. Is there a way for me to save this regex and just reference it twice? Maybe something that ...
5
votes
6answers
391 views

Replace a character except last x occurences

I have a file that has a bunch of hostnames correlated with IPs that looks like this: x-cluster-front-1 192.168.1.2 x-cluster-front-2 192.158.1.10 y-cluster-back-1 10.1.11.99 y-cluster-back-2 ...
5
votes
3answers
316 views

difference between .* and * in regular Expression

I've a file named "test" that contains linux Unixlinux Linuxunix it's linux l...x now when i use grep '\<l.*x\>' , it matches : linux it's linux l...x but when i use grep '\<l*x\>' ...
5
votes
1answer
3k views

Substitute text with sed and keep part of the original text

I am trying to convert <id>1</id> <Name>ENTERPRISE RESOURCE PLANNING</Name> to: <column name="id">1</column> <column name="Name">ENTERPRISE RESOURCE ...
5
votes
2answers
1k 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 ...
5
votes
1answer
152 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 ...
5
votes
4answers
1k views

Recursively rename subdirectories that match a regex

I have a media server with a folder called Series. (/media/Expansion2/Series/) In it, I have (surprise!) TV series. These are just the show names, e.g., /media/Expansion2/Series/The Big Bang Theory/ ...
5
votes
1answer
4k views

grep for words of no more than a certain length

I'm looking for a way to grep things like: i log for E M, i 1 f x i 0, I xi 1, 3 1,. simply based on number of characters. Nothing in that hypothetical output is longer than three characters. This ...
5
votes
2answers
483 views

exact match in regrex when using vim, man, or less

when using vim, man, or less, I want to do some exact match in regrex for example, when using man, I want to check the argument '-c' if I use /'-c' the matching could be -cim ...
5
votes
3answers
290 views

VIM see regular expressions matches as you type

If I type /regex then the cursor temporarily jumps to the first match and all matches are highlighted. This updates as I type. Is it possible to get this behavior when I'm getting ready to make a ...
5
votes
3answers
966 views

Regex & Sed/Perl: Match word that ISN'T preceded by another word

I'd like to use sed or perl to replace all occurrences of a word that doesn't have a certain word in front of it. For example, I have a text file that contains a plot of a movie and I want to replace ...
5
votes
3answers
229 views

vim regex not need \ to escape

In Vim is there an option to write regexs in the same style as Awk for example/sp\{0,\}/ Would be /sp{0,}/
5
votes
1answer
111 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 ...
5
votes
2answers
714 views

Regexp in .ssh/config

At work, I often have to log into hosts which follow a common naming scheme, e.g. qc01 - qc12, hc01 - hc10, and so on. All of these need to get the same config values in my laptop's .ssh/config. Of ...
5
votes
3answers
93 views

The ERE regex to split() string between a delimiter and end-of-word

I'm using a long gawk 3.1.6 script to do a complex conversion of Zim markdown text into GtkDialog code and am stuck on the following problem... Sample ASCII input... [[link|label label]] [[link]] ...
5
votes
2answers
174 views

Lowercase all but the first (Uppercase) letter from UPPERCASE in cyrillic

To make all letters a lower case except the first letter. The first letter would look like "Uppercase" after I changed (from UPPERCASE in cyrillic). The rest (not UPPERCASED) leave unchanged. I'm ...
5
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 ...