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 [regex]

Regex (or regexp) is known as regular expression matching of patterns, strings or characters in, for example, a large text file. Questions should be tagged as such whatever the programming language involved and the tag can also apply to command-line or graphical programs that have regex plugins or some regex capability.

6
votes
1answer
378 views

RegEx with \d doesn’t work in if-else statement with [[

i wrote the following script. It will be used in a build process later. My goal is to decide whether it's a pre release or a release. To archive this i compare $release to a RegEx. If my RegEx ...
1
vote
1answer
69 views

How remove repetition of content?

I have a file psychrobacter_aquaticus.txt contain the following content: WP_021815421.1 OG5_132922 deth|YP_182312 3 -07 42.35 81.7 WP_021815427.1 OG5_128110 rbal|NP_868155 1 -55 50.72 ...
0
votes
0answers
88 views

Regex for fail2ban config file to match pregreet lines in mail.log

The lines in mail.log to match are like: May 6 10:06:16 mikes-serverbox postfix/postscreen[15486]: PREGREET 14 after 0.1 from [173.225.99.104]:57130: EHLO ylmf-pc\r\n May 9 11:36:36 mikes-...
1
vote
1answer
37 views

How to replace and edit content of file?

I have a file which contains the following content: set0027: set0027_100-250 set0027_1-150 set0027_50-200 set0038: set0038_100-250 set0038_1-150 set0038_50-200 I want to modify it in the following ...
1
vote
1answer
31 views

Trouble getting rename working with regex groups

Folder of files that need to be changed to YYYY-MM-DD.pdf but currently are MMDDYYYY.pdf This is what I came up with but it's not working rename -nv 's/^(\d{2})(\d{2})(\d{4}).*/$3-$1-$2.pdf/' *.pdf
0
votes
1answer
46 views

E: Unable to locate package regex

I'm trying to use the regex package in my python script, using: import regex as re and the package is missing: $ ./lula2.py Traceback (most recent call last): File "./lula2.py", line 13, in <...
-1
votes
1answer
64 views

Write a regular expression which catches user $HOME in shellscripting

I'd like to write a regex in shellscript which would compare the user input path to user$HOME. Then it would raise an error in case the user input path contains the user $HOME. The home folder ...
1
vote
1answer
77 views

Negative to positive number range in regex in bash scripting

I am trying to make a regex expression that will validate a number that is in the range of -100 to 100. I made this regex expression: (^-[1-100]* |^[0-100]*) but it doesn't work as expected. I am ...
4
votes
2answers
279 views

Extracting names from filename in Bash

I have a directory filled with thousands of files in the format LastnameFirstnameYYYYMMDD.pdf. The last and first name will always been in title case. I'd like to extract the last name so I can move ...
0
votes
2answers
34 views

RegEx - How to extract second matches using grep

How to extract second numbers in this text using grep command in ubuntu. filename : Results.html?owner=12345 14219,98.2,31-s,19225 39219,92.7,31-s,29225 ouput desired : 12345:98.2 12345:92.7 ...
1
vote
1answer
75 views

grep doesn't work as expected

I'm trying to understand how grep deciding to take $ as regex, or as a searchable character. My results show that grep is inconsistently deciding what $ means. As such, I made a basic example: $ ...
1
vote
1answer
52 views

How can I grep patterns only inside files with a specific file name?

I spent plenty of time searching for an existing question about my issue but I falied to find any solution for this case in specific. I apologize in advance if there is an existing thread/question ...
0
votes
3answers
60 views

What is the correct way to format this grep?

I am having trouble figuring out where to place the \ in this command. grep "^D.*\(A1|A2|A3\)$" input.txt > output.txt I'm searching for each line that starts with a D and ends with A1, A2, or ...
0
votes
1answer
45 views

Gnu sed regular expression extension, how to type these two characters?

This may be a stupid question, but I'm not sure and just want to confirm. Gnu sed has some regular expression extensions that work in both BRE and ERE mode. The following are an excerpt from the sed ...
2
votes
1answer
46 views

how to create a regex string where you don't know what to escape

I have a passwd file with the line containing root being: root:6UZSjeWUui3JQ:0:0:root:/root:/bin/sh I want this line to be changed to: root:$1$dog$cNv/OuAd7CMNdrhWsHXAR.:0:0:root:/root:/bin/sh ...
0
votes
1answer
54 views

How to verify the position of characters from the column? [closed]

I have a column MBI contains 11 characters. Out of these 11 characters: 2nd, 5th, 8th, and 9th characters will always be a letter. Characters 1, 4, 7, 10, and 11 will always be a number. The 3rd and ...
0
votes
2answers
140 views

Regex “.*?” giving same output as “.*”

This the file I am using: Shane is a little to play ball small silly When I use egrep 's.*l' new I am getting the output as Shane is a little to play ball small silly When I use egrep 's.*?l' ...
1
vote
1answer
65 views

Regexxer stopped working - it doesn't find the string

Regexxer is a brilliant search-replace tool for multiple files, but suddenly it stopped working August 10. I posted this on the Regexxer bug page: I used this brilliant program less than 20 hours ...
0
votes
1answer
59 views

Bash case match trouble with mixed numbers and letters

I am trying to allow a user to enter in a size that will be used in fallocate, so I need to make sure text is entered as 1G, 8G, 512M, etc.. I have the following code below; however, all correct ...
0
votes
1answer
42 views

Need help on an egrep regex

I am trying to create an egrep command to grep the number of lines containing a specific text from a text-file but seem to have an error somewhere. The text-file contains several thousand lines and ...
4
votes
2answers
821 views

sed with PCRE (like grep -P)

I am happy that grep does support Perl Compatible Regular Expressions with the -P option. Is there a reason why the tool sed does not have this feature?
1
vote
1answer
1k views

How to filter a regex (exclude special characters)

Let's say that I need to filter all my files' names that don't include special characters. ASDA123fasf - would pass asasdasd*dasd - wouldn't pass
1
vote
1answer
3k views

How to find a line starting with 'a' and ending with 'c' using grep?

I have a file named newfile. The contents of this file are: abc xyz abc Now I want to find lines which start with a and end with c. I enter the following command but the results are not the same as ...
6
votes
2answers
196 views

How do I search for lines in a file that only contain ASCII characters and then act on them?

I have a text file that looks like this: English words only English and 日本語 日本語のみ English words only English and 日本語 日本語のみ English words only Also English words only English and 日本語 日本語のみ English ...
1
vote
1answer
35 views

Replace only characters after symbol and before another?

I'm having real difficulty with this. Apologies for the noobish question. Lines grepped out yields this: 192.168.1.234/50112(LOCAL\username) Desired output: 192.168.1.234-(LOCAL\username) I know ...
0
votes
0answers
25 views

Getting only specific pattern using locate [duplicate]

I tried: $ locate 'tar' | head -2 /bin/tar /bin/unicode_start How I can get rid of this 'unicode_start'?
0
votes
3answers
107 views

Remove files with specific extensions by regexp

I have Makefile for compiling LaTeX *.tex file (tex->dvi->ps->pdf). Then I have command clean for removing unnecessary temporary files (dvi, ps...) like this: rm `ls | grep "^${FILENAME}\.(aux|dvi|ps|...
0
votes
1answer
508 views

Rename file using regex

I've little issues trying to change the name of several amount file names using rename command. I have 1 main directoy, and so many others inside, after this, the whole content are .wav files. /...
3
votes
2answers
1k views

Why am I getting the error “missing terminating ] for character class”?

I want to cut a string with brackets using sed. How to avoid an error if I want to drop a string with [? For example: $ echo '[Om/mystring' | grep -oP '(?<=[Om\\)\w+' grep: missing terminating ] ...
0
votes
1answer
908 views

how to use regular expression with iptables

I am trying to do a routing rule to a bunch a subdomains matched by regular expression. iptables -t nat -A PREROUTING -m string --string "/must-[a-z0-9-\.]+gonow[a-z\.]+/g" --algo regex -p tcp -m ...
0
votes
1answer
575 views

Find and Grep working properly via prompt but not via Jenkins

I´m trying to filter some files using find and grep via the command below: find . -type f -exec grep -q --exclude=*.{cpp,gradle,java,ttf,txt,xml,md} . {} \; -and -print The funny thing is: via ...
0
votes
1answer
433 views

regular expression that would catch 2 word in a string

I am trying to come up with a regular expression that would catch this string - must-c046-was001-ix.1.oca.best.net The strings will always contain must and best. Any suggestions ?
3
votes
1answer
841 views

Replace all lines between two patterns (inclusive) with a file content

All is said in the title , I want to replace all lines between two patterns with a a file content. file1 line 1 line 2 foo foobar bar line 6 line 7 file2 line 3 line 4 line 5 desired file line 1 ...
0
votes
0answers
50 views

Regex pwd for regular expression [duplicate]

How do I create a password that satisfies a regular expression in Ubuntu? The expression is: ([A-Za-z]+).[^&%$#@!]+\d+\1\D+
2
votes
2answers
6k views

How to check for a matching pattern in bash?

I have a file in which value starts with ABC or ABD. After fetching the value from input file how to check it starts with ABC or ABD? eg: value=$(cat file | awk 'NF{print;exit}' | awk '{print $1}')...
1
vote
1answer
434 views

Regex to find files with certain number of underscores

I have two files: AAAAA_AAAA_BBBBBBBB_SSSSSS AAAAA_BBBBBBBB_SSSSSS I want only the file with two underscores be shown. I used: ls -1 ^?*_?*_?*$ I got: ls: ?*_?*_?*$: No such file or directory ...
0
votes
1answer
446 views

Rename file using regex (Linux Command line)

I have a backup script which is creating two files: a) db_mywebsite_2018-01-10_00-25.sql.gz b) mywebsite1_2018-01-10_00-01.tar.gz I'm looking for a Linux command with regex to rename both files to ...
2
votes
1answer
708 views

Why is the \d metacharacter not working in the find command with -regextype posix-egrep active?

I have a list of .jpg files in my current directory with the following paths: ./ex.jpg ./ex1234.jpg ./ex4321.jpg And I am trying to match only those that contain numbers with regular expressions and ...
1
vote
1answer
5k views

How to use grep to match lines where the first character falls in a range?

I am trying to grep lines where the first character is an A, B, or C. I am trying this: grep -i "^[a-c]*" data.txt I want it to only care about the very first character, the rest of the line I don'...
9
votes
1answer
356 views

Grep -E, Sed -E - low performance when '[x]{1,9999}' is used, but why?

When grep or sed are used with the option --extended-regexp and the pattern {1,9999} is a part of the regexp that is used, the performance of these commands become low. To be more clear, below are ...
4
votes
2answers
1k views

Asterisk not working with grep

I searched for ki with * as per the example below and it should have returned the first three lines. I am not sure why it returns the last line when there is no ki matching it. $ grep "ki*" ...
-2
votes
1answer
140 views

Editor with python compatible regular expressions [duplicate]

I am searching an editor which supports the regex syntax like python does. I want a GUI like gedit. The old school editors vi, vim, emacs are no solution, since the editor should be easy to use. I ...
9
votes
1answer
34k views

How to include a space character with grep?

I have a file named example $ cat example kali.pdf linux.pdf ubuntu.pdf example.pdf. this.pdf grep .pdf and when I use grep to get the line that has a space before .pdf, I can't seem to get it. ...
4
votes
4answers
8k views

Why is sed not working?

I have some HTML that I am trying to extract links from. Right now the file looks like this. website.com/path/to/file/234432517.gif" width="620"> website.com/path/to/file/143743e53.gif" width="620"...
1
vote
2answers
14k views

grep pattern with leading spaces

I need some help in setting the correct pattern for grep. I need to find all occurrences of pattern where line may have leading space(s). For example: In the following file: 1. No pattern ...
1
vote
2answers
3k views

How to extract only 7 characters using grep

I am using a regular expression with grep. I want to extract exactly 7 character passwords, but I am getting 7 and more than 7 characters as a result. grep '[a-zA-Z0-9]\{7\}' /usr/share/wordlists/...
-1
votes
1answer
1k views

Regex to find word 1 or word 2 but not word 3

I have the following message: Apr 12 12:48:02 poe true: sudo: nagios : command not allowed ; TTY=unknown ; PWD=/var/log/nagios; How do I make a regex that looks for the word Sudo or Command but ...
1
vote
1answer
46 views

creating directory for group of files that have a common string in their names

I have lots of files all of which are similar to the following: frame1_M.png frame1_S.png frame1_L.png frame1_N.png frame1_D.png frame1_M.png frame1_S.png frame1_L.png frame1_N.png frame1_D.png ...
3
votes
1answer
1k views

Using grep to find all substrings of strings that may contain special characters

I am trying to use grep to find the string "crk" in a file. I know the string exists in a c source file in the form kop.crk_op = CRK_MOD_EXP; However, when I search using grep -rnw --include '*.c' ...
2
votes
2answers
335 views

How do I generate a match for a regex?

Is there a tool out there that would take in a regex argument and spit out a valid combination? It should make a string that matches the regex. For example: clear clear ls (-[al]{2}|-a -l|-l -a) ...