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

learn more… | top users | synonyms (1)

1
vote
3answers
34 views

Remove patterns from string using bash

I'm trying to remove some patterns from a file input string (parsing file line by line). Here is a sample string: 1: (10/17 12:49:31.175) - CONSTANT ID1 - CONSTANT ID2: RAW DATA OUT > ...
1
vote
2answers
47 views

Regex for contains at least one instance of STRINGA and no instances of STRINGB

What regex will match: contains at least one instance of "1221", and no instances of "7474" i.e. matches "7777 1221 4543" but does not match "7777 1221 7474 4543" I suspect I have to use negative ...
9
votes
4answers
395 views

Regular expression using \\ vs using \

Why does grep e\\.g\\. <<< "this is an e.g. wow" and grep e\.g\. <<< "this is an e.g. wow" do the same thing? If I add a third slash, it also has the same result. BUT, once I ...
1
vote
2answers
62 views

What does this regex mean? <!--.*?-->

What does this regex do? <!--.*?-->
-1
votes
1answer
65 views

Regular expression to mask credit card digits in a file

What is the most simplest way to mask all numbers but the last 4 digits of a credit card number in a file using sed. Using egrep with the following regular expression we can find numbers of Visa, ...
0
votes
0answers
25 views

Find & replace image URLs in one file from another file

I am trying to replace all the image source URLs in an HTML file from a list of URLs in a text file. File1.html <td class="MetadataRes" width="380px" colspan="2" style="border-top: 1px #336699 ...
1
vote
3answers
90 views

“bad flag in substitute command: '{” while replacing a string with sed from one file to another file

I am trying to replace strings found in File1 with strings in File2 File1 <IMG SRC="/Repository/GetImage.dll?baseHref=Orange/2011/03/27&amp;EntityID=Ad12911&amp;imgExtension=" /> ...
2
votes
4answers
71 views

replace several comma out side one or more sets of curly braces and exception that in one or more sets of curly braces

In the text file I have multi records. Every record has multi column with comma separated, some column have one sets of curly brace and other have more than one curly brace. I need: If a comma is ...
0
votes
2answers
37 views

Regex to grep filler followed by signed float

I want to extract the last column from lines that look like: 880.952 NAV_Y uSimMarine -146.17516 The number isn't always negative, and isn't a fixed length (i.e. doesn't ...
2
votes
2answers
22 views

Match from current line until a line that doesn't match a pattern

I really tried searching but could not find anything (it's hard to know what exactly to search for). I know how to do this with sed: print from current line until the line that matches SOMETHING: ...
1
vote
1answer
46 views

Regex works in shell but not through Perl script

I'm having trouble executing a Perl script through the Unix shell using Perl's system command. I've had more complex regex commands I had to adjust accordingly to convert from Unix to Perl, and ...
0
votes
2answers
39 views

To match only innermost environment by Regex

I want to match the innermost environment of begin{question} and its corresponding end{question}. Example data \section{Takayasu arteritis} \begin{question} {You get a patient. What do you notice ...
2
votes
1answer
73 views

To write this Pseudocode with Regex

I am doing a Makefile which I run regularly by Crontab every day at 0230 crontab -e; 30 2 * * * /bin/thePseudocode Python-like Pseudocode directories = ["Cardiology", "Rheumatology", "Surgery"] ...
0
votes
3answers
61 views

Bash Combine Replacement and Sub String Extraction in One Step

For a string in this format: ./my-site.sub.domain.com I would like to remove the .,-, and /, and trim the remainder to 16 characters: mysitesubdomainc I've done this ...
4
votes
4answers
65 views

Replace using vim - Replace a pattern […] by a string

I have a file containing lines in below e.g. format - [foo@host.com, bar@host.com], Payment processed - 23499, params = {'invoice':3243} I only want the account numbers i.e. 23499. It's a number. ...
2
votes
2answers
43 views

What is the regex to validate Linux users?

When adding a new user, how is the string validated? I suppose there is a regular expression. What is that regular expression?
0
votes
3answers
44 views

Shell pattern matching vs. regular expressions occurence [duplicate]

I am relatively new to Unix, and I stumbled across one curiosity. Some shell constructs like case or find, employ pattern matching, but it is not quite regex. Other commands, like ed, sed, vi, and ...
2
votes
2answers
79 views

Find characters in the file

I need to find lines in the text file that have at least one x and at least two ys. I tried the following: grep -E "x.*y{2}" file.txt This method did not work. Is there another way to do it?
2
votes
3answers
174 views

grep to find words with all vowels

Using grep -E and regular expressions I need to find words containing all of a group of characters in no particular order. For instance a word containing all vowels (aeiou) however the vowels can ...
1
vote
1answer
101 views

vim replace search text by manipulated search text

This is a very simplified version of my problem. Suppose I have a text file containing several places the code 20091a or 20092a or 20093a. I'm wishing to surround all this code by the text ...
2
votes
3answers
102 views

Yum regular expression or wildcard search?

Fedora documentation says: 5.2. Advanced Searches If you do not know the name of the package, use the search or provides options. Alternatively, use wild cards or regular expressions with ...
12
votes
3answers
2k views

Why does [a-z] asterisk match numbers?

I have 3 directories at current path. $ls a_0db_data a_clean_0db_data a_clean_data $ls a_*_data a_0db_data: a_clean_0db_data: a_clean_data: $ls a_[a-z]*_data a_clean_0db_data: a_clean_data: I ...
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
322 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
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
2answers
86 views

Extract specific set lines matching a rule

I have a large file with the logs as shown below. There are about 30000 instance of such events logged. I need to extract those lines beginning with RINGING and CLOSE (included) and which does NOT ...
4
votes
4answers
56 views

regexp which matches all tokens independent of order

I have a quick (and hopefully simple) question about regexps. I'm trying to think of a regexp which matches all of a set of tokens irrespective of order. So for example I have a file with the names ...
-1
votes
2answers
59 views

How to remove a newline character in a particular line through perl?

I want to remove a newline character in a particular line through perl one-liner command. Input: 1407233497,1407233514,bar 1407233498,1407233515,foo mingstats&fmt=n 1407233499,1407233516,foobar ...
0
votes
1answer
38 views

How can I parse my String using regular expression to get only certain number of matches? [closed]

I am currently using this following regular expression to split my String into groups. ^([a-zA-Z0-9]*)(\[([a-zA-Z0-9=]*)\]){0,1}@([a-zA-Z0-9]*)$ Pty[R=4]@ID I am getting four groups 1) Pty 2) ...
3
votes
1answer
53 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 ...
1
vote
3answers
79 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
109 views

Checking if an input number is an integer

I'm trying to check if an input is an integer and I've gone over it a hundred times but don't see the error in this. Alas it does not work, it triggers the if statement for all inputs ...
8
votes
7answers
385 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
1answer
612 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 ...
4
votes
5answers
153 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> ...
6
votes
1answer
447 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
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 ...
2
votes
3answers
59 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 ...
2
votes
4answers
200 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
38 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 ...
5
votes
3answers
306 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
213 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
210 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
70 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
257 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
216 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
248 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
633 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
133 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 ...