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

learn more… | top users | synonyms (1)

3
votes
2answers
589 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 ...
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
3answers
142 views

Replacing a pattern in line preceding another pattern of interest

I have a log file with many instances of statements as shown below: 14 Aug 19 16:30:11.506: <DATA> |POS|IDLE|1|01131844090|5950|$hostIp|$size |$data 20 Aug 19 16:30:12.439: <DATA> ...
3
votes
3answers
310 views

Replacing string in all files found by grep. Can't get it to work

I'm trying to do what title says. So I find all needed files with grep: grep --include=\*.{php,ini,conf,sh} -ril -P "'([\d\w\-\_\.]+)(@domain.com)'" '/var/www_data/somepath/' Now I assume it's ...
3
votes
1answer
234 views

Why doesn't this regex work?

I want to look at the output of git status -s to determine whether there are any untracked files in the tree. I tried the following test, but the regex doesn't match. Why not? $ git status -s ## ...
3
votes
1answer
83 views

find utility does not recognize bracket notation

I've two files in the current folder: submitWeb.m submit.m the following find does not show the two files: find . -regex .*submit\(Web\)?\.m But the above regex expression works fine in ...
3
votes
2answers
149 views

How to add a line in many files

I have many .html files and I need to add a meta tag after <head>'s start tag in each file. How I can do that? Can vim help me?
3
votes
1answer
7k views

grep and escaping a dollar sign

I want to know which files have the string $Id$. grep \$Id\$ my_dir/mylist_of_files returns 0 occurrences. I discovered that I have to use grep \$Id$ my_dir/mylist_of_files Then I see that the ...
3
votes
2answers
174 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?
3
votes
1answer
300 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/ ...
3
votes
2answers
48 views

Extracting just a single group from a regular expression

I am trying to get a list of numbers from some hacked-together log files using zgrep. So, I am using: zgrep -ie "total time: .* ms" * This returns the whole line that I care about. I'd just like ...
3
votes
1answer
2k views

Get all regex matches between two patterns and print them to file

I've got a file with a bunch of long lines. I'd like to grab every group between two patterns and print them to a new file, one match per line. I could manage to do this with Python, but I'd prefer ...
3
votes
3answers
192 views

What does . match?

In working with regular expressions, I have been told that a dot character . will match everything. Except for newlines \n. Are there any other exceptions? What about the NUL character \0, or the ...
3
votes
1answer
52 views

Using sed to replace only the first two regex catches with different strings

So I'm trying to change two instances of a regex pattern to two different strings with sed like: sed -E "s/\\t([A-Za-z]|[-])+$/\\t`get_addr.py -1`/" /path/to/file.ext sed -E ...
3
votes
1answer
37 views

Problem with regular expression in gawk ('\<' not working)

I am trying to get into bash a little bit. I was going through this guide but the second example in this subsection on gawk doesn't seem to work. The problem is this command: ls -l | awk ...
3
votes
1answer
49 views

Accept only y or yes with a one-line regular expression

I want to check if an input is y or yes (inputs in lowercase) and reject everything else What I have now is ^y[e]{'0,1'}[s]['0,1']$ But this accepts y, ye, ys, yes. I don't want ye and ys to be ...
3
votes
1answer
132 views

pcregrep newline lookbehind assertion bug?

Attempting to use pcregrep to print the first line after a blank line. For example, contents of file first line second line I need second line to be printed. Here are a few tests using the same ...
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 ...
3
votes
1answer
175 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) ...
3
votes
1answer
672 views

How to insert both vimscript variable and string into vim search and replace

I've only just found out about Vimscript's existence as of today, so forgive me if this question is more basic than it seems, but I've been struggling to find an answer to this question for the better ...
3
votes
5answers
5k views

Find files in multiple folder names

I am trying to list all the files from dir1, dir2, dir3 and dir4 which might be anywhere in as a sub directory of my cwd using the find command. I tried the following with no success: find . -type f ...
3
votes
1answer
2k views

Escaping of meta characters in basic/extended posix regex strings in grep

Is it possible to escape all meta characters of a string inside a variable before passing it to grep? I know similar question has been asked before on SE (here) and also a good explanation here, but I ...
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
659 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', ...
3
votes
1answer
243 views

Character classes: construct my own

I want to construct my own character class in a script, then modify (and use) it, for example: [:myclass:] contains a, *, \n (as linefeed) and [WHITESPACE]. I want to add all characters to ...
3
votes
1answer
53 views

How do back-references match in sed?

I found a one-line sed command on this site which removes duplicate entries in $PATH. There is one concept that I can't get my mind around. It uses saved matches in the match section to detect the ...
3
votes
3answers
205 views

How can I get the first column of a 300GB txt file?

let me first explain my problem in detail. It's actually pretty simple. I have a huge .txt file, 300GB to be more preciese, and I would like to put all the distinct strings from the first column, that ...
3
votes
2answers
859 views

Wget - Mirroring a full website with requisites on differents hosts

I am trying to make a full static copy of a wordpress website with wget to be browsed without any network connection (all links and images must be converted). The different requisites for the pages ...
3
votes
1answer
284 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 ...
3
votes
0answers
31 views

file(1) and magic(5) : prioritizing a result

My question follows that one: file(1) and magic(5) : describing other formats . I want to describe a FASTA sequence ( http://en.wikipedia.org/wiki/FASTA_format) It could be a DNA sequence (with only ...
3
votes
0answers
131 views

How to use regrex with AWK for string replacement in this example? [duplicate]

Possible Duplicate: How to use regrex with AWK for string replacement in this example? Suppose there is some text from a file: (bookmarks ("Chapter 1 Introduction 1" "#1" ("1.1 Problem ...
2
votes
6answers
2k views

Regex that would grep numbers after specific string

So I have a line: ID: 54376 Can you help me make a regex that would only return numbers without "ID:"? NOTE: This string is in a file.
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 ...
2
votes
3answers
740 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, ...
2
votes
2answers
3k views

grep: regex only for match anything between parenthesis

How do I match word only between parenthesis Input : this is (test.com) Desire Output : test.com
2
votes
2answers
687 views

How to find files with a certain subpath?

I need to find all xml-files that are placed in folders named config. Also config must be somewhere under a folder named trunk. For example, I am interested in all files like below: ...
2
votes
3answers
106 views

Apply item replacement inside enumerate in Perl regex?

Data 7. Masi is the same as \begin{enumerate} A B ABC ha lorem \end{enumerate} 1. ABC is \begin{enumerate} P B Lorem maam \end{enumerate} which I want to be 7. Masi is the same as ...
2
votes
4answers
2k views

Extract IP address from a string

The Linux host command returns: hostA.domain.com has address xx.xxx.xxx.xx How do I get just the IP address and put it in the $ipaddr variable? open(FILE, "hostlist.txt") or die("Unable to open ...
2
votes
2answers
204 views

How can I extract the numbers in the file using sed or any other tool?

I have a file that has this format [ 2014/05/01 10:48:26 | 13963 | DEBUG ] It took 11.16837501525879 seconds to complete the process So I have thousands of lines like this and I would like ...
2
votes
2answers
250 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 ...
2
votes
3answers
100 views

Pulling out keywords using standard command line utilities?

As an example, this is the pronunciation of "when" according to Wiktionary. enPR, IPA and X-SAMPA are different schemes for showing pronunciations. when:* {{a|US}} {{enPR|wĕn|hwĕn}}, ...
2
votes
3answers
589 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
393 views

Negative lookbehind/ahead assertions in Linux less pager (or vim)

I want to find all instances of "index" not followed by .php in a log using less. /index(?!\.php) does not work. Is this possible? What is the regex for less and vim (do they differ?). Is this not ...
2
votes
1answer
364 views

Regex doesn't work with grep

I have a string like this TEST="/dev/sda1 on /media/sda1 type vfat (rw,relatime,fmask=0000,dmask=0000,allow_utime=0022,codepage=cp437,iocharset=iso8859-1,quiet)" (Result from mount | grep sda1) ...
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
1k 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 ...
2
votes
2answers
153 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, ...
2
votes
3answers
303 views

awk, sed, grep, perl… which to print out in this case?

I have this syntax in a file (http response times from analog): <thead><tr><th class="x">seconds</th><th class="R">reqs</th><th ...
2
votes
2answers
388 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 ...