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

learn more… | top users | synonyms (1)

1
vote
3answers
698 views

find with user variables

I am trying to call find with a few variables. So far I had this: DIRECTORY="./ " FILENAME=-regex" .*test.*" find $DIRECTORY $FILENAME Which works fine. If I change the filename to : ...
1
vote
4answers
610 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] ...
2
votes
2answers
464 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 ...
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 ...
1
vote
3answers
971 views

Compacting `find` name patterns

I am using find . -name '*.[cCHh][cC]' -exec grep -nHr "$1" {} ';' find . -name '*.[cCHh]' -exec grep -nHr "$1" {} ';' to search for a string in all files ending with .c, .C, .h, .H, .cc and .CC ...
1
vote
2answers
208 views

Match a simple Regex

I need to match this: (tt\d{1,10}) in a file with the file ending txt. I have tried grep and ack-grep but without results. ack-grep -G \.txt$ -g "(tt\d{1,10})"
2
votes
4answers
702 views

Change currency format to generic number using sed

I am working on a large csv file with all coma separated entries. The entire document is composed of plain numbers, except for a few columns reflecting currencies that are formatted as text. For ...
11
votes
6answers
472 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"?
4
votes
4answers
3k views

AWK - how to reduce the greed of a regular expression?

I have an example: echo "@article{gjn2010jucs, Author = {Grzegorz J. Nalepa}, " | awk '{ sub(/@.*,/,""); print }' Is it possible to write a regular expression that selects the shorter pattern? ...
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 ...
0
votes
1answer
771 views

Asking ls to only list files in a path that match a specific glob/regex

In Zsh: Is there a way to use ls to only list files and folders in the current directory that match a specific globbing or regex pattern? For example, say I have multiple files that start with the ...
2
votes
3answers
525 views

Find word in sentence with Bourne Shell (sh)

I'm trying to determine if a particular word appears in a sentence using Bourne Shell (sh). For example: #!/bin/bash INPUT='Lorem ipsum dolor sit amet.' if [[ "$INPUT" == *dolor* ]] then echo ...
2
votes
2answers
2k views

Replace the shortest match of a string pattern

I have this string: update mytable set mycol=myvalue where mycol=yourvalue; I need to convert it to: insert into mytemp select * from mytable where mycol=youvalue; I can accomplish it like this ...
1
vote
1answer
642 views

Regular expression how to get value in shell script?

I have to write a shell script to get value from a string, something like: string="abcdef" pathName How can I use grep command with the use of regular expression to get "pathName"?
2
votes
3answers
829 views

with sed, how can I replace word within a matching line?

I am trying to write a sed expression to detect lines similer to this: s0:12345:respawn:/sbin/agetty -8 -s 115200 ttyS0 linux and replace the "ttys0" with "something_else" I do not want to detect ...
4
votes
1answer
356 views

Emacs simple arithmetics in query replace

Is it possible to do the following in emacs: Query replace all numbers in a region by the number +n, for example if you have: 12 and 7 are nice numbers let n be 3, then it should transform to 15 ...
0
votes
1answer
140 views

awk extended pattern matching (embedding pattern matching in actions for already matched strings) [closed]

I want handle strings of the form: PREFIX_TYPE_N,DATA So, does the *awk (gawk, mawk, nawk) support including pattern matching in the action for already matched string? Something like this (of ...
1
vote
2answers
413 views

How to find and replace files contents that match a pattern?

Suppose we have a folder named Project. I would like find each file in the folder that contains the pattern &$ at the function call. For example should replace: AnyFunction(&$arg); with ...
2
votes
2answers
361 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 ...
1
vote
5answers
5k views

(Mac Terminal) sed to parse JSON … what am I doing wrong?

In Mac Terminal, I'm trying to parse the "name" out of this JSON response: {"created_at":"2012-08-27T20:04:27Z","deleted_at":null,"id":21462840,"item_type":"video","name":"CastingBy-v12 ...
9
votes
2answers
664 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: ...
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 ...
4
votes
1answer
130 views

Using Regex in vim movment

Is there any way to use regular expression for movement in vim. For example, I want to move my cursor to the first occurrence of the pattern abc. So can I do something analogous to fa but now a ...
3
votes
4answers
601 views

Sort input file by the results of a regex

I'd like to sort a file based on the results of a regex expression. For example, if I have the following property declarations in Obj-C @property (nonatomic, strong) id <AlbumArtDelegate, ...
4
votes
2answers
385 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
1
vote
4answers
519 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 -> ...
3
votes
2answers
265 views

Do procmail recipes have an alternator “or” operator?

Given the following procmail recipe: :0: * ^Subject:.*viagra.* * ^From:.*viagra.* /dev/null Procmail would only delete emails that were both from viagra, and had viagra in the subject. Is it ...
2
votes
1answer
178 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 ...
1
vote
1answer
54 views

cclive: what kind of regexes are allowed?

Is there a place where I can look, what kind of regexes I can use with the --tr option from cclive?
6
votes
3answers
327 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 ...
2
votes
1answer
311 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
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
538 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" ...
0
votes
2answers
207 views

Difference in regular expression handling between Perl, Kikki, and Kodos

#!/usr/bin/perl $mystring = "[2004/04/13] The date of this article."; if($mystring =~ m/(\d+)/) { print "The first number is $1."; } Perl returns 2004, but Kiki and Kodos return /04/. Why ...
1
vote
2answers
548 views

Weird zsh grep -P behaviour

I connect to server with PuTTY from Windows. I have a long yet not touched (I suppose) by hands of local admin config: $ cat /opt/jira/.subversion/config ... ### must be enabled, which is ...
13
votes
5answers
826 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). ...
0
votes
1answer
115 views

Replacement in VIM with regular expression matched part unchanged

Suppose I want to append two digit numbers in my file with "some_thing". Like from this: 12 23 45 to 12_something 23_something 45_something How can I do this using regular expression ...
2
votes
2answers
362 views

Detect pattern repetition in a file

I want to make sure a file follows the following pattern: ... ... ... foo ... foo_KO ... ... ... bar ... ... ... ... bar_KO ... The file is extremely long, so, in other words, I want to make sure ...
1
vote
2answers
392 views

How do I generate a list of the characters occurring before a certain character only in lines that contain another certain character using regex?

I know that's super confusing (total noob-sorry!). To clarify, I have a text file that looks like this: 407-OL? 408-2-OL? 408-OL? 418-het? 420-1 and 2- OL? 429-2-left unscored? 430-2-left both ...
4
votes
1answer
331 views

match files based on an md5 using find

I have a bunch js and css files in a directory. Some of them have been given a name based on an md5() of their content (looks like f10521a21bb013cb81e0909809818ad6.js). I'd like to match these files ...
4
votes
1answer
2k views

escaping a single dot with double backslash - awk

"effective awk programming" book has an example on Field-Splitting. here is the example: If you want fields to be separated by a literal period followed by any single character, use ‘FS = ...
2
votes
1answer
270 views

Why doesnt ctrl+d work with this?

I wanted a simple way to process text in my clipboard without having to create a file. I tried using the following line: awk '{print $1}' <(cat) but I couldn't send cat the EOF character using ...
1
vote
3answers
2k views

sed pattern matching

I recently asked someone at work about how to take the output of ipcs -qa and make it space delimited, so I can parse it/store it in the database for monitoring. He gave me this: ipcs -qa | sed 's/ [ ...
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 ... ...
5
votes
4answers
2k 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/ ...
29
votes
4answers
57k 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 ...
4
votes
1answer
256 views

substituting third occurrence of a pattern using sed

Here is a sed command. I want to substitute third occurrence of 00 to 11 echo "0000000000000000" | sed -e 's/00/11/3' The output is: 0000110000000000 But I thought output would be: ...
2
votes
2answers
1k views

regex replace text in xml file within node from the command line

I have an XML file and I would like to replace everything that is between the open and closing tag within multiple instances of the g:gtin node with nothing. Is this possible from the command line, ...
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 ...