Questions tagged [regular-expression]

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

Filter by
Sorted by
Tagged with
0
votes
1answer
28 views

Regex matching multi line search

I have this kind of text file that I want to parse: Host server1 option1 value1 option2 value2 option3 value3 ... Host server2 option1 value1 option2 value2 option3 ...
8
votes
2answers
127 views

bash regex multiple matches

I have a nice little bash script which parses a message for a regular expression and does something with the capture groups: regex='\((Closes|Resolves):\s([0-9]+)\)' msg='Fixed a problem (Closes: 1234)...
-1
votes
1answer
47 views

How to move the only file in every subdirectory to outside and change the name to the directory name?

I have a large number of directories, each containing a file. The structure looks like this: . ├── 1.apple │ └── foo.txt ├── 10.banana │ └── foo.txt ├── 101.orange │ └── foo.txt ├── <number&...
0
votes
0answers
21 views

Multiple expressions in “sed” --> -e expression #1, char 27: unterminated `s' command [closed]

I have this command: sed \ -e "s|{{VAR1}}|$HOME" \ -e "s|{{VAR2}}|666" \ -e "s|{{VAR3}}|fdsfdsafds" \ -e "s|{{VAR4}}|88jhgjh1111" \ ...
-1
votes
3answers
18 views

List All Files that Match a Particular RegExp Along with the Number of Matches

I want to search a directory (and any subdirectories) for files whose content match a particular regular expression and, for each file that matched, I'd like to list the number of times that the ...
0
votes
1answer
23 views

How to properly pack regex that contains both double as well as single quotes into a variable and evaluate it in Bash?

This stackoverflow question gives the following regex for RFC5322 compliant e-mail format matching: (?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\...
0
votes
0answers
11 views

for below Variable output need a regex or linux and windows command to display only specific text which is after 'host' and before 'is' [duplicate]

KVM05786Y: The Communication host ADMK05786HTREDIRECT_JA3A is SUCESS KVM05786Y: The Communication host KOPYUTRCEPORT_JA3A is SUCESS KVM05786Y: The Communication host ADMK05786HTSRM_JA3A is SUCESS ...
-1
votes
2answers
64 views

Help me to get command or ansibile Regex to fetch below details

My data: KVM05786Y: The Communication host "ADMK05786HTREDIRECT_JA3A" is SUCESS KVM05786Y: The Communication host "KOPYUTRCEPORT_JA3A" is SUCESS KVM05786Y: The Communication host &...
0
votes
2answers
43 views

awk paragraph with string and the following paragraph in pgn chess games

How can I get a paragraph with the string and the following paragraph with awk? The file looks like this: [Event "Chess game"] [Date "2021.01.08"] [White "Player A"] [...
-1
votes
2answers
50 views

How to match regular expression for below string in bash?

Below string I need to compare conditional operator. So only if 81.1.4_r4345 doing some operation or else matching with “done” then execute some operation. vasanth-releases_81.1.4_r4345.tar.gz ...
0
votes
1answer
44 views

How to use BASH variable for AWK filtering results

I have this script, I'm getting first letter of my directory name, and I wan't to get directory names that start with the same letter and write to textfile. The for loop is looping list of directory ...
0
votes
3answers
54 views

sed replace characters in capture group

I've got this, which almost does what I want git show-branch --current $SHA1 \ | sed --quiet --regexp-extended \ 's/^.*\* \[[a-z]+\/(B-[0-9]+)-([a-z0-9-]+)\].*/\1 \2/p' \ | sed --quiet 2p and ...
0
votes
2answers
41 views

Regex that grep numbers after specific string in multiple files and return average

I would like to return the average of all numbers after a specific string in multiple files i.e. We are given 10 files (file1.txt,...,file10.txt). Each file contains among others Test1: Avg. length 24....
0
votes
0answers
28 views

Regexp with new lines in bash and Perl (and /s modifier)

Given this code cat test `asdasdads adsasd` `adsajdakjsnd` I'd expect this perl -pe 's/`(.*)`//s' < test To return blank. However, it returns `asdasdads adsasd` Therefore, the \n in the string ...
-1
votes
3answers
24 views

extract substring of string

How to extract 3ccc2392-45ed-11e7-bc98-4061862b8d34:1-35942477 from the following string? mysql-bin.000025 217141189 3ccc2392-45ed-11e7-bc98-4061862b8d34:1-35942477, It is fetched via line=$(...
0
votes
3answers
48 views

sed portability: extended regex vs. backslash

We can write the next command in two ways: # using extended regex $ echo foobar | sed -E 's/(foo)(bar)/\2\1/' barfoo And: # using backslashes $ echo foobar | sed 's/\(foo\)\(bar\)/\2\1/' barfoo ...
1
vote
1answer
46 views

Difference $1 and $& Perl if only one capture group was found

From my understanding, $& is the last found capture group. If there is only one capture group, why don't return both variables the same? E.g.: $ echo "key: value" | perl -ne "s/([a-...
0
votes
1answer
87 views

Fail2Ban Regex - NGNIX - Custom Regex

I am trying to create a custom log file for fail2ban NGNIX logs to catch a specific event. Regex is still relatively new to me so im trying to improve. This will be put into a custom filter but for ...
4
votes
4answers
309 views

grep string between backtick

`someString` complete(1) I need "someString" that is in between backticks. tried below but doesn't work. cat file.txt | grep '\`.*\`'
1
vote
1answer
25 views

How do I pdfgrep using a specific pattern (Syntax?)

I'm trying to use pdfgrep to search each occurences of a specific pattern (MUST start with E OR S) then followed by 5 digits (Only) THEN execute a command afterward (Which is likely to be a mv command)...
6
votes
3answers
184 views

Replace to first matching character using sed

Trying to update some latex code. The four lines: something $^\text{TT}$ and more stuff something $^\text{T}$ and more stuff something $^\text{M\times N}$ and more stuff something $^\text{T} {...
0
votes
1answer
79 views

Find recursively all files whose content match a specific regular expression

I like to search all PHP files and find a particular string that is identified by a regular expression. The regular expressions that I use to find the string is: \$[a-zA-Z0-9]{5,8}\s\=\s.{30,50}\;\$[a-...
0
votes
3answers
163 views

Use the 'sed' command to find a pattern and then replace everything after that using Python

I have a file like this on my Linux system, and I want it changed to the output below using a Python script. 1_file_name=xyz.vps: input0_bpp=8 input0_is_padding_enable=0 input0_task0_file_name=${...
0
votes
3answers
34 views

grep with negation in pattern

How to use grep (or another tool) to match with negation parts in pattern? example php cronjob.php test php cronjob.php scan_document php cronjob.php scan_document -v php cronjob.php scan_documenttest ...
0
votes
2answers
40 views

Bash: String substitution/shorten of string with RegEx (RegEx Fail)

I would like to shorten strings with Bash. Unfortunately this does not work as planned. I would like to have only the front part with the letters. It has unfortunately one of the variants below still ...
0
votes
1answer
38 views

Moving group of files to another directory using the command “rename” with regular expressions

I have some markdown files that I want to move to a different directory called "newDirectory2" within "newDirectory1". These files contain the string "abc" or "ABC&...
0
votes
1answer
38 views

Putting single quote in the begining of column

I have a .txt file with publications for which columns are separated by a single space. However, titles also have spaces an in order to separate the columns correctly I need to have all titles in ...
-2
votes
1answer
49 views

bash + Compare Strings with regex

in my bash script I want to verify if string is a disk so I did the following example if [[ $disk =~ sd{a..z} ]];then echo "we found a disk - $disk . . . . fi but actually the expression not ...
0
votes
3answers
48 views

grep: start matching line at given position (skip first n characters)

I have file containing lines in following format: 2021-Jan-26 05:35 foo bar 2 2021-Jan-26 05:37 asdf 2 foo bar 2021-Jan-26 05:37 foo The patterns that I am interested start at position 20. So when ...
0
votes
0answers
8 views

PHP logs for last 1 minute

I need last 1 minute logs of PHP-fpm for automating alert generation and to use webhooks on other applications. I have used below mentioned command awk -v d1="$(date --date '-60 min' '+%d-%b-%Y %...
-3
votes
2answers
59 views

What is the difference between [0-9]{3} and [0-9]<space>{3} in RegEx?

I have a file - cat regex30.txt (914).582.3013 (873).334.2589 (521).589.3147 (625).235.3698 (895).568.2145 (745).256.3369 I want to convert this to 914.582.3013 873.334.2589 521.589.3147 625.235....
1
vote
1answer
240 views

grep: use square brackets to match specific characters

So I am experimenting with the power of grep using this resources The problem I am currently encountering is that it doesn't seem to work as I intended. so I have an demo.txt file that contains foo....
0
votes
1answer
52 views

How to replace special characters in BibTeX (e.g. {\'{o}} = ó) within a shell script?

Does anyone know of any utilities or have experience converting special characters in a BibTeX file to its proper unicode equivalent in a bash script? Trouble is I can't even find a full list of the ...
0
votes
1answer
36 views

GNU tar's “transform” option behaves inconsinstently when pattern ends in “/”

I have come across the following strange behavior of GNU tar when using the --transform option to transform path elements: When I try to tar an entire sub-directory and want to transform the path to ...
0
votes
0answers
54 views

Why '$' in regex isn't working as expected in debian? [duplicate]

I have the following file: cat regex16.txt gives foo bar baz bar foo baz baz foo bar bar baz foo foo baz bar baz bar foo I want to grep only the lines which end with bar. So, ...
0
votes
1answer
24 views

grep unexpected behavior when [0-9] pattern is given on ubuntu [duplicate]

When I use [0-9] pattern to search for all numbers in a file, whether to use or not use single/double quotes around the pattern produces different outputs. $ cat numbers this line has 3 this line has ...
-1
votes
1answer
39 views

Select a particular portion from a html dump using regex based on a criteria using Unix commands

I am stuck with a question where I am supposed to display only those commands with a number of votes >= 1 from a dump of page http://www.commandlinefu.com using Unix commands. I am attaching a ...
0
votes
1answer
38 views

Bash move files matching a pattern to new folder

I know there are similar questions to mine but I don't understand regular expressions at all so here's my new question: I have a bunch of files in a folder. I need to move some files into another ...
0
votes
4answers
64 views

Grep for only binary (0,1) numbers in a file with base 10 numbers

So let's say i have a file with both binary numbers (0,1) and some other numbers in base 10 (0-9): 010... 10567 011... 23678 ... etc. the above example isn't actually their exact representation but ...
0
votes
1answer
21 views

Proper Syntax for writing all output of a command using stdin, not just the output of the command

I am trying to send the output of a curl request to a file using regular-expression. curl -k -v -x x.x.x.x:x https://repo.nagios.com/nagios/7/repodata/repomd.xml > curl1 Using the above syntax only ...
2
votes
2answers
49 views

show a list of files with a certain pattern

I have a lot of files named either one of these patterns: 1 Surname, Name - Title. Subtitle.extension 2 Surname, N. Surname, N. - Title. Subtitle.extension 3 VV. AA. - Title. Subtitle....
3
votes
1answer
37 views

grep displaying also results which are 1 character off?

I often have a typo in my grep searches, especially when it comes to long names. So for instance, grep "super_duper_utralong_name" *txt would normally not contain super_duper_ultralong_name ...
1
vote
0answers
29 views

Match multiple expressions in different lines with a logical AND using grep - all expressions must occur in the same textfile [duplicate]

I tried to return all python-scripts (textfiles) which contained both main() and plt.style.use somewhere within the file, i.e. not in the same line. I found some documentations like this explaining ...
1
vote
2answers
106 views

Replace several consecutive lines with a single line using sed

In all my python scripts, I'd like to replace the 2 consecutive lines import matplotlib as mpl mpl.style.use(mpl_plt_default_template) with just the line plt.style.use(mpl_plt_default_template) So ...
-1
votes
1answer
27 views

Using apt search to find a string regardless of positioning

Here is the pseudo code for what I would like to achieve: sudo apt search *something* E: Regex compilation error (error message for trying) The idea is to search for the string regardless of the ...
4
votes
5answers
160 views

sed: match special color characters

I have a file containing special color encoding characters: $ cat zz aaa.gpg bbb.gpg ccc.gpg $ cat -A zz ^[[38;5;216maaa.gpg^[[00m$ ^[[38;5;216mbbb.gpg^[[00m$ ^[[38;5;216mccc.gpg^[[00m$ I need to ...
0
votes
1answer
36 views

Prepend two lines containing comments and URLs before specific line in python-scripts using sed

In some of my python-scripts, I've put the line if __name__ == '__main__' in order to make the content only be executed, when the script is run itself as main-script (as opposed to being imported, see ...
0
votes
0answers
24 views

Change DISPLAY to remote host

I would like to replace the IP in $DISPLAY to the remote host IP ($SSH_CLIENT or $SSH_CONNECTION) $(echo $DISPLAY | sed -e "s/^[^:]*\(.*\)/$(echo $SSH_CLIENT | awk '{ print $1}')\1/") I did ...
0
votes
1answer
22 views

What is the correct grep regex-string for searching any words after a left-parenthesis starting with a specific letter?

In some of my python-scripts, I got glob.glob(parent_folder_path + glob_str). These lines with the associated files shall be printed out in the console with a proper grep command, but so far I couldn'...
-1
votes
2answers
64 views

How to remove a text followed by backslash in file using sed or regex pattern

I am having the below contents in a file, I have to remove the text "monitoring" along with the backslash at suffix (monitoring/) <base href="/monitoring/" /> so the ...

1
2 3 4 5
47