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

learn more… | top users | synonyms (1)

2
votes
1answer
93 views

String digit regex replacement

I'm struggling a bit with this replacement regex, I could not understand why it is behaving this way. What I want to do is to replace all numeric digits after the last /: str="aa/a0b0/42" echo ${str/%...
2
votes
3answers
54 views

Recursively delete files with 'WIDTHxLENGTH' in the name in a directory

I have a directory that has subdirectories, and I want to delete images named like thumbnail-name_WIDTHxLENGTH.{jpg,png}, where WIDTH and LENGTH are both decimal integers, like box-image-150x150.jpg ...
3
votes
2answers
95 views

How to search for the word stored in the hold space with sed?

This is a sed-specific question; I am well aware it could be done with other tools but I am working on expanding my knowledge of sed. How can I use sed to globally quote (actually backtick) a word ...
0
votes
1answer
26 views

how can I target filenames with 2 digits with the rename tool?

I am trying rename files in a directory which have a 2 digit number - i.e. any and all numbers from 10 to 99. I want to prepend a 0 so that for example 23 becomes 023, so that files like this ...
2
votes
5answers
638 views

Replace characters in user part of email address

I have a list that looks like this: user-name@domain.example user.name@domain.example user_name@domain.example ... I would like to replace any occurence of a dot in the username part. So that: ...
1
vote
2answers
198 views

“egrep: empty (sub)expression” when attempting to filter out words from a list

I'm trying to follow this question/answer from Server Fault: In tail -f, how do I filter out stuff that has certain keywords? The following is producing egrep: empty (sub)expression: # Respect user'...
3
votes
4answers
210 views

Match-zero-or-more operator in shell globbing

I'm stuck in a quite trivial problem here: how can I make the * symbol in bash mean zero or more, like it does in tools such as sed? For example, ak* should match any file whose name consists ...
1
vote
3answers
216 views

Fast field extraction with grep

The problem I have a 32M lines file with the following format token^Iname^Iurl$ where ^I is the tab escape sequence, and $ is the end-of-line. I need to get the url corresponding to not more than ...
2
votes
1answer
164 views

How do I combine grep with multiple arguments and different output switches

I want to grep with multiple arguments where i want to show lines in the order they appear in my source file before one argument and lines after the other argument , i e combine: grep -A5 onestring ...
0
votes
0answers
113 views

Need help with a regular expression with calibre

I'm actually in trouble about some regex in order to import books properly with calibre. Hopefully someone more experimented than me will have time to help... My book's names (entries) are in this ...
4
votes
2answers
331 views

difference between [] and \(\) in Basic Regular Expression

Consider: $ echo '<H1>heading</H1>' | grep '<H\(1\|3\|2\)>.*</H\1>' $ <H1>heading</H1> and $ echo '<H1>heading</H3>' | grep '<H[1-3]>.*</H\...
2
votes
1answer
63 views

Running grep for a string with non-rendering characters

So I have a log.txt files which I can search in Notepad++ on Windows, however due to the sheer number of them, I'd like to be able to run grep on them. However, the data is formatted with non-...
4
votes
1answer
48 views

What REs can I use with =~ operator

The man page for bash has this to say about the =~ operator: An additional binary operator, =~, is available, with the same precedence as == and !=. When it is used, the string to the right of the ...
0
votes
0answers
14 views

AWK searching for numeric patterns in a string [duplicate]

I'm trying to remove any occurrence of six-digit numbers occurring in my data. for instance: "This is test data 765648.txt" would become "this is test data.txt" and "This example 457623 has embedded....
0
votes
2answers
42 views

Find the position of repeated character “&”

How can determine the position of “&” in the column number 5 to substring the value 2 or substring value 3 by awk functions column1|column2|column3|column4|variable1:value1 & variable2:...
0
votes
1answer
178 views

SED find and replace element in filename with incremental values

A sample filename is Sample-0.0.1-SNAPSHOT.jar (result of a maven build in jenkins). My goal is to run a shell script that executes on failure and what that script does it adds incremental numbers to ...
1
vote
1answer
56 views

sed - simple regular expression replacement not replacing

I'm trying to use sed to remove any subdomain prefix from a list of hostnames, but it's not replacing anything when I use a regular expression. Here's the simplified example that's not working: echo ...
3
votes
2answers
83 views

Remove all but the latest backup file monthwise

I've got a directory, containing lots of weekly generated files with names like db_20130101_foo.tgz db_20130108_foo.tgz db_20130115_foo.tgz ... and so on. Over the years, the disks will get ...
8
votes
3answers
139 views

grab text by pattern with start and end tag in multiple lines [duplicate]

I want to grab multiple lines in a file with a pattern that has a starting and an ending tag in an ungreedy way. For example I have the following input: file.txt START test1 test2 foo END some more ...
1
vote
1answer
183 views

pkill with regex

I have few java processes running like below, java -jar /my/path/to/app/myapp.jar java -jar /my/path/to/app/prodapp.jar java -jar /my/path/to/app/testapp.jar java -jar /my/path/to/app/myapp_v.01....
1
vote
2answers
119 views

How can I use the environment variables inside a regular expression in awk?

#!/bin/sh ALTER="1153" NAME="aio" for i in $(ps -eo pid,etime,comm | \ awk -v alt="$ALTER" -v name="$NAME" '$2~/[^0-alt]-/ && $3~/name/{print$1} '); do echo $i done
0
votes
2answers
58 views

how do i write a regular expression using grep to find lines that start with 'O' or 'o' AND contain 2 consecutive chracters

how do i write a regular expression using grep to find lines that start with 'O' or 'o' AND contain 2 consecutive 'c' characters. grep '^[Oo]c{2}' filename
3
votes
2answers
107 views

Using find -exec how to print name of file in front of each line?

Similar to, but beyond: find . -iname "somepatter*txt" -print -exec cat {} \; How might the file name from each file be printed in front of each line with the results from find?
2
votes
2answers
103 views

Remove specific (known) extension from filename if it's there

I'm not even sure whether grep is meant to be used for regex in one-line data. Googling led me to this lookahead expression that, along with grep, matches any .ext file and returns bare name: XX@XX:/$...
1
vote
2answers
72 views

Using a shell variable with grep [closed]

I have a list of tokens in a text file and want to use grep to get the lines from a second text file that contain those tokens, but seem to be having trouble accessing the shell variable with grep: ...
1
vote
1answer
25 views

Regex quantifiers are not working well with locate

This output is rather self-explanatory: XXXXX@debianvirtualbox:~$ locate -r "^/var/lib/tomcat[0-9]/.*" -l 10 /var/lib/tomcat8/conf /var/lib/tomcat8/lib /var/lib/tomcat8/logs /var/lib/tomcat8/webapps /...
4
votes
3answers
157 views

egrep regular expression for times over five minutes

I have the following time formats in a text file `1` equals one second. `5|01` equals five minutes and one seconds. `13|01` equals thirteen minutes and one seconds. `21|12|01` equals 21 ...
1
vote
2answers
58 views

How to make + work in sed's regexps on FreeBSD?

Consider the following typescript: $ freebsd-version 10.0-RELEASE-p5 $ echo ' found' | sed -n '/[[:blank:]]\+/p' $ echo ' found' | grep '[[:blank:]]\+' found When I do this on Arch Linux with GNU ...
4
votes
3answers
251 views

grep regular expression solution (greedy not working)

I have the following text in the data.txt file :MENU1 0. public 1. admin 2. webmail :SYNTAX ! opt1, ... : :ERROR1 Error #1, blah... blah.. blah... Please do ... :ERROR2 Error #2 ... and I want ...
2
votes
2answers
99 views

Cleaning PHP exploits from infected files using sed

Recently my server has been a target for a series php exploit attacks and using ClamAV I was able to identify many infected files. The catch here is that there are legitimate files that are infected ...
0
votes
0answers
50 views

swap strings of text inside vim using regex

I am working with a file where I need to swap "string A" and "String B's" locations simultaneously with a regex inside the vim. Here is an example line of code: succ_1st=`grep 'resulted in successful ...
0
votes
1answer
85 views

Match everything after backslash and before space

I have strings similar to the following: *unknown*\*unknown* (8) hello\morning (3) I'm trying to match just morning or *unknown\*. So far I have tried: [^\\]+$ But that matches from backslash ...
10
votes
5answers
1k views

How to print file content only if the first line matches a certain pattern?

I am writing a script, I want to check if the first line of the file matches a certain pattern and if it does then print out the file. How can I achieve this? How do I check for pattern? Is there a ...
-3
votes
1answer
121 views

Regular expression: when every alternate character is a digit

I am trying to write a Linux command using egrep that validates lines whose even character is a number. I am doing it this way: egrep "^(.[0-9])*$" text.txt However, this fails for i) empty ...
0
votes
1answer
26 views

subtract dates from two files for same process

I have initiated 50 processes and in one file "say start_time", I have stored when process was started and in another file "say end_time", I have stored when process finished. files are like below - ...
1
vote
2answers
89 views

how to swap text inside files [duplicate]

Inside my file I need to swap places of all instances of : grep 'search string' and sed '/string of text/,/string of text/{//!d}' In VI I have tried a regex with no luck - it says something ...
0
votes
1answer
18 views

Is there a way to get all the NEWS.gz which is/was this year in /usr/share/doc/ of $PACKAGENAME

If people go into /usr/share/doc/$PACKAGENAME (you can use any valid package name which you have installed on your system) you will see that the directory has various files in it, at the very least ...
-3
votes
1answer
43 views

Regexp pattern - is it correct and what is its purpose? [closed]

Is this pattern correct? logPattern = ^NAME.log(-\d+)?$ What is the purpose of this?
1
vote
1answer
135 views

Including or in grep pattern

I want to grep a word wrap in single quote or double quote eg. "myWord" 'myWord' I am using grep -rn "\"\|\'myWord\'\|\"" this should allow "myWord" , 'myWord" , "myWord' and 'myWord' , but ...
1
vote
1answer
90 views

Why is vi (but not cat) showing ^[[31m[^[[m^[[31m" after I use sed and output to a file I then edit? [closed]

When I use cat Variables/user-extensions.js | sed -e 's/css/XXX/' on an input file of the form storedVars["css_body"] = "css=body"; storedVars["css_main"] = "css=main"; I see output such as ...
1
vote
1answer
187 views

Sed replace portions of lines not beginning with http://

I'm getting stuck with sed.. I'm trying to prefix links in an html file with a / when they don't start with http.. I'm aware this is possibly not the best way to go about this but I'm just after a ...
1
vote
3answers
256 views

parse credentials from PHP configuration file

I'd like to write bash script to parse data from a configuration file. I searched for this but without finding something I could modify to suit my needs. Joomla! config file: public $access = '1';...
4
votes
3answers
510 views

sed, convert single backslash to double backslash

I have a json string, which has a potpourri of doubly escaped/singly escaped newline chars. Json parser doesn't allow its string value to have single backslash escapes. I need to uniformly make all ...
1
vote
1answer
76 views

grep -E regex syntax changed?

A lot of documentation I've read says that the following command should match lines where x appears at least 3 times in a row but no more than 4 times in a row: grep -E {x}\{3,4} E.g. http://www....
0
votes
1answer
458 views

logstash regex match in if condition

In logstash filtering, I have multiple tags setup based upon different error conditions and all the tags has a prefix, something like "abc:" In the output, I want to send email based upon just "abc:*"...
4
votes
1answer
378 views

Match pattern \\\" using grep

I have a json string inside json. This got encoded multiple times and I ended up with many escape backlashes: \\\". The much shortened string looks like, '[{"testId" : "12345", "message": "\\\"the ...
0
votes
3answers
110 views

Pulling out a certain value from a string via regex

I've see a bunch of examples, but I just can't seem to make this work. Can grep output only specified groupings that match? for example seems like it should work, but I get either errors or no output ...
2
votes
1answer
112 views

Double square parenthese inside grep pattern

grep "^[[:space:]]*clientPort[^[:alpha:]]" zoo.cfg This is a grep pattern to extract the line that contains the port number from the configuration file. 1) Normally in a regex, I use square ...
0
votes
2answers
161 views

Use multiple commands in sed to insert text after string with special characters

I am having a little trouble trying to do a search and replace using sed in a config file. I want to use multiple commands using the -e option to do the following. Step 1: find the first occurrence ...
1
vote
3answers
86 views

How to search multiple search patterns from a file with grep

If I search for multiple search strings in grep : usually just do: grep "search1\|search2" somefolder/*.txt but, what if I have 100 or more search strings? Can I say like this: grep "stringPattern....