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

learn more… | top users | synonyms (1)

6
votes
2answers
439 views

Is sed \{x,y\} range greedy?

I would like to know if, for instance, \{x,y\} in sed will try to match as much or as little as possible characters. Also, can someone explain to me the bellow unexpected behaviour of sed? echo "...
1
vote
3answers
48 views

How to grep a heading in a manpage containing control characters

I have a manpage, and I want to find something in a specific section. Grep is having a hard time, and I figured out the issue was headings have control characters in them, e.g. SEE ALSO is really: ...
1
vote
1answer
62 views

REGEX and GREP — Search string including optional and static words

I am trying to formulate a regex expression for use with grep such that I find a static word present at the beginning of each line, followed by only one word from a list of options. That is, I ...
2
votes
1answer
36 views

Regex to match = followed by a newline so they both be deleted

I am working with the raw source content of a mail.app message in OSX, but results it gives me the text in quoted printable MIME Email encoding. so I need to remove all those strange characters to get ...
2
votes
1answer
67 views

How to use BRE or ERE regex to match a pattern which does not have a specific string

How to use a posix BRE or a posix ERE regex to match a line that doesn't contain 834 at any place in string? I want to find grep '!<pattern>' file which will be equal to grep -v '<pattern>...
2
votes
6answers
499 views

Extracting IP address from a text and store it in a variable

I have a text file named abd shown below. 48878 128.206.6.136 34782 128.206.6.137 12817 23.234.22.106 I want to extract only IP address from the text and store it in a variable and use for other ...
1
vote
2answers
91 views

bash regex: asterisk metacharacter kills redundant newline characters

To test the basic functioning of the asterisk metacharacter in search operations using grep, I used a file (regex.txt) with the following contents: $ cat regex.txt 1 11 111 1111 11111 $ There ...
1
vote
3answers
133 views

How to grep all words starting with digit?

I am wondering how to grep all words starting with digit. So far, I have made this: egrep "^[[:digit:]]" tst But it prints me just a lines starting with digit. Thanks in advance with help.
2
votes
1answer
331 views

shell: The =~ operator does not work as expected

Consider the following script: #! /bin/bash line="confusing" if [[ $line =~ [[:lower:]]* ]] then echo "matches!" else echo "does not match." fi While the above produce the expected result,...
2
votes
2answers
77 views

How to escape metacharacters for egrep like metaquote from Perl?

Perl has a function called metaquote() to escape all special characters for a regular expression. Is there an equivalent technique for egrep? Example: If I am searching for the string abc.def.ghi, I ...
2
votes
2answers
94 views

Replacing matching text after two matching lines

I have a YAML file which includes a stanza looks this: admin::common::passwords: alice: password: '$6$oTQhLvN/4VFJPscD$8LYwUMSFi' bob: password: '$6$JKOtLF0wHeZfIskt$W/M5.ugDS' If the ...
1
vote
2answers
52 views

Help with regular expression in grep

someone can explain me why this regexp doesn't work in grep? ls -la ./ | grep -E '^d.*\<\..*\>$' In my (wrong) opinion this have to show me the line of the directory with a name that start ...
1
vote
2answers
125 views

Find works fine in command line, but won't work in script

I am trying to write a script on a Debian Jessie host. I am having an issue with trying to run the following command (with a twist) in a script: find ~/* -path ~/FileSniper* \ -prune -o \ -type f \( ...
2
votes
2answers
166 views

Delete row if two patterns are found in the same line

I'd like to delete a line from a text file (input.txt) if two patterns (string1and string2) are found on the same row, using sed. I'm trying: sed -i "/\b\(string1\|string2\)\b/d" input.txt, but ...
1
vote
2answers
53 views

Regex on Postfix using Centos

I have a Centos server running only postfix. I want to filter out mails containing word gratis or grátis or grétis or grâtis. The second letter is the problem. So I used this rejex: /^Subject:.*gr....
2
votes
4answers
52 views

How to append a line that does not start with '<' to the previous line

I have a log which contains XML lines. Sample format below: <head> <body> <line> asdasd</line> </body> </head> I want to scan the log file and ...
1
vote
2answers
34 views

Java comments regex: egrep “(/\*\* | /* | \*/ | \*\*/)” text.txt

I'm trying to extract lines starting or ending a Java comment: What I have is: egrep "(/** | /* | */ | **/)" text.txt I noticed that this works for all lines (such as /* comment */) except ...
0
votes
2answers
79 views

Problems with regex in grep

I'm trying to remove malware code from a site using a script, I'm having trouble getting the regex right for the malware part. What I want to do eventually is to use sed in order to find the ...
2
votes
1answer
90 views

sed regex for capture group between delimeters

I have a two line file that I'm trying to get some info out of for a bash script using sed. # File Comment PrefixForInformation {information to be captured} I need to get the information between ...
0
votes
0answers
16 views

UnionFS with filtering to determine default save location

I'm wanting to set up a UnionFS mount which will save all files/directories matching a certain regexp (or similar pattern-matching system) to one location and all other files/directories to another ...
1
vote
4answers
109 views

Grep for a number in line

Can someone help me to get the below two outputs? Input /usr/IBM/WebSphere/7.0/UpdateInstaller/java/jre/bin/java -Declipse.security -Dwas.status.socket=48101 /home/axg009/usr/IBM/WebSphere/8.5/...
2
votes
1answer
95 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
55 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
96 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
680 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
221 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
231 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
225 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
171 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
133 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
69 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
44 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
199 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
59 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
88 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
147 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
217 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
128 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
62 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
117 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
111 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
74 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
161 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
59 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
277 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 ...