Questions tagged [regular-expression]
Regular expressions are a means of matching a pattern of characters within a string.
2,545
questions
0
votes
2
answers
34
views
Rookie Question: Regex in ZSH Script
I think my problem is very simple. I want to construct a new filename, which has a fixed prefix and a number (two digits), which is part of the old filename.
Example: I have a file 'blableblub23.pdf' ...
0
votes
1
answer
53
views
Trying to write a script to find ORF [closed]
I'm new to bash, and I found this script to identify ORF, the problem I have is that it gives me three results (ATGATGGATCCTTGCGTGCAGTAA, ATGGGATAA and ATGATGGATCCTTGCGTGCAGTAA) and not two as it ...
0
votes
1
answer
45
views
Return content between corresponding brackets
I have a file with all type of brackets {}[]()- nested, open and close appropriately. I would like to return the content within the matching square brackets after the string (text:). The content of ...
0
votes
1
answer
26
views
sscanf equivalent in shell script
I have a file which has some stats and counters stored for some ACL rules. I want to design a shell script to read that file and parse those counters.
I have done the similar thing in c programming ...
1
vote
2
answers
78
views
How to search filenames by regex with "find" and rename this files
I trying to find all files ending with :
ancestors-the-humankind-odyssey-trailers-720-1262.mp4
high-tech-divers-1080-1234.mp4
amazon-divers-720-1323.mp4
and rename this files like this :
720-1262....
0
votes
0
answers
23
views
expect script regular expression not working
In expect script I am trying to capture some text from output
Text in output is like ivalue=16ef7baa-0de1-48bf-9e04-d486defbee1c,
code:
expect -re {^(ivalue)\=(.*)(\,)$}
puts "expect_out (0, ...
0
votes
1
answer
23
views
How to recursively change names of files to match?
I'm trying to change the names of multiple files recursively within a directory, looking something like this:
parentdirectory/dir1/foo_1.txt
/dir2/bar_1.txt
/dir3/foo_2....
0
votes
1
answer
48
views
I'm trying to understand how to shorten regex
I'm trying to list all directories that are 1GB or larger. I know I could use ncdu, but at the moment, I'm just trying to understand how to shorten regex...
I found this command, du -h . | grep '[0-9\....
0
votes
2
answers
19
views
match number range with non significative zeros in zsh
I have in a directory files like this :
booklet-001.pdf
booklet-002.pdf
booklet-003.pdf
…
booklet-997.pdf
booklet-998.pdf
booklet-999.pdf
Then, I whant to match the files numbered from 079 to 256. ...
1
vote
1
answer
59
views
How to get the matched expression in GNU Recutils?
In the GNU Recutils manual, it is written that
The longest possible match is returned; this applies to the regular expression as a whole and (subject to this constraint) to sub-expressions within ...
0
votes
0
answers
30
views
Rename command in Ubuntu: change character after date only when date at the beginning of the filename
For example this example with a date (my dates change, but they have always the format YYYY-MM-DD) and a trailing -:
2022-04-28-examplename-some-more-text-2022-04-28-more-text.txt
should become the ...
0
votes
2
answers
103
views
Using a regex search with sed to sort entries in a file
I have a text file of the following form:
dir1/sub-dir1/.../filename1 author date
dir1/sub-dir1/.../filename2 author date
.
.
.
dir2/sub-di2/.../filename1 author date
dir2/sub-dir2/.../filename2 ...
3
votes
1
answer
121
views
ignore whitespace at the beginning of a search pattern
This sed expression works, but is there a better way to represent the white space in both the search pattern and replace string?
BRANCHTAG=7.17.9-main-5ee3e99d5ff002862d93728d821461033de1186d
...
0
votes
0
answers
28
views
Get a PulseAudio Sink Input based on application.process.binary
The command pactl list sink-inputs will print a list of all sink inputs, each one contains a series of properties and some will have the application executable under a property called application....
0
votes
2
answers
78
views
How to quantify POSIX extended regex?
Password should start with a capital (uppercase) letter
Password should contain a lower-case letter
Password should contain a number
Password length should be a minimum of 8 and less than 16 ...
0
votes
1
answer
35
views
How does the regular expression [\\\/][^\\\/]*$ work?
I've some notes of useful regular expressions and one that I always use is the following:
echo '/home/user/folder/file.txt' | sed -E 's/[\\\/][^\\\/]*$//g'
The result that I get from this regular ...
3
votes
3
answers
418
views
How to to suppress error in bash script
I am trying to suppress error generated by date command but still error is coming post executing script.
#!/usr/bin/bash
input="30 FEB 2022"
reg="^[0-9]{1,2}\s[a-zA-Z]{1,3}\s[0-9]{1,4}$...
2
votes
2
answers
474
views
Bash : `[0-9]+$` Regular Expression is not working to match exclusively 3-digit numbers
I have executed below code to check if field "eid" is having length 3 digit and all are digit -
#!/usr/bin/bash
input="A01#PoonamSahani#IVS#123456"
#recCount=`echo $input | awk -...
0
votes
1
answer
77
views
Why can't I match ] with sed
I'm trying to match either [ or ] in a character set in sed but for some reason it doesn't like ]
$ echo 'foo [bar]' | sed -E 's/[\[\]]//g'
foo [bar]
The following works:
$ echo 'foo [bar]' | sed -E '...
0
votes
1
answer
41
views
String searching with grep [duplicate]
I'm just searching in emacs some strings with following pattern:
^*DEFUN*event*$
So i've used grep:
grep -nR "^*DEFUN*event*$" *
but got no match, instead there's a lot of them for example:...
-4
votes
1
answer
79
views
SED - Create new line if finds ONE or ANOTHER MATCH
I have some files in a folder
Each file has this structure:
...
infos: '{"NAME":"Product","CLASSES":[255,107,255,108,255,255,0,0,255,255,255,255,255],"ISSALE":0,...
-1
votes
2
answers
55
views
SED - Generate line only if match some words in OTHER LINE [duplicate]
Assume I have 2 files in a folder
...
product: Nike New Awesome Running Shoe
price: 123
color: black
...
...
product: Nike New Awesome Salloon Soccer Shoe
price: 123
color: black
...
I need to ...
-1
votes
5
answers
83
views
SED - Generate line using some content of other line
I have some files in a folder
Each file has this structure:
...
product: Nike New Awesome Shoe
price: 123
color: black
...
I need to accomplish bellow scenario using single SED command, in all files ...
-1
votes
1
answer
37
views
need help with a RegEX to match comments with no unicode chrarchters there [closed]
so here is my example
#match
#match
#match
foobar#not-matched
the regex I was using was \s?\# but this ended up matching $# in my bash script which I don't want it to do
that or a regex which ...
0
votes
2
answers
83
views
How to run zgrep with multiple AND patterns?
I know how to do it with grep, but the command doesn't work with zgrep
grep -E 'Pattern1.*Patter2' fileName
I'm using zgrep to match patterns inside a .json.gz file.
Because the files are too big, I ...
0
votes
2
answers
31
views
move recently created files matching a pattern from one folder to a different one
I am looking for a bash script example which does the following:
I have files being written under /tmp folder
I do at the xterm prompt, ls -ltr which gives me a list of recent files such as:
...
0
votes
3
answers
120
views
Regex to match JSON in an HTML
From this JSON-HTML webpage I want to filter out only lines with this pattern:
"realname": "SOME_VALUE_COMES_HERE",
I tried this:
curl https://www.mediawiki.org/w/api.php?action=...
0
votes
2
answers
23
views
All whitespaces are matched in man
In WSL-Ubuntu 18.04 with Bash 4.4.20(1) and man with less I ran inside man grep the command -o,| aiming to match an -o with a comma or with a whitespace after it.
But, instead, all whitespaces in the ...
0
votes
1
answer
55
views
How to make a list of values out of a webpage?
In this webpage there is a list of MediaWiki special pages (i.e. pages of the Special: namespace) and I wish to make a simple text list of all these names automatically.
I assume that a matching ...
1
vote
2
answers
59
views
Regexp to fix GPS data
I'm looking to fix GPS data in text file on Linux
Incorrect string model: 49:29:37N 005:28:39E
2or3digits:2or3digits:2or3digits(N|S) 2or3digits:2or3digits:2or3digits(E|W)
Correct string model: 49:29:...
1
vote
0
answers
58
views
How can I recursively search file contents by ANDed patterns and print the output like ag/the silver searcher would?
This will be easier to explain via example. Here are my input files:
file1:
x
x
a
b
c
x
x
file2:
x
x
c
b
a
x
x
file3:
x
x
x x a b c x x
x
x
file4:
x
x
x x c b a x x
x
x
file5:
x
x
a b
x
x
file6:
...
1
vote
5
answers
82
views
How do I replace all the spaces in just a substring of certain lines in a file?
I have 3000 Markdown files named "journal/*.md", some of which contains file links like this:
blah blah text [Label here](file:///path/to file/may contain/spaces)
I want to change all such ...
2
votes
2
answers
279
views
Replacing // with /* */ using sed command [duplicate]
How do you replace // with /* */ in sed command?
I have tried:
sed -e "s/////* *//g" <<< filename
but this results in bad flag in substitute command: '/'
Anyone know how to fix ...
0
votes
2
answers
47
views
Delete a block including nested braces
I have the following block in an NGINX config file
server {
listen 80;
listen [::]:80;
server_name _;
root /web/default/wwwroot/;
# Load ...
1
vote
2
answers
191
views
How to "give priority" to certain pattern instead of others in sed?
I have this sed filter:
/.*[1-9][0-9][0-9] .*/{
s/.*\([1-9][0-9][0-9] .*\)/\1/
}
/.*[1-9][0-9] .*/{
s/.*\([1-9][0-9] .*\)/\1/
}
/.*[0-9] .*/{ # but this is always preferred/...
0
votes
0
answers
12
views
How to expand regex from lines in a file similar to echo on command line? [duplicate]
I have a list of strings in a text file. The strings have regular expression ranges/lists in them. I want to be able to expand all the possibilities. It works if I select a string and paste it after ...
4
votes
2
answers
233
views
grep -w matches only the first occurrence of a pattern in a line
I'm trying to understand why grep -w (version 3.1 of the GNU implementation) matches only the first occurrence of a certain pattern in a line.
Here's an example. I would expect that it would match n1, ...
0
votes
1
answer
91
views
How can I match any character BUT a string with POSIX Extended Regex (for sed)?
I am dealing with a string:
Input File
----
some**wor*ds**with**asterisks
By telling sed to match any character that is not an asterisk:
echo "some**words** **" | sed -E 's/\*\*([^*]+)\*\*/...
1
vote
6
answers
100
views
Extract URLs from inside img html tag across multiple files
I have a folder with lots of .html files, the files have <p>..</p> html tags and <img...> html tags across all of them
I am trying to extract ONLY the URLs found inside the <img......
2
votes
2
answers
265
views
How to format this IP list?
I have a comma-separated list in this rather simple format:
IPrangestart,IPrangeend,int number,date (delimited by slashes),Name (is dirty, contains spaces, dots, dashes, quotemarks etc)
The first ...
0
votes
1
answer
116
views
Regex with grep vs egrep
Can someone explain what the following expressions mean with grep? How is that different from egrep?
With grep:
^[0-9]\+$
[0-9]\{3\}.*[0-9]\{3\}
^.*[0-9]$
With egrep:
^[0-9]{7}$
^.*$
0
votes
1
answer
41
views
Select line matching pattern followed by substitution using `sed`
Here's what I'm trying to replicate:
$ cat << EOF | { var=$(grep 'foo=');\
if (( $? == 0 )); then echo "$var"\
| sed -E 's/foo=(.+)/\1/'; else exit 1; fi; }
foo
foo=bar
EOF
bar
...
0
votes
2
answers
136
views
How to use string substitution in one command with command substitution?
When the git branch command is executed it usually shows:
* main
.... # other branches
Having the following through command substitution and string substitution work as expected:
current_branch=$(...
0
votes
2
answers
61
views
Replace either of `"(.+)"` or `{(.+)}` by `\1`
I can do
{(.+)}-->\1, and
"(.+)"-->\1, or
({.+}|".+")-->\1.
I would I do {(.+)}|"(.+)"-->\1?
$ echo "\"x\"" | sed -E 's/"(.+)"/...
3
votes
3
answers
556
views
How to use OR with grep?
I ran grep 'd(o|i)g' a.txt and a.txt contains dig but nothing was returned. Is this the right way to match dog or dig?
0
votes
2
answers
54
views
Unable to match a specific regex with bash
I have a txt file with content as such:
Adedunmola Okikiola Adewole 512.035
215−39 ^M
Ademir Cleto de Oliveira 055.735
445−13 ^M
Adilson ...
1
vote
2
answers
281
views
Regex remove URLs paths and keep last slug of the URLs
I have a file that has different HTML tags inside it and multiple img src URLs spread out across it:
<img src="https://www.example.com/parent-folder-1/subfolder-1/image1.png">
<img ...
0
votes
1
answer
50
views
grep -z: why \0 produces no results?
Scenario:
$ cat t0.txt
xxx
yyy
$ man grep | grep '\-\-null\-data' -A1
-z, --null-data
Treat the input as a set of lines, each terminated by a zero byte (the ASCII NUL character) ...
-1
votes
1
answer
111
views
How to grep the difference between two files into a file
Saying that I have two files: a.txt and b.txt.
The content of a.txt:
mosfet23
cap098
inductors 98
metal96
The content of b.txt:
test 23 cap098
drc45 metal96
lvs cap098
Of course I can use vimdiff to ...
0
votes
2
answers
54
views
How to grep selected strings separated by -
I am new in grep and I have a list of packages where I only need to display certain results.
Here's a list of packages:
apache2/oldoldstable,oldoldstable,now 2.4.25-3+deb9u12 amd64 [installed]
apache2-...