Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [regular-expression]

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

0
votes
1answer
47 views

Grep not understanding whitespace delimiter?

In the following example: apt-file search apache2.conf | grep -E "apache2.conf\b" Output: apache2: /etc/apache2/apache2.conf apache2-doc: /usr/share/doc/apache2-doc/examples/apache2/apache2.conf.gz ...
1
vote
3answers
407 views

Using sed to substitute a string containing variable integers

I have a text file and want to use sed to replace the following string (including the "s): " rel="lightbox[1846]" title=" with # The numbers between [ and ] are variable and change. I want to ...
0
votes
2answers
185 views

Making a Procmail regex recipe for malicious email body text

At work we recently began receiving emails with what looks like malicious code contained in the email body. I'm trying to create a procmail recipe that will use regex to match only the non-word ...
0
votes
2answers
53 views

RegExp: Trying to understand it. Making a query

I was running a few tests and thought of one I can see myself using frequently. So I have a few files around that I would usually source as they are common in a lot of things that I would do. I was ...
1
vote
2answers
298 views

Overcoming newlines with grep -o

I have a file similar to such. SampleOutput { "1": { "state": "RUNNING", "stats": { "name": "some-file", "uris": [ "some-example.com" ], ...
-3
votes
2answers
44 views

Unix shell : substring with regular expression [on hold]

I have this text: 1234 I want to use a regex to select all except the first digit (in this case 1) - so a pattern that selects 234 How can I do this? I can't use variables, only regex. And also I ...
0
votes
4answers
56 views

How to extract the CSV file name from each line of this file

I have a file which contains lines like those: -rw-rw-rw 1 root root 6379 May 24 2016 test1.CSV -rw-rw-rw 1 root root 23249 May 25 2016 test2.CSV -rw-rw-rw 1 root root 2995 May 26 2016 test3.CSV ...
3
votes
2answers
446 views

Looking for duplicate instances of a tag in a file

Mutliple snippets of code exist in a file similar to the following: <blah>Spread the peanut butter <ramout assot="f0123_fun10" bapel="2 or 6"/> on good looking bread <ramout assot="...
0
votes
0answers
32 views

sed command to insert between two strings that are returned from another sed command

I have a YAML file that's about 25K lines and I have about 450 instances where I need to insert between two specific lines. Here's an example of the file entries: - changeSet: id: ...
1
vote
2answers
30 views

Regular expressions in grep

The man page for grep reads (emphasis mine) REGULAR EXPRESSIONS A regular expression is a pattern that describes a set of strings. Regular expressions are constructed analogously to ...
0
votes
0answers
47 views

Complex string replace - multiple files, multiple different strings, must include certain text

I have a number of XML files (Atlassian Confluence space exports FYI), that I need to parse through to replace certain strings, as long as the line begins with a set string. Here is an example of a ...
4
votes
2answers
481 views

sed string with bad chars

I have a random string with ugly chars in it: ÓˇÌ„ˇ™ÌÓ‹ÍÙ› ‹„ı That chars have to be eliminated. The Whitelist consist of: a-zA-Z0-9 -_* + ß ä ü ö () % @ € & = . and Space My first Script ...
19
votes
5answers
16k views

How to select first occurrence between two patterns including them

How can I select first occurrence between two patterns including them. Preferably using sed or awk. I have: text something P1 something content1 content2 something P2 something text something P1 ...
555
votes
13answers
1.3m views

How do I grep for multiple patterns with pattern having a pipe character?

I want to find all lines in several files that match one of two patterns. I tried to find the patterns I'm looking for by typing grep (foo|bar) *.txt but the shell interprets the | as a pipe and ...
2
votes
2answers
11k views

sed: problem while replacing string using wildcard

I am trying to replace a string using the following command: sed -e "s/\<$5\>/$6/g" "$2/$1" > "$4/$3" $5-> "hostname=*" $6-> "hostname=int1" $2/$1-> "source file path" $4/$3-> "...
0
votes
3answers
5k views

Ignore lines containing a capital letter

Why does the following command still return some lines containing a capital letter? aspell dump master | grep ozz | grep -v '[^A-Z]'
2
votes
1answer
172 views

Regex in IF condition in awk

I have awk script file as below. I need to add another condition in the if statement to check if the string contains atleast one alphabet. How can I add the extra condition to the present if statement?...
-1
votes
1answer
30 views

\back slash charectar and . in grep

I am confused about the backslash character \ and single quotations inside grep. From my understanding it preserves the literal meaning. For example if I do echo 'This is \., it gives me This is \. ...
0
votes
2answers
34 views

grep STOP regex replacing -A option

I'm looking/searching for a way to make grep stop when finding the "|_" specific string after having found the search string. for example, i want to print ONLY the lines starting 2 lines above "...
3
votes
2answers
694 views

Merge two greps into single one

I need to extract an ID from the output of another command. Currently my extracting command looks like: someID=$(command | grep -oP '(?:^Successfully\sbuilt\s)([\da-z]{12}$)' | grep -oP '([a-z\d]{12})...
0
votes
2answers
115 views

Swap two strings separated by a symbol in multiple filenames of a directory

I want to change a string like author - name in some books' filenames to name - author. I'm trying ls * | sed -r 's/(.+) - (.+).pdf/mv \2 - \1.pdf/' | sh Maybe its a mix of Switch two arbitrary ...
3
votes
2answers
56 views

Using regular expressions in “less”

I'm trying to use a regular expression in the man page of Bash by using less. I press / in less to enter a pattern, and I type z and press the Enter. I expected it to not match upper-case z (Z), but ...
3
votes
0answers
44 views

Renaming files with vidir (moreutils)

I'm trying to use vidir from the moreutils package and I have the following issue: When I load the files I want to rename with vidir like this: $ vidir or like this: $ find -type f | vidir --...
3
votes
2answers
1k 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 the ...
0
votes
2answers
39 views

Need regex to replace the whole line, but the last optional character

It sounds like it should be trivial task, but I cannot figure it out, so I decided to ask. I need to replace the whole line, but optional last character. For instance, I want: abcd to become efgh but ...
0
votes
0answers
24 views

Rename multiple files, removing text before prefix [duplicate]

I have 70 ish csv files that look something like: C03-10dfg-PI-35.csv C03-fgfg-PI-36.cvs C03-dfdf-PI-37.csv .... .... .... I want to rename all files to remove everything before PI. Outputs: PI-35....
0
votes
3answers
50 views

In a bash shell script, how to rename multiple files, keeping only the numbers and the extension of the original file names?

I have a directory with a bunch of files with names like this: generic name- dd -01 _.txt generic name- dd -02 _.txt generic name- dd -03 _.txt generic name- dd -04 _.txt And I want to rename all of ...
-1
votes
2answers
2k views

Regular expression in unzip files argument not working

I'm trying to view the contents of a zip archive using an extremely simple regular expression. This works: rmorton@Rockette:~$ unzip -l Downloads/WeiDU-Linux-236.zip "*/i386/tolower" "*/i386/weidu" "*...
4
votes
2answers
94 views

When and why did ^ and $ take on their meanings of “beginning of line” and “end of line?”

Many command-line tools (grep, flex, etc.) use the ^ symbol to denote "beginning of line" and the $ symbol to denote "end of line." When did this convention arise? It seems perfectly reasonable to ...
1
vote
3answers
1k views

Recursively edit files based on parent directory's name

I have a large directory with hundreds of folders, all following the naming scheme "a_b_x", with x being a random word. In every one of these folders there are 3 text files, all with a couple of ...
3
votes
2answers
470 views

Find Files not an regex whitelist

I have a huge Folder with thousands of Files. Some Files have some Characters in it, which are not allowed. (UTF-8 signs) So I have a white list of allowed characters and a beginning of a bash-script ...
6
votes
8answers
2k views

Replace anything between parentheses even if spanning multiple lines

I would like to use bash or shell script and replace anything between the two parentheses with an empty space. The text between the two parentheses could be in multiple lines, such as: myFunction (...
1
vote
2answers
5k views

How can search for both single quotes and double quotes in a grep search?

When I do a search bindkey in the zsh plugins directory for key conflicts I get responses from both the .zsh script files and .md files, and some of the zsh readme files use a double quote in the ...
3
votes
1answer
875 views

Multiline grepping – What’s wrong with this expression?

Consider this sample file (line numbers are for reference only): 1 Reference duiarneutdigane uditraenturida enudtiar. 2 3 Reference uiae uiaetrtdnsu iatdne uiatrdenu diaren uidtae 4 on line 23. 5 6 ...
0
votes
4answers
82 views

How can I delete everything but an alphanumeric pattern?

I have a file that has a lot of junk and special characters as well. I want to keep a particular alphanumeric pattern and ignore everything else - e.g AB123456789 - I want to extract only this keyword ...
109
votes
5answers
119k views

How to use find command to search for multiple extensions

I can get all jpg images by using: find . -name "*.jpg" But how can I add png files to the results as well?
-2
votes
1answer
53 views

How to match string in file

I'm trying to reproduce this code in Python to Bash import re w = open("filetest.txt") for item in re.findall(r'STRING:\s*(.+)"', w.read()): print item In Bash, but I do not know if it's correct, ...
0
votes
3answers
63 views

How to test if a folder exists using regular expression in Bash?

as all know we can check easily if folder exists by the following bash command [[ -d /opt/presto-server ]] && echo folder exist but when we have folder as /opt/presto-server-32.34 then how ...
-2
votes
1answer
41 views

grep to find lines that contain six or more non-vowel characters in a row

Using grep and regular expressions, I need to output all lines in a file that contain at least six characters in a row that aren't vowels. This includes any consonants, punctuation, numbers, etc.
1
vote
2answers
51 views

append brackets to all constants beginning with L:: where it not exist on end

I have many PHP Files where I need to add on end "()" to every constant beginning with "L::" which don't have on end "(". It should by applied to all *.php including subfolders) : L::WHAT_EVER <?=...
2
votes
1answer
35 views

Recursively find directories but do not print subdirectories of matches

Considering the following directory structure: dir1 [domain.com] foo dir1 subdir1 subdir2 FooFile dir2 [domain.com] foo dir2 subdir3 [domain.com] foo dir3 subdir4 ...
0
votes
2answers
79 views

how to deal with sed if Argument list too long

Trying to apply the following command: sudo sed -i "s/auth-user-pass/auth-user-pass pass.txt/g" *.ovpn But i keep getting the following error: sudo: unable to execute /bin/sed: Argument list too ...
0
votes
4answers
51 views

how to let part of the regex (that is variable) be matched literally, ignoring control characters?

I got some filenames containing characters that are regex special control ones. I need to prepare a regex that considers all these characters literally. Simplified test case: strFilenameOnDB="...
-2
votes
1answer
41 views

Is there any initiative to transition all shell globs into standard regex?

I see many imagination between shell globs (wildcards) syntax and standard regex. Is there (or was there) any initiative to transition all shell globs into standard regex? By that I mean, using ...
-1
votes
3answers
70 views

What does the following number-processing sed code mean?

I came across a code in script which add ',' after every 3 digits from backwards. the code only considers numeric data. Following is the code. sed 's/\(^\|[^0-9.]\)\([0-9]\+\)\([0-9]\{3\}\)/\1\2,\3/...
7
votes
2answers
5k views

Pattern Matching Exclude Duplicate Characters

Is there a regular expression for the following that matches characters in a character set but only once? In other words, once a character is found, remove it from the set. If grep cannot do this, ...
3
votes
1answer
331 views

Simple Bash regular expression match missing char (OS X)

I am matching a regular expression in bash on OS X 10.11: $ msg='[4] the message' $ [[ $msg =~ (.+)\t(.+) ]] $ echo ${BASH_REMATCH[1]} [4] $ echo ${BASH_REMATCH[2]} he message Between the ']' and ...
-1
votes
1answer
53 views

How to grep for a particular string?

I am trying to get a string that matches the following pattern: starts with foo and finishes with bar. grep -e "^foo*bar$"
1
vote
1answer
912 views

LFTP exclude file extensions

I am trying to mirror directories with lftp but I don't want to download filetypes that are notoriously large like .mp4 and .swf. But I am having trouble with the regex - and seeming like the exclude-...
4
votes
3answers
291 views

Extract substring according to regexp with sed or grep

In a (BSD) UNIX environment, I would like to capture a specific substring using a regular expression. Assume that the dmesg command output would include the following line: pass2: <Marvell ...