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

learn more… | top users | synonyms (1)

7
votes
1answer
169 views

Grep search returning a different line

This is my grep search grep 'Invoker_Slark*' true_pairscore.txt But it returns the line Invoker_Slardar. Even though the file contains Invoker_Slark. Why is that?
0
votes
1answer
171 views

Want to replace the words with space in multiple files using SED commmand [closed]

Want to replace the words with space in multiple files using SED commmand I have a word as 'Danny' with single quotes in many of my files in multiple directories which I want to change to 'Danny ...
3
votes
1answer
86 views

Merging text files based on their filename

I have hundred of files with the following format: 2013_01_19_xyxyxyx.txt 2013_01_19_xyxyxyx.txt 2014_03_21_xyxyxyx.txt 2014_03_21_zxzxzxz.txt 2014_03_22_abababa.txt 2014_03_22_cbcbcbc.txt etc. I ...
3
votes
2answers
119 views

Replacing 3 or more digits with equivalent number of *

I want to replace any 3 or more digit string in a text file with equivalent number of *.For example: abc-1234-45 --> abc-****-45 echo "abc-1234-45" | sed 's/[0-9]\{3,\}/*/' I tried this but it only ...
0
votes
1answer
491 views

grep: match pattern on a particular position

I need to match a pattern with grep, only if it starts at position 16. Lets suppose I want to match the string ' pattern' (starting with space) the following should match bla bla bla bla pattern ...
2
votes
1answer
252 views

How to print strings matching a pattern with grep?

I want to get a list of strings matching the following pattern: CLASS_NAME:"*" where * represents any number of characters. I tried: grep -o CLASS_NAME:\".*\" script.js and grep -o ...
0
votes
5answers
612 views

bash event not found trying to match and exclude parenthesis in grep

In a very long line I'll summarize with: (foo),(bar,baz(word,right),(end) I want to print only: (bar,baz(word,right To match the second parenthesis, I exclude the word that follows the ...
3
votes
3answers
612 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. ...
4
votes
4answers
4k 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
63 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
6k 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
220 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
836 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
310 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
329 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
53 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
177 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
248 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
288 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
4k 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
47 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
48 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
188 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
151 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
1k 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
206 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
28 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
120 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
62 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
254 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
217 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
76 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
121 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
333 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
8k 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
145 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
3k 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
343 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
746 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
78 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
82 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
830 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
424 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
176 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
32 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
93 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
237 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
181 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]] ...