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

learn more… | top users | synonyms (1)

3
votes
3answers
709 views

Regex not matching

why this does not match..? sed -e '/--Updated?[[:space:]]+Date/d' inputfile this..: --Updated Date: 2013-11-06 15:32:13 d? is because sometimes I have Update Date, and sometimes Updated Date. ...
5
votes
4answers
6k views

Grep for range of numbers [duplicate]

How would you search a log file for a date range? Log file looks like this: 01/14 00:00:01 INFO: received connect request from 10.10.10.10 I need to condense the log to 10 minutes starting ...
26
votes
1answer
1k 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 ...
0
votes
3answers
41 views

Grep lines from foe to foe2 (not included), regexp

The file is organized in entries of the form: foe, 1.... 2.... 3.... foe2, 1.... 2.... All sub-items begin with four spaces, and then text or number item. All entries like foe, begin ...
0
votes
1answer
64 views

How can I use gsub with a pattern? And how to tell it which file to awk?

I've read here that I can replace the values of all columns containing a particular text using awk -F'\t' -vOFS='\t' '{ gsub("CC", "C", $1) ; gsub("AA", "A", $1) ; print }' But where do I tell awk ...
3
votes
3answers
8k views

Why can't find -regex match a newline?

Why does this fail? touch "$(printf "a\nb")"; find . -regex './.\n.' I also tried these, none of which work: find . -regextype posix-extended -regex '.\n.' find . -regextype posix-awk -regex ...
3
votes
2answers
243 views

How to use restricted characters in egrep?

I'm trying to use a regex in egrep on Centos 5.10, based on the one I made from this question on SO. T12345 .{1,100}[\n\r].{1,100}2014.03.04 I can successfully match the regex to the content in my ...
3
votes
3answers
1k views

How can I extract/parse a complete URL from a semi random string?

I'd like to have bash parse/extract a full URL (and only the url) from a random short string. Examples: bob, the address is http://www.google.com or https://foo.com/category/example.html is up ...
1
vote
1answer
350 views

awk date identification from input file

I currently have daily files that come in via FTP with incorrect dates in the first column of the file. I have figured out how to deduct one day to derive the correct date and print this to a new ...
1
vote
3answers
472 views

What does the dot in regex “^[A-Za-z]{2}[1-9]{2}.*” do?

I am very new with regexp. I would like to know the pattern defined by this regexp : ^[A-Za-z]{2}[1-9]{2}.* It starts with 2 alphabetic characters then 2 numeric characters then a . and * which is ...
2
votes
2answers
73 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 ...
1
vote
4answers
269 views

regex matching with “locate”

I'm trying to use the locate command to find files in my home folder, however whenever I try and run this I get no results: locate -i -l 4 --regexp '^\/home\/jack\/[A-Za-z0-9\/\ ]*(My.)*$' I've ...
0
votes
3answers
308 views

How can I use grep to search a file for string combinations?

What I'm trying to do is use grep to search my Apache logs. What I want is to find is every entry from a certain IP address that also has .html or .php in the line. In other words, I want to see what ...
2
votes
1answer
345 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 ...
20
votes
6answers
5k 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 ...
1
vote
2answers
58 views

How can I append multiple extensions to a string with sed?

I want to create a script that will let me download images from entire subreddits at once, but I'm currently stuck on just getting the links right (one problem at a time! ^^). Anyway, since I can't ...
0
votes
1answer
52 views

Piped search queries in vim or less

I would like to be able to search a log file for lines that being with ^2014-02-18 15:30:[0-9:,]* and have UserName:someguy much later in the same line. I am sure I could do a regex to include any ...
6
votes
2answers
201 views

Regex `/pattern/g` and ed `:g/pattern/`: which came first, and why `g`?

The g option (e.g. s/pattern/replacement/g) for many tools that use regex-style pattern matching and the :g command in ed, ex, vi, and vim have pretty similar usage and meaning: match the given regex ...
2
votes
1answer
204 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 ...
4
votes
4answers
2k views

Print line after nth occurrence of a match

I am looking to display the line 4598 in the following file. Effectively I want to display the line AFTER the nth occurrence of a match. In this case, the line after the 3rd occurrence of <Car>. ...
2
votes
1answer
224 views

Counting consecutive repetitions of a specific character (e.g. ,)

Say I have a file with lines like the two following: foo,bar,,baz,xy foo,,bar,baz,xy,, I would like to count how many times I have ,, (two consecutive commas surrounded by any other character) in ...
0
votes
1answer
30 views

Finding all kinds of extensions referenced in a html file

Here is my problem statement : There is a folder with many html and text files. I need to recursively go through each one of them and find all kinds of file extensions referenced in these html/text ...
1
vote
1answer
173 views

Why isn't sed using the extended regex mode by default?

I am using sed. I was using a regex that was correct as far as I could see but sed did not do anything. Turns out that I was using \s+ which sed can not understand (?) and when I switched to [ ]+ it ...
2
votes
2answers
90 views

Why don't I need to escape a character class in sed but I need to escape the rest?

I am confused with sed. I had a file with the following contents: [ 2014/02/10 14:45:15 | INFO ] Saving data for customer: 273667 [ 2014/02/10 14:45:15 | INFO ] Saving data for customer: ...
2
votes
2answers
392 views

SED change only start of line and preserve end of line

Basically I have a situation where I frequently need to modify my hosts file to access different servers at my workplace. In an attempt to automate the process. I've been trying to use sed to make the ...
1
vote
1answer
264 views

How to get the JSON STRING from the given string

I have a string given below. I just need to get the first accountHeader (bolded) JSON string, I tried everything.. It didn't work. Can you help me to get the data? <START ...
0
votes
1answer
87 views

All occurrences of a regex get removed without using the “global” modifier

I have the following text in the file sentence.txt Linux refers to the family of Unix-like computer operating systems that use the Linux kernel. Linux can be installed on a wide variety of ...
2
votes
1answer
137 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.
1
vote
2answers
377 views

Find duplicate file names with specific matching pattern

I have a folder with some files (snippet of the contents of the folder) PAT1.URGRSVP.50.WR786842JOB11632.WRS20140.FILE0005.DAT PAT1.URGRSVP.50.WR786842JOB11643.WRS20140.FILE0003.DAT ...
11
votes
4answers
11k 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 ...
4
votes
1answer
149 views

Why does grep treat '[D]ebug' string differently?

I've created simple text file named "T" to test the unusual behavior of grep: 1 Debug 2 debug 3 determined 4 Determined Tried different syntax: $ grep De T 1 Debug ...
1
vote
4answers
4k views

Regular expression in Bash to validate IP-address

In my Python script i have a regular expression for searching IP-addresses like 0.0.0.0-255.255.255.255 in a file, it looks like this: ...
0
votes
1answer
468 views

How do I join the next line when a line matches a regex for whole document in VI?

As title, for example, I have lines like abcdefg join! abcdef no join abcdefg join! If a line matches regex ^a.+g$ then I would like them to be joined, so it would be: abcdefgjoin! abcdef no join ...
5
votes
6answers
849 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 ...
1
vote
2answers
97 views

How does “$” work in “apt-cache search something$”

I understand that $ apt-cache search package_name will search for matches in names and descriptions. But how does this work? $ apt-cache search package_name$ For example, $ apt-cache search ...
1
vote
1answer
95 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 ...
5
votes
1answer
926 views

Using Modifiers of Perl-compatible Regex (PCRE) in grep

According to grep --help and man grep, we can use the -P option in order to interpret the pattern as a Perl regular expression (PCRE, to be precise), instead of the default POSIX basic regular ...
1
vote
3answers
584 views

How to get numbers by using regular expression, but only last one

In this example I can get 00. expr match "00 foo 99.jpg" '[^0-9]*\([0-9]\+\).*$' I want to know how to get last number from string, in this case 99 by using regular expression. The string may ...
3
votes
1answer
192 views

Expanding comma-separated list into separate lines

I have a file with some data in this form: Prefix text: First Name, Second Name, Third-- The prefix differs by line. The number of names varies from one to several. The suffix (-- in the example) ...
1
vote
1answer
36 views

Where can I find documentation on shell script syntax for using a regex within a variable construct?

I'm debugging a shell script (which I didn't write) that contains this loop: read line < "$pid_file" for p in $line ; do [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid="$pid $p" done ...
4
votes
2answers
106 views

Why sed prints first string from file even if it's failed to find matching groups

I'm trying to write sed expression which will extract matching groups from file. E.g. I have .cue sheet and I want to extract it's genre tag. It has following format: $ cat file.cue | grep GENRE REM ...
1
vote
2answers
317 views

Why is my grep + regex not working?

I recently decided that enough was enough -- I was going to learn to use grep fluently. It's been all of three hours and I'm already stumped by this toy problem. I'm currently syncing a RAID5 array, ...
5
votes
3answers
208 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]] ...
-1
votes
1answer
267 views

extracting ip address from a file [closed]

I'm trying to extract all the IP address from a file. FILE *>i85.95.2.160/29 172.16.24.4 0 200 0 64515 4445 15412 i *>i85.112.80.0/28 172.16.24.4 0 200 ...
1
vote
1answer
161 views

Parsing csv with sed and perl one-liner?

I have a web server which stores configurations to PostgreSQL, and I plan to retrieve information from a certain table and then later parse it into csv format. PAYLOAD <?xml version="1.0" ...
2
votes
2answers
2k views

Using a variable in a regex

How can I use a variable string in a regular expressions while read line do if [[ line =~ *"$key"* ]]; then echo line fi done < "$filename" $key is a string ...
1
vote
2answers
1k views

Differing outputs of the commands “ls .*” and “ls *.”

The command ls .* when run gives as output the following : All the files in the current directory starting with a . (hidden files) All the files in the hidden directories present in the ...
1
vote
2answers
52 views

use search result in replace statement with vim substitute

I know this is gonna be a stupid question, but I've tried here, google and the official documentation. I've got a file with a whole heap of words of the form 'foo' and i want to change them to ...
0
votes
3answers
271 views

Ignore lines containing a capital letter

Why does the following command still return some lines containing a capital letter? aspell dump master | grep ozz | grep -v '[^A-Z]'
2
votes
2answers
183 views

Understanding -regex with GNU find

Background I have what I think should be a simple case. I want to find all files with "cisco" in the name and do something with those files (via xargs). Finding files with ls Before I use xargs, ...