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

learn more… | top users | synonyms (1)

1
vote
1answer
396 views

How to match group 1 in a regex followed by a 0 rather than matching group 10

In regex plugin of gedit, I use a regex to match/search and another for substitution. In the matching regex, I only have one group. In the substitution regex, I use \1 to refer to the group, and I ...
1
vote
2answers
126 views

In-place substitution for lines matching some pattern in a given set of files

Following on from my previous question: Remove slashes/parent paths from filenames inside CSS and Javascript content I arrived at the following command which replaces any filenames in these ...
4
votes
1answer
501 views

Are there zero-width assertions in sed?

Is it possible to make lookahead or lookbehind zero-width assertions in sed? I want to emulate Perl's (?=) and family. My sed is not GNU sed version 4.0.
2
votes
1answer
193 views

sed one-liner to delete word-medial spaces

I have a text file that consists of some N ORMAL E NGLISH W ORDS in all-caps. Unfortunately, these words all have a space between the first and following letters, which is not acceptable. I tried ...
3
votes
2answers
3k views

splitting textfiles according to a regular expression

By pasting into a text editor, I have a textfile gleaned from an online edition of Wilhelm's Yi Jing translation which I now want to split into 64 unequal parts, according to the 64 hexagrams of the ...
0
votes
1answer
795 views

Curl and grep html text

Is there a way to grep html text from a curl request and output grab the value that is between two html tags? More specifically the input value of a form?
5
votes
3answers
1k views

What regular expression engine type does bash use?

I use RegEx Buddy to prototype and debug my regular expressions. RegEx Buddy allows me to choose between a number of different regular expression engine types (.NET, Java, Perl, GNU BRE, GNU ERE, ...
2
votes
2answers
498 views

parsing through log file and printing out regex backreferences

I want to parse my /var/log/maillog to print out the email addresses in "from=<>" and "to=<>" (mainly to do a quick check for false positives in the DNS RBLs that I've configured) A maillog ...
23
votes
4answers
15k 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?
4
votes
2answers
8k 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 ...
6
votes
1answer
1k 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?
2
votes
3answers
763 views

sed, grep, or tr command that returns only Latin characters from a UTF-8 file

I am working with the text of the 300 tang poems, which unfortunately is a single file containing both Chinese and English. Since I am interested in 'extracting' the English, I hope to use sed, ...
12
votes
6answers
7k 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 ...
2
votes
3answers
1k views

sed one-liner to delete any line that begins with a digit

Pretend I am working with this file. The beginning of the first visible line is obscured, so ignore that. 43 is the beginning of the next line, and that line would be deleted. The next line (which ...
36
votes
4answers
19k 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 ...
12
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?
0
votes
2answers
333 views

Why does this regex find a match for \x63 with a pattern [\80-\BF]

I am trying to filter valid/invalid UTF-8 bytes, but I get strange results from the following regex (which is intended to handle a 3 byte form of UTF-8). I have intended the pattern should not ...
3
votes
2answers
179 views

Why isn't this sed regex matching?

I've recently started learning sed. I did $seq 10 | sed '/[^049]/d' I was expecting 4 9 10 as output. But I got 4 9 Where am I making mistake in understanding this regex?
2
votes
1answer
2k views

Grep - ^(\s*$) vs ^\s*$

I want to search for a field that only has spaces. When counting, this two commands gave me different results: grep "^\s*$" -E -c grep "^(\s*$)" -E -c Another thing. What is the correct way to say ...
1
vote
1answer
299 views

Easy Regex on Apache Logs

I'm struggling to create a regex for Apache logs. It's for ignoreregex on Fail2ban. I'd really like to be able to put something like /house to catch all files in that directory or ...
4
votes
2answers
786 views

substrings and regexps

I have a string contained in a variable, and I want to extract substrings based on position relative to another substring. My solution seems to work unless the string is sent to a function as an arg. ...
3
votes
2answers
610 views

ANDed conditional using regexp and variables

I want to test whether a line, read in from a file, has a specific beginning AND an ending containing a word held in a variable. Here's some code: The input file is: line one #; line two #; line ...
6
votes
3answers
10k 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
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 ...
3
votes
1answer
305 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/ ...
2
votes
3answers
755 views

Filtering paths to a specific depth

I'm using rsync and the flags -nPaAXz ~/ to check which files are going to be copied. This is far too verbose to make any sense of. How could I filter the output so I view the list of files/folders ...
3
votes
1answer
2k views

Bash regex matching not working in 4.1

Upgraded to Bash4 and found that it is not matching regexes: $ echo $BASH_VERSION 4.1.2(1)-release $ [[ "20110228" =~ "^[0-9]{8}$" ]] && echo matches But Bash 3.0 is: $ echo $BASH_VERSION ...
3
votes
2answers
2k views

sed or tr one-liner to delete all numeric digits

So, I have this textfile, and it consists of mostly alphanumeric characters. It's a standard document. But since I copied it and pasted it from a PDF, there are page numbers in there. I don't much ...
3
votes
2answers
677 views

Using grep/sed/awk to classify log file entries

I need to process a very large log file with many lines in different formats. My goal is to extract unique line entries who have the same starting pattern, e.g. '^2011-02-21.*MyKeyword.*Error', ...
7
votes
1answer
371 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 ...
14
votes
10answers
3k 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 ...
1
vote
4answers
2k views

Processing control characters

I have a log file which contains a bunch of non visible control characters such as hex \u0003. I would like to replace this using something like SED, but can't get the first part of the regex to ...
3
votes
1answer
286 views

Ex and Vi: buffers and regexes

I want to put regex matches to buffer but cannot get even simpler example y :.put myNewBuffer return msg [line] [put] [buffer] working ie how to put something to buffer? So to regular expressions ...
2
votes
2answers
392 views

lookbehind and using it with grep in Vi?

Trying to get into Vi (not Vim), after learning Vim. Vim has a lookabehind like /\(Not this\)\@<!$, how to do it in Vi? If I want to search recurvively down directory in Vim, I could do :vimgrep ...
4
votes
2answers
413 views

How to find files with names having a long string after the first dot

My Samsung Galaxy S doesn't like files where the name contains a long string after the first dot. I guess some part of the software has a too short buffer for the file extension. How can I find all ...
3
votes
4answers
2k views

Applying a regex to stdin

In programing we often see the use of Regular Expressions. One of the most common forms is: newText = text.replace( /regex/, 'replacementString' ) If stdin is text and stdout is newText, what ...
38
votes
2answers
33k 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.