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

learn more… | top users | synonyms (1)

2
votes
2answers
99 views

Trouble getting regex to work with find

I'm having a lot of trouble getting find to work with regex flags. Im using egrep on its own to find a date pattern in a file, like this: egrep '[0-9]{4}-[0-9]{2}-[0-9]{2}' which will return dates ...
2
votes
2answers
132 views

regular expressions with grep

I have this command right here: grep -B10 -A10 '14:14:50 {"channels":["/alerts/6979/new"],"data":"New alerts for unit 6979"}' mygateway.log Rather than searching for 14:14:50 I would like the 50 to ...
2
votes
1answer
178 views

grep all words with specified length, without letter x

So I am stuck here: grep -E '^....c...c...$' shows all words with the expr I need. Now from this words I only need the ones that don't have a x. Who can help me?
2
votes
3answers
2k views

remove white space before delimiter with sed

I have data of the following format that I want to input into LibreOffice calc data | num | num | num | num For some reason Libreoffice does not think the string "3214 " is a number by ...
2
votes
2answers
372 views

AWK match string, not Regex

How do I match a string with AWK? Say I have the string [(*.+ it should only match strings containing the string [(*.+. The string is entered by the user, so if you know a cli utility that can ...
2
votes
4answers
532 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 -> ...
2
votes
1answer
246 views

Delete repeated words between brackets inline

Our input looks something like 2012-04-17 [GBPGBP] 2012-04-13 [GBP GBP] 2012-04-13 [GBP] 2012-04-11 [GBPGBP] 2012-04-11 [GBP GBP] 2012-04-10 [GBPGBP] 2012-04-06 [GBP GBP GBP] 2012-04-17 ...
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?
2
votes
3answers
136 views

expr help - managing strings

I need to take text like this: A234321=http://www.google..... a normal URL And pull out only the URL, getting rid of the first part. I think I can use expr to do it, but I can't figure out the ...
2
votes
2answers
1k views

Regex : match 2nd and 3rd character

I was doing some exercises on regular expressions, but I can't seem to be able to crack this one : egrep in a file where the 2nd and 3rd character are the same. I tried : egrep '^..{2}' ...
2
votes
1answer
179 views

Awk regular expression syntax with number of repetition - different handling between gawk 3 and gawk 4

I am having trouble understanding or explaining why the following expression fails in gawk 3.1.x and yet works in gawk 4.1.x: (Minimum working example) echo ";#ADCDE#" | awk '/#.{5}#$/' -> produces ...
2
votes
1answer
105 views

Search for “not newline” in vim?

To search for A T\n in vim I can just do /A T\n What would be the equivalent for not newline at the end? /A T[^\n] did not work.
2
votes
2answers
214 views

Print some fields of /etc/passwd

I have a question concerning the usage of wild cards in Linux. Here I have some lines from a long list of lines in the /etc/passwd -file. root:x:0:0:root:/root:/bin/bash ...
2
votes
3answers
154 views

mv files to a different path which is constructed via a regex replacement

I have a directory full of files stored in subdirectories according to their filename, i.e.: 20d1/d325/52d1/20d1d32552d1a95249e62662fbdf924dd72c4027.jpg ...
2
votes
1answer
1k views

Forcing Bash to use Perl RegEx Engine

As you may already know, a lot of the features modern RegEx engines support (back referencing, lookaround assertions, etc.) are not supported by Bash RegEx engine. Following is a simple Bash script I ...
2
votes
3answers
477 views

How to reverse-match a string in the Vim programming language?

I want to find the last index of any character in the [abc] set in the abcabc string but the search should start from the end of the string: " Returns the 0th index but I want the 5th. let ...
2
votes
1answer
3k views

sed special character replace not working in shell script

I want to write a script 'test.sh' that will take a user's input and replace all special characters with a '\' + the character. My script: #!/bin/bash echo 'input='"$1" arg=`echo "$1" | sed ...
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
182 views

prepend digit to filenames which begin with digits (with zmv, please)

I have a directory with files named like 19.png, 20.jpeg, and README00.txt I want to rename these to 119.png, 120.jpeg, and README00.txt. I would like to do this with zmv as it works well in so many ...
2
votes
1answer
315 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
2answers
1k views

Sort by regular expressions

I've got a set of POSIX regular expressions* ^BEGIN:VCARD\r$ ^VERSION[^A-Z] ^FN[^A-Z] ^N[^A-Z] ^NICKNAME[^A-Z] ^EMAIL[^A-Z] ^X-\([A-Z-]*\) ^TEL[^A-Z] ^ADR[^A-Z] ^ORG[^A-Z] ^TITLE[^A-Z] ^BDAY[^A-Z] ...
2
votes
1answer
552 views

Emacs: query-replace, regular expression, reuse of search result

How do I combine something like query-replace with a regular expression search-and-replace that in part reuses the search result? In my case, how to transform The programmers are "sort of" ...
2
votes
2answers
1k views

How do I transfer multiple files with a common suffix and prefix using an offset?

I have thousands of files whose names are of the form: sequence_1_0001.hmf sequence_1_0002.hmf sequence_1_0003.hmf ... sequence_1_0001.jpg sequence_1_0002.jpg sequence_1_0003.jpg ... ...
2
votes
1answer
125 views

Does find support OR in its regexes?

For example, I want to find all files in a directory that end in 'm' or 'sh'. The following works correctly: find . -regex '.*.m' -o -regex '.*.sh' But this fails: find . -regex '.*.(m|sh)' What ...
2
votes
2answers
1k views

How to treat a file as a single line with grep to apply a regexp search pattern?

I want to match everything that is between some lines with regexp but not this that match the start and the end. This sound to me as positive lookbehind and positive lookahead start text bla ...
2
votes
1answer
495 views

Regex : how to verify that there are 13 greek letters with an odd number of consonants

Another regex that I can't seem to crack :( I tried with egrep '([qwrtzpsdfghjklxcvbnmy]{1})|([qwrtzpsdfghjklxcvbnmy]{3})|([qwrtzpsdfghjklxcvbnmy]{5})|([qwrtzpsdfghjklxcvbnmy]{7})' greek.txt ...
2
votes
2answers
1k views

Arithmetic operations in regex

I am using gedit regex plugin (Python style regex). I would like to do some arithmetic operation on a backreference to a group. For example: PART 1 DATA MODELS Chapter 2 Entity-Relationship Model ...
2
votes
1answer
293 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
4answers
49 views

Matching string with a fixed number of characters using grep

I am trying to find all 6 letter words using grep. I currently have this: grep "^.\{6\}$" myfile.txt However, I am finding that I am also getting results such as: étuis, étude. I suspect it has ...
2
votes
1answer
76 views

Removing non-printable characters using POSIX sed

Files created with roff and other "old-school" tools (for example man pages on many Unix systems) generate bold and underlined text in minimalistic terminals using tricks involving non-printable ASCII ...
2
votes
1answer
42 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
3answers
52 views

A challenge for sed convert code from Mathematica to Matlab

I have the following output from Mathematica to C code: $ cat file.txt Complex(1.3, 2.7)*Power(kappa, 2.0) + Complex(0.3, 2.0)*Power(kappa,5.0)*log(alpha) The file is extremely long (many tens of ...
2
votes
1answer
76 views

csplit not obeying backslash r \r as carriage return

I have a file with \r\n line endings (CRLF carriage return line feed). This command works: $ csplit --silent --prefix=email-emailbad.txt- \ --digits=3 emailbad.txt '/^\.^M/'+1 '{*}' This ...
2
votes
1answer
66 views

using grep --perl-regexp

this is a sample output of ls $ ls -lAF -rw------- 1 martin martin 168 Jan 25 23:26 aaa -rw------- 1 martin martin 168 Jan 25 23:26 bbb -rw------- 1 martin martin 168 Jan 25 23:26 ccc I would ...
2
votes
2answers
1k views

How to do a regex search in a UTF-16LE file while in a UTF-8 locale?

EDIT: Due to a comment Warren Young made, it made me realize that I was not clear on one quite relevant point. My search string is already in UTF-16LE order (not in Unicode Codepoint order, which is ...
2
votes
1answer
422 views

Expand regex on multiple lines in Vim

I am working on removing comments from a C source file. Let's focus on multiline comments /* ... */ and ignore the inline ones (//) The following command seems to work with (solaris) sed: ...
2
votes
3answers
855 views

How to cut the output to only gather the filename and get-parameter?

I have multiple files with multiple links that are formatted like this: <a href="http://example.com/fnord.layername.html?parameter=FOO-_-BAR-_-FNORD" class="poit"> <img ... /> ...
2
votes
3answers
640 views

regexp in ksh for extensions tgz, tar.tgz

I'm trying to get a regexp (in ksh) to identify files with only the following extensions: tgz, tar.gz, TGZ and TAR.GZ. I tried several but can't get them to work. I'm using this regexp to select only ...
2
votes
2answers
458 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 ...
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 ...
2
votes
3answers
42 views

bash: regular expressions in if expression

In my script on my ubuntu machine I declare a string like this: DEBUG_PACKAGE_LIST=$(apt-cache search dbg | awk '{ print $1 }' | grep -e "-dbg") For you to help you understand my problem here is ...
2
votes
1answer
49 views

How to match a particular form of optional sequence from a manual synopsis, including variations?

In this Q&A there is a reference to the manpages synopses being based "loosely" on the Extended Backus–Naur Form of metasyntax notation. It's interesting and serves as background. That being said, ...
2
votes
1answer
45 views

How to perform a multiline grep across multiple files?

I'm trying to grab this pattern wherever it occurs in multiple log files (Note:these patterns can vary greatly in size, i.e. the amount of Blahs): Found an txt File Blah Blah 10019874 ...
2
votes
2answers
41 views

procmail recipe with a regex doesn't work

In my ~/.procmailrc I have the following lines: :0 \* ^Subject:.*(O|o)pen( \|)(S|s)(onic|urge) $MAILDIR/OpenSurge/ As you see, I would like to put any mail containing “Open Surge” (or any variant ...
2
votes
1answer
97 views

Using regular experision in “rpl” command

I need to replace text in lots of files. I'm using rpl for this purpose, but I can't find any way to make it use regular expressions. Can rpl use "regex"? how? Edit: I'm using rpl 1.5.2 from Ubuntu ...
2
votes
1answer
948 views

Using BASH versus ASH on Synology

The majority of my photo files have the following naming convention / format IMG_20131204_120000.JPG, showing date and time the photo was taken (iPhone/Apple naming convention). However, not all my ...
2
votes
1answer
149 views

Regex to find NFS filesystems that aren't being mounted with nosuid, except for ones with special needs

I have a script that runs the command mount -v 2>/some/file, which I then parse with another script using the following regex: ^.*[\ \t]+type[\ \t]+nfs(?![\ \t]+.*\b(?:nosuid|nosetuid)\b).* The ...
2
votes
4answers
94 views

Alternative regex for {}

I am trying to replace all of this line, but the numbers: looktype="123" so only the numbers show up. Is this possible in any easy way? {sub ("look type=\"[0-9]{0,3}", "TEST")} I am trying this ...
2
votes
1answer
136 views

How to negate a regex inside another regex

That is, given a POSIX regex X, what POSIX regex is the complementary of X? (in a more detailed way: how to get regex X' that matches all strings that are not matched by X, but doesn't match any ...
2
votes
2answers
470 views

Parse apache logs to exclude IP ranges

I need to look for some requests in a huge pile of apache logs. My only requirement is this: I need to only view the requests coming from any IP address that is NOT included in a list of 50 IP ranges ...