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.

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

Itemized Meaning of the RegEx: “(/.*)?”

I am trying to determine each element of this regular expression: "(/.*)?" Here is what I have learned on my own so far: () == grouping regex together / == ??? . == equal to any one ...
0
votes
0answers
40 views

batch renaming all files in a folder to lowercase letters regex not working [duplicate]

I need to rename all folders and files to lowercase, I looked on this forum and found the following command find . -name * -type f -exec rename 'y/A-Z/a-z/' '{}' \; I tried this, and while the ...
2
votes
2answers
256 views

replace text with part of text using regex with bash perl

For example I have this output: string1 anynameveryveryverylong string2 string1 othernameveryveryverylong string2 I want truncate the name to the first ten characters: string1 anynamever string2 ...
0
votes
0answers
35 views

A lot of errors while reinstalling MongoDB [duplicate]

I can't use MongoDB so I need to uninstall, but when I try to reinstall it, the program shows me a lot of errors in MongoDB, MySQL and also in dpkg. sudo apt-get purge mongodb-org* Reading package ...
3
votes
3answers
179 views

bash perl \z doesn't work

I have this text file or sdout: text1 text2 text1 text2 text1 text2 I have this code: perl -pe "s/text2\n\z/text3/s" text.txt #Note the modifier "/s" I with \z expect him to ...
0
votes
2answers
55 views

Bash command to recognize difference between variables

I am given from stdin 2 variables a name and a number given in any order. How can I identify them correctly in a bash script such that I can print them in a file in the same order(age, Name) ./a.sh ...
0
votes
0answers
72 views

ignore python cache in Unison: ignore Path */__pycache__

Is ignore Path */__pycache__ the correct approach to ignore all python cache files from being synced with Unison? I do so by adding Path */__pycache__ in the graphical user interface. I know very ...
0
votes
3answers
57 views

Re: How can I escape meta-characters when I interpolate a variable in Perl's match operator?

The link How can I escape meta-characters when I interpolate a variable in Perl's match operator? didn't help me. I have tried with Python: import os line='ID_SN=02 MS=DC:A6:32:7E:74:08 S=* ...
0
votes
0answers
38 views

matching single and double quotes in grep using -E [duplicate]

For practicing how grep works in Ubuntu terminal, I made one .txt file: '"..asdf_-_-..34'""'"'" jd8m***ns'''_**''x;';nn*?/...; ++..++ ./,/;]_+))(+_)((*+_":|.<"{}...
0
votes
1answer
49 views

bash perl “and” operator

with a output: apple text1 peach text2 banana text3 melon text4 For delete rows that begin with "apple" or "banana" i put: perl -pe 's/^apple.*\n|^banana.*\n//g' And output is ...
2
votes
2answers
288 views

bash --> perl command: print only the replaced text

I have the file "test.txt" that contains: Val1 = '59' Val2 = '76' Val3 = '42' Val4 = '53' I with this command: perl -pe "s/^Val2 = '(.*)'/\1/" test.txt I Want: 76 But I obtain: ...
0
votes
1answer
78 views

Geany find and replace characters with accent

I use Geany editor for some time, and find it very useful. However, I cannot manage to find an efficient way to replace all characters which have accents in a single action: Rubén, Éliot y el niño =&...
0
votes
0answers
27 views

grep regular expression to remove packages [duplicate]

This is an unexpected result I get $ dpkg -l | grep linux-modules-nvidia-440-5.4.0-* rc linux-modules-nvidia-440-5.4.0-26-generic 5.4.0-26.30+2 amd64 ...
1
vote
1answer
612 views

Does grep distinguish a variable and the $ regex?

I want to grep for a variable that has the string test, but I also want to make sure that it will only find test if that is the only word on a line. So, I tried this: string=test echo "test" ...
1
vote
2answers
43 views

How to sort only usb keys?

I'm on Ubuntu 20.04 what if i want to sort only the usb keys to have an output that would be like this. usb key name / size Gb / path to the usb key I tried this but I have both the hard drives and ...
0
votes
1answer
98 views

How to escape backticks in a .bash_aliases file?

How do I escape backticks as below: root:~# root:~# cat .bash_aliases alias clean='docker rm `docker ps -aq`' root:~# see also: https://stackoverflow.com/q/1250079/4531180 and https://...
0
votes
1answer
63 views

sed with parens in seach not working

I need to replace DatabaseConnection.GetConnection() with DatabaseConnection.GetConnection(_connectionString) Using grep (GNU grep) 3.1 grep GetConnection() CustomerRepository.cs yields bash: syntax ...
0
votes
0answers
39 views

Install all packages containing a certain prefix (in Ubuntu 20.04) [duplicate]

In Ubuntu 18.04 if I wanted install all packages with the prefix postgresql, I use the command sudo apt install postgresql* but in Ubuntu 20.04 it seems there are some packages which contain * so this ...
4
votes
4answers
654 views

How to use split file into two based on line start?

I have this file 1.txt: -e a b -e c d -e e f I want to split it into the following two files. 2.txt -e a -e c -e e 3.txt b d f where 2.txt contains all the lines starting with -e, and 3.txt ...
1
vote
2answers
549 views

Create and append key value pairs to a file of JSON objects with a regex of each JSON object value

I have a big file that contains json objects each object in a new line. File example {"Name" :"%Hana-29-Mrs-Smith","job":"engineer"} {"Name" :"%...
2
votes
1answer
100 views

What does grouping in extended regular expressions mean?

I read that grouping treats an expression as a single unit. What does that mean? Furthermore, what does grep "\(grouping\)" file.txt or grep -E "(grouping)" file.txt do? I also ...
1
vote
0answers
39 views

Use exact match for apt install

I want to install g++-9 but something like: sudo apt install g++-9 Interprets g++-9 as a regex (probably because there is no exact match as I don't have the toolchain PPA installed) and installs ...
0
votes
1answer
35 views

Change regex's result order after sed as order of ls -v

I am working on 53 xml files (az-0.xml ~ az-52.xml). And I ransed '/<loc>/!d; s/[[:space:]]*<loc>\(.*\)<\/loc>/\1/' az-*.xml > AZ to get all stuff in <loc> and merge them ...
0
votes
1answer
421 views

Is exclamation mark(!) included in regex or glob?

In many articles they never included '!' in glob, which is wrong. So, where does ! truly belong to ? I have used ! in glob eg. ls lis[!ta]* does this mean ! is a part of glob?
1
vote
2answers
281 views

How to find a replace newline and commas in a file using regex function in gedit?

i have a text file : "postalAddress2": "Postal Address Line 2", "postalCity": "Postal City", "postalPostCode": "Postal Code", "postalState": "State", i need to remove the second column, ie, watever ...
1
vote
1answer
399 views

Asterisk problem with shell script

Well I have this problem. The problem is when I use an asterisk(*) as a paramenter to compare it with a regex expresion on a if statement. the regex expressions works fine until an asterisk is used. ...
0
votes
2answers
71 views

Reason for giving me more than 2 letters after sorting in regex

I have a text file that is named main.txt and it contains the strings: 1246 12 43 1 I have written the code: cat main.txt | egrep "[12-4]{,2}" I got the following output: 1246 12 43 1 Can you ...
1
vote
1answer
88 views

How to remove the first two characters of the file name when it is a hypen?

I have a bunch of mp3 files named - foo - bar.mp3. My goal is to remove the first hyphen and the first space on the terminal (Bash), which would give foo - bar.mp3. I tried it with rename 's/^..//' * ...
0
votes
1answer
62 views

Regular expression help

The strings to be matched are based on length. String S should be matched iff (|S| mod 3) > (|S| mod 2) where |S| is the length of the string. I've tried to find wrt LCM but I don't know how to ...
2
votes
2answers
75 views

Help with regular expression

Trying to build a regular expression that will match all strings that begin with word "med-" and end with ".opol.nei.com" like this one med-fe3-ua-kiol-33.opol.nei.com
0
votes
1answer
71 views

Regex for IP addresses [closed]

I'm currently learning regex will this detec any IP address? (((([0-9]|[1-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9]|25[0-5])){1,3})(\.){0,1}){4} My previous one was this (([0-9]|[1-9][0-9])|(1[0-9][0-9])|...
-1
votes
1answer
504 views

Exclude match if there is word between two patterns

There is a file: cat file.txt dog blah crocodile blah dog blah cat blah crocodile blah dog blah crocodile blah dog blah cat blah dog blah cat blah crocodile blah dog blah crocodile blah mice blah cat ...
1
vote
2answers
65 views

Remove all file except multiple file by command

I have multiple file on my Ubuntu directory. I want to use a terminal command to delete all files except the files that are generated today. Today is 2019-10-15 and my files are: ... ...
5
votes
1answer
2k views

Find command with regular expressions

I am practicing a find command and the moment, and am curious about the usage of regular expressions in the find command(similar to grep). Does the command support it? If so, what are the options to ...
3
votes
1answer
4k views

How do you match any character in bash regex?

In a bash script, why does message='123456789' echo "${message//[0-9]/*}" display ********* but message='123456789' echo "${message//./*}" displays 123456789? All the documentation I've seen ...
4
votes
1answer
246 views

apt-file regex: find multiple packages at once using or

I'd like to search multiple packages at one using a regexp connecting pattern with logical or |. Assuming the two files /usr/lib/apache2/modules/httpd.exp and /usr/lib/apt/apt.systemd.daily exist: $ ...
0
votes
1answer
75 views

Valid linux command rejected in awk script

The test command is: $ if echo 'abcd' | grep -q '[a-zA-Z]'; then echo 'alpha'; fi From terminal I get 'alpha' as expected. But when I put the command in text file akpl.awk and run with $ awk -f ...
1
vote
1answer
95 views

filter data inside a file

I have a file that contains this type of data. Input: mstage1.service-ft.google.com:mstage1.service-ft.google.com mstage1.service.google.com mstage1.service.google.com:mstage1.service.google.com ...
2
votes
4answers
345 views

How to extract interesting piece of output in bash

My output of command is like: File Size : 23 kB File Size : 9.8 kB File Size : 9 kB File Size : 128 kB File ...
-1
votes
2answers
1k views

Grep and Regex: filter subdomains in a file

First of all sorry I should learn some grep and regex instead of asking that question but I have a little shortage of time for now. And I am definitely going to learn egrep eventually. So here is the ...
2
votes
0answers
1k views

geany editor match newline

I have a plain text file with a large number of lines with a blank line between each pair of lines. I want to remove the empty lines. I am trying using the Replace dialog with regex, to replace '\n\...
1
vote
2answers
80 views

How do I perform a exact match in man page?

For exmaple, I open man page of tar and I want to locate -z quickly. But when I type /-z, things like -i, --ignore-zeros also get matched. So I have to press n several times to reach -z. However, I ...
1
vote
2answers
548 views

Find unicode by its code via grep

How find a symbol by code of Unicode? E.g. try find a space ('\u20' or '\u0020') in the second line: echo -e "u20\n \nfoo" |grep "\u20" - found the literal u20, not the space line. echo -e "x20\n \...
0
votes
0answers
162 views

Regex for /etc/passwd Content

I have a collection files from many systems in my enterprise in a store directory (/store/) and I wanted to find all of the passwd files and cat them out to a single file. Intuitively, the command I ...
1
vote
1answer
380 views

Rename multiple folders, using regex

As the title says, I'm looking for a command / tool - preferably something running from the terminal (I'm using Ubuntu on a remote server), that can replace a specific part of multiple directories at ...
7
votes
1answer
1k 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
102 views

How remove repetition of content?

I have a file aq.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 53.2 WP_012456834.1 ...
0
votes
1answer
672 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
63 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
42 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

1
2 3 4 5 6