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

learn more… | top users | synonyms (1)

5
votes
3answers
171 views

How to remove dot and space from beginning of filenames

I'm trying to change jpg files named with a dot and a space at the beginning and with a missing dot before the end (like this . Startjpg to Start.jpg The dot makes the files hidden from sed and ls ...
2
votes
3answers
27 views

Reverse match in sed, replace opposite of what was found

Say I have this output from my syslog, precalculated src_hash => 20 bytes @ 0x7f811c001e20 A6 2D E5 CD 2A BA F0 42 56 66 19 D4 61 1A E3 A0 .-..*..BVf..a... C7 5E 5F 77 ...
4
votes
6answers
174 views

Finding the number of time a particular number in a file where range also specified

I have a file with numbers separated by ,(comma). In between it also contains a number range like 300-400. Say for example I have a text file, namely testme.txt which looks like, ...
4
votes
2answers
53 views

sed regex issue

I'm trying to replace a string in a file: $RELEASE = '1234'; The whitespace between RELEASE and the = symbol could be either spaces OR tabs. The quotes around the version number could be either ...
4
votes
3answers
124 views

Regex alternation/or operator (foo|bar) in GNU or BSD Sed

I cannot seem to make it work. GNU sed documentation says to escape the pipe, but that doesn't work, nor does using a straight pipe without the escape. Adding parens makes no difference. $ echo ...
1
vote
4answers
57 views

Simple sed replacement of tabs mysteriously failing

This ought to be really simple, but for some reason it is not working: sed -i.bak -E 's/\t/ /' file.txt Instead of replacing tab characters, it's replacing t characters. I've tried every ...
2
votes
1answer
41 views

Removing non-printable characters using POSIX sed

Files created with roff and other "old-school" tools (for example man pages on many Unix systems) generate bold and underlined text in minimalistic terminals using tricks involving non-printable ASCII ...
5
votes
1answer
595 views

Why do I have to escape a “dot” twice?

I know that we can escape a special character like *(){}$ with \ so as to be considered literals. For example \* or \$ But in case of . I have to do it twice, like \\. otherwise it is considered ...
4
votes
3answers
96 views

Search and replace strings that are not substrings of other strings

I have a list of replacements like so: search_and -> replace big_boy -> bb little_boy -> lb good_dog -> gd ... I need to make replacements for the above, but at the same time avoid ...
2
votes
3answers
42 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 ...
0
votes
3answers
62 views

Grep finding phone numbers in files

How would I look for files in the /etc directory (but not subdirectories) that contain a standard United States long distance phone number, written using the pattern of 1-###-###-####, where each # is ...
1
vote
1answer
25 views

awk regex matches wrong?

Playing around with awk I noticed this behavior: [root@ror6ax3 ~]# grep open * | awk '$2 ~ /opens*/ {print $0}' install.log:Installing openldap-2.4.23-32.el6_4.1.x86_64 install.log:Installing ...
3
votes
1answer
28 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 ...
1
vote
1answer
38 views

Why does the same sed regex (after grep) fail when run in a bash script vs bash command line?

Here's the script. It is successful when I run it from the BASH prompt, but not in the script. Any ideas? When I say "fails," I mean the sed regex doesn't match anything, so there is no replaced ...
0
votes
0answers
13 views

Using regex in locate [duplicate]

I would like to search for both words pdf and hack in all filenames and their paths. The order of the two words doesn't matter. I use the command locate with a regex pattern: locate --regex ...
6
votes
5answers
226 views

Strategy to extract movies's name from this ununiform dataset?

I am working on a movie database problem to improve regular expressions, this is the problem I'm running into. My dataset looks like this: Movie Name (variable space and tabs) year Movie1(can ...
4
votes
3answers
236 views

Which regular expression methods to validate input could be used in shell scripting?

#!/bin/sh re="\/$" if [ $1 =~ $re ]; then echo "${ATTENTION_PREFIX}$1 DIRECTORY MAY NOT CONTAIN A \"/\" OR LITERAL SLASH!${ATTENTION_POSTFIX}" exit 1 fi Executing sudo file.sh ...
2
votes
1answer
41 views

Context of the matching regular expression

I'm using egrep with the -o option in order to just get the matching part of the line, e.g. cat /usr/share/dict/words | egrep -o '(aa|ii)' Now I'd like to see some context of the match, i.e. a few ...
4
votes
3answers
159 views

Print all lines not containing punctuation

I want a regex pattern which can print all lines not containing punctuation : Input : .I am line 1 I am ! line 2 I am line (3) I am line 4 Output: ( should be) I am line 4 What I've tried so ...
2
votes
3answers
48 views

A challenge for sed convert code from Mathematica to Matlab

I have the following output from Mathematica to C code: $ cat file.txt Complex(1.3, 2.7)*Power(kappa, 2.0) + Complex(0.3, 2.0)*Power(kappa,5.0)*log(alpha) The file is extremely long (many tens of ...
2
votes
1answer
65 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
2answers
28 views

can regex capture groups be used in GNU find command?

With the GNU find command (GNU findutils 4.4.2), a regular expression can be used to search for files. For example: $ find pool -regextype posix-extended -regex ...
1
vote
4answers
189 views

Enclose multiple lines in quote Vim

I have blocks of the following form: String that is not supposed to be enclosed in quotes String that is supposed to be enclosed in quotes String that is not supposed to be enclosed in ...
2
votes
1answer
41 views

How to match a particular form of optional sequence from a manual synopsis, including variations?

In this Q&A there is a reference to the manpages synopses being based "loosely" on the Extended Backus–Naur Form of metasyntax notation. It's interesting and serves as background. That being said, ...
1
vote
5answers
46 views

grep for text that may or may not be present

Lets say I have a file batman; robin; superman; password = ""; wonderwoman green lantern if I want to check if there is a password present, i.e there is something entered in the "" . here is an ...
-1
votes
2answers
43 views

How to get `expr match` to extract colon-delimited key-value pair?

Given: myvar="present-value: 1", I'd expect expr match "$myvar" '\([0-9]\)' to output 1. However, instead it outputs blank and exits with a non-zero status code indicating no match. How can I get it ...
2
votes
1answer
40 views

How to perform a multiline grep across multiple files?

I'm trying to grab this pattern wherever it occurs in multiple log files (Note:these patterns can vary greatly in size, i.e. the amount of Blahs): Found an txt File Blah Blah 10019874 ...
0
votes
2answers
21 views

Finding and replacing text inside awk block

I want to find and replace a value inside awk block. Here is a sample input file to my script P1 10,9:11/18013013582 ,10:1 ,167:0 ,487:5/E.164 11,9:15/310410532169026 ,10:60 ,167:0 ,487:4/IMSI ...
1
vote
2answers
80 views

Aix grep wildcard not working

I have a file called message that has the line CSC/UT/USA/WBIMB/SAP/orders05:orders05/ORDERS05/NONE/ORDRSP/758 I am doing a grep (Platform is AIX 6.1) grep ...
3
votes
3answers
101 views

History of Bash globbing

Is there a historical reason why Bash "globbing" and regular expressions are not identical? For example, I believe that in Bash [1-2]* matches anything that starts with a 1 or a 2 followed by anything ...
1
vote
3answers
19 views

To bold one line with Perl regex

I have 1. Lorem He he he % not sure spelling 2. Lorem ipsun I want to have \textbf{1. Lorem} He he he % not sure spelling \textbf{2. Lorem} ipsun My pseudocode attempt perl -000pe ...
2
votes
3answers
97 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
3answers
79 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
1answer
65 views

csplit not obeying backslash r \r as carriage return

I have a file with \r\n line endings (CRLF carriage return line feed). This command works: $ csplit --silent --prefix=email-emailbad.txt- \ --digits=3 emailbad.txt '/^\.^M/'+1 '{*}' This ...
0
votes
2answers
79 views

How to match date in Perl — assuming UNIX regex is the same as Perl's

I want to match dates of the form Monday May 26 My attempt to do this is the following: /(.*day Jan.*|Feb.*|Mar.*|Apr.*|May|Jun.*|Jul.*|Aug.*|Sep.*|Oct.*|Nov.*|Dec.* [1-31])/ The regex form in ...
3
votes
3answers
102 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
1answer
117 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 ...
2
votes
6answers
378 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.
0
votes
4answers
63 views

Print text between tags (inclusive) if certain text is found

I've got a task to extract data from several Apache servers. The task is to print out: <Directory ...> ... </Directory> where +ExecCGI is located within. Let me give an example to ...
1
vote
2answers
83 views

Regular expression find 3 or 4 numbers

So, I have 50+ words and I have to match the numbers in them. These numbers can be 3 or 4 digit numbers. I think I tried every way there is, but nothing seems to work (I need to remember the number as ...
3
votes
5answers
112 views

How to “grep” a string of a returned result

How to grep the number 50.0 from the string temp=50.0'C In JavaScript I can use the expression /temp=(.*?)'C/ to get it. But I cant get it running in grep.
0
votes
1answer
62 views

Search for pattern across multiple files and replace a sub-part in all occurences

I am helping a friend with her thesis written in LaTeX and I need a quick solution for a "search and replace". Using a terminal in Linux, how can I find the following pattern: \caption{*\cite{*}} ...
1
vote
5answers
99 views

Extract one occurrence of a pattern from file

I have large file containing logs similar to one shown below. I would like to find all transactions(TR#) that were affected with the error. I need to extract one occurrence of each TR# ID. How could ...
-1
votes
2answers
56 views

Regular expressions in a shell [duplicate]

I have two questions about using regular expressions inside a shell. 1. Use of * According to wikipedia page on regular expressions: *: The asterisk indicates there is zero or more of the ...
0
votes
1answer
102 views

bash using grep and sed

This is for our weekly prac, no marking or grade (we don't even get the answers!) I'd really appreciate it if anyone has the time to help explain this exercise to me, as I'm trying to get this work ...
1
vote
2answers
51 views

Lazy regex using Bash

I'm trying to match just the text contained within the HTML tags using Bash's built-in regex function: string='<span class="circle"> </span>foo</span></span>' regex='<span ...
0
votes
1answer
32 views

Expansion of *,#,% - What do they mean? [duplicate]

for j in * .* ; do cp $j ../$name-S$i.gid/${j%%. * }$i.${j#*.} done Could someone tell me what this for loop does? What do *, % and # mean? ($name is a path of a directory in which there are ...
1
vote
2answers
127 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
2answers
157 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 ...
3
votes
1answer
81 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 ...