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

learn more… | top users | synonyms (1)

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
2answers
42 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
1k 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
38 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
32 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
41 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
105 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
66 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
87 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
127 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
547 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
2answers
2k views

syntax of ls --hide= and ls --ignore=

ls --hide and ls --ignore provides the possibility leave out files defined through regular expressions set after the --ignore= part. The latter makes sure that this option isn't turned off via -a, -A. ...
3
votes
5answers
4k 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

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
631 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
193 views

Interpret an interpolated variable as a literal string in Perl regex

I am using perl to colorize a matched string MATCH=aaa printf "aaa\n/dev/aaa\nccc\n" | perl -pe "s/($MATCH)/\e[1;31m\1\e[0m/g" This works well until I try to match a string containing special ...
3
votes
1answer
229 views

rsyslog: make %syslogtag% equal length

I have rsyslog configured to log in the following format: $template long-iso,"%$NOW% %timereported:8:15% %syslogtag% %msg%\n" which produces logs like this one: 2013-12-14 15:59:39 ...
3
votes
1answer
219 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
1k 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
35 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
132 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
828 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
280 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
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
706 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
722 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
2k 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
532 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
103 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
167 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
170 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
95 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
548 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
1answer
277 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
57 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
138 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
290 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
367 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 ...
2
votes
3answers
60 views

Find and change certain LaTeX coding in vim, using regular expressions?

How can I change the following LaTeX coding: \cite{1,3-7,9} to \cite{1,3,4,5,6,7,9} in vim? I would prefer to use regular expressions.
2
votes
3answers
80 views

bash - extract filenames from html file containing multiple links

I have downloaded an html file autogenerated by a script on a webpage. The file contains multiple links, including links to images I am trying to extract the full names of the images, for example ...
2
votes
1answer
71 views

sed regexp HEX char

i have this string: echo -e "a12\x8fb12\x9f"|xxd 0000000: 6131 328f 6231 329f 0a a12.b12.. i want delete this sequence string 12\x9f and 12\x8f with sed. i can do it with this ...
2
votes
3answers
108 views

Find a pattern with no repeated numbers in egrep

I need help finding a regular expression to match all digit sequences which has no repeated digit. Example: 198345 -> matched. 198315 -> not matched (1 repeated twice ) How can I achieve this in ...
2
votes
2answers
234 views

changing pattern by removing spaces?

file : hi hello,new york, , ,brazil site ,brazil january,month is feb , , ,indiana jones task: delete all horizontal space (spaces and tab) between any two comas (like , ,) if ...
2
votes
2answers
1k views

extract last match from logfile till end

Using sed, grep, awk, or ruby, what's a simple way to find the last match of a string in a larger log file, and send every line following it (inclusive) to another file? Example: grep -A222 ...
2
votes
3answers
922 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 ...
2
votes
2answers
3k views

Sed: replacing digits

I am trying to write some install scripts for when I install/upgrade Ubuntu. I'm having trouble with fixing logrotate.d: /var/log/ufw.log { rotate 7 weekly missingok notifempty compress delaycompress ...
2
votes
1answer
187 views

sed one-liner to delete word-medial spaces

I have a text file that consists of some N ORMAL E NGLISH W ORDS in all-caps. Unfortunately, these words all have a space between the first and following letters, which is not acceptable. I tried ...