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

learn more… | top users | synonyms (1)

5
votes
5answers
124 views

How can I “merge” patterns in a single line?

I am doing grep and sed and I get 2 lines of a file that I am interested in. How can I get these lines in a single line ending with the new line character? Now I am getting: pattern1 pattern2 ...
4
votes
5answers
69 views

Insert Lines after Multi-Line Regex Match

I've got an XML file I need to append to after a specific set of tags. Basically the file structure looks like this: <key>servers</key> <dict> ... Server Details ... </dict> ...
2
votes
1answer
723 views

lftp exclude syntax confusion

I am confused with the syntax description given for lftp from their website: -x RX, --exclude=RX exclude matching files -X GP, --exclude-glob=GP exclude matching files How ...
4
votes
3answers
160 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 ...
4
votes
1answer
114 views

command: ls /etc | sort | grep d* is yielding no results but ls /etc | sort | grep p* lists entire directory

I am playing around with piping and grep tonight. I know that grep uses regex and that * means 0 or more occurrences of the preceding character. So the way I understand it is that if I do the ...
6
votes
1answer
250 views

find words with all vowels in order from a file

I have a large dictionary file with 300,000+ words in it and I'm trying to find all words with the vowels aeiou in that order and have only exactly 5 vowels. My current attempt does not seem to be ...
3
votes
1answer
31 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 ...
14
votes
10answers
2k views

There must be a better way to replace single newlines only?

I am in the habit of writing one line per sentence because I typically compile things to LaTex, or am writing in some other format where line breaks get ignored. I use a blank line to indicate the ...
2
votes
3answers
42 views

bash: regular expressions in if expression

In my script on my ubuntu machine I declare a string like this: DEBUG_PACKAGE_LIST=$(apt-cache search dbg | awk '{ print $1 }' | grep -e "-dbg") For you to help you understand my problem here is ...
3
votes
1answer
40 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 ...
2
votes
4answers
49 views

Matching string with a fixed number of characters using grep

I am trying to find all 6 letter words using grep. I currently have this: grep "^.\{6\}$" myfile.txt However, I am finding that I am also getting results such as: étuis, étude. I suspect it has ...
0
votes
2answers
30 views

add bracket across numbers at only end of each line

I have a file having lines as: ram_reg_10/raja_reg_9/raghu_reg_8 abc_reg_4/bcd_reg_5 cad/pqr_reg_91 I want to convert string "_reg_number" into [number] at only last of every line in vi ...
6
votes
2answers
163 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 ...
0
votes
3answers
31 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 ...
5
votes
3answers
196 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
1answer
187 views

Aptitude regex engine

I was trying to lookup the versions of libdb in the debian archives with something like that: $ aptitude search '~S~ndev$(~nlibdb~(\d~|-~)!~v~rnative)' which didn't work as expected so I changed ...
3
votes
5answers
114 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.
4
votes
6answers
187 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, ...
2
votes
3answers
49 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
2answers
57 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 ...
1
vote
4answers
65 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 ...
0
votes
3answers
78 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 ...
2
votes
3answers
68 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
79 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
604 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
107 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 ...
1
vote
1answer
45 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 ...
1
vote
1answer
27 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
33 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 ...
0
votes
0answers
14 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 ...
4
votes
3answers
814 views

File manager which is able to filter using regular expressions

Is there any graphical file-manager which supports thumbnails for linux like thunar or nautilus, which allows me to filter a directory by a regular expression. For example typing /home/user/*tex ...
2
votes
2answers
388 views

Regular expressions with ExpectJ

I've been used the ExpectJ Java library to automate some jobs on a Linux server, however now I need to automate a job that sends a different text response for each execution. Here is the output: Last ...
4
votes
3answers
276 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 ...
6
votes
5answers
233 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 ...
2
votes
1answer
42 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 ...
6
votes
3answers
180 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
52 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
68 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
41 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
212 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
50 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
votes
2answers
48 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 ...
1
vote
5answers
47 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 ...
2
votes
1answer
46 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 ...
3
votes
3answers
105 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
2answers
146 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 ...
2
votes
3answers
98 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 ...
1
vote
3answers
22 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
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 ...
4
votes
1answer
65 views

Regex this question format into LaTeX

1. Lorem ipsun la la la? 1. Sopor 2. Stupor 3. Torpor 2. A patient has Lorem? 1. Sopor 2. Stupor 3. Somnolentia 4. La 5. Coma 3. Doesn't Response to strong external irritants is short. Tendon, ...