All Questions
Tagged with regex or regular-expression
2,843 questions
3
votes
3
answers
901
views
How do I ensure a bash script argument is a specific value?
The following script is supposed to check $1 and see if it is a specific value. It only works when one Bash [[... =~ ...]] regex check is passed in an if statement.
The error is: When more than one ...
0
votes
2
answers
59
views
How to replace all `[` and `]` in sed?
Logical way would be to use an 's/[\[\]]/_/g' regexp replace, however it does not work as intended to.
It replaces a chain of [ and ] with a single _.
$ echo 'x[]x'|sed -E -e 's/[\[\]]/_/g'
x_x
$ echo ...
0
votes
2
answers
109
views
find and regex matching behavior? Why won't 'L1[0-6].*' match "L15a" [closed]
I'm trying to understand some subtle behavior of find and its -regex option
I am trying to create symbolic links from files entitled slides_.*.pdf in folders that start with L1[0-6]. I tried the ...
1
vote
1
answer
44
views
Where can one find a repository of regex for logcheck?
I was looking at logwatch, but found that it appears to have been replaced by logcheck, and that logcheck is configured by default for just two files using various regexs to match lines in those logs.
...
3
votes
2
answers
201
views
Syntax Error on ssed for Regex Subroutine definitions
I'm stuck with this error without clue or reference how to solve it:
I have this list on text.txt
Angie Apple
Angie Banana
Angie Tomato
Angie Peach
Angie Onion
I'm running substitution through ssed (...
6
votes
3
answers
377
views
Help w/ "posix-extended" regex for 'find'
Can someone explain the issue I've described below, and perhaps suggest a resolution?
I'm trying to process a set of filenames using find. My platform is macOS (Ventura 13.7.7), and the shell is zsh ...
-1
votes
1
answer
80
views
sed: why my regex does not work correctly
So, I'm facing a curious issue with sed; I have reduced my problem down to the following. Why does
sed -e 's/\"\([^\"]*\)\">/\1/' <<< '["\{">'
return ["\{&...
-1
votes
3
answers
224
views
Perl RegEx one-liner that outputs all matches from one line of STDIN
Here is a silly example that effectively illustrates what I need:
echo '"this text", " ", "is in speech marks"' | perl -lne 'print "$1" if /"(.*?)"/'
...
2
votes
3
answers
377
views
sed: invalid usage of line address 0
I need to keep only last 30 days events in the log and remove other data.
Log has timestamp (date +%e/%b/%Y:%H:%M) and next line values like
01/Jan/2025:00:00
value1
value2
...
01/Jan/2025:06:45
...
3
votes
2
answers
186
views
Embedded special characters skewing sed output
The Issue
I've been parsing a file with sed trying to tweeze out the desired data. This has worked fine for most lines in the file but there appears to be some embedded special characters that are ...
0
votes
5
answers
881
views
Extract email addresses from line, with multiple email addresses per line
I have a text file that mixes names and email addresses, in a comma-separated list.
First Person <[email protected]>, Second Person <[email protected]>, Third <[email protected]>
...
-2
votes
1
answer
150
views
sed inplace in selective block
I have a dir containing logstash config files
these files have filter blocks, and sometimes nested child blocks
need to match whole filter block
filter { any text till final matching closing brace for ...
3
votes
6
answers
230
views
regex to find text plus trailing 10+ spaces; sum of two matches' lengths is part of matching condition
I'm trying to clean files that are copy/pasted versions of my Cygwin (mintty) terminal running bash1. Usually, the input and output are separated by a linefeed ('\n'), as expected. However, when I ...
8
votes
1
answer
606
views
Is there any way to see the string that was matched in grep?
I'm not talking about -o option.
Posix says:
The search for a matching sequence starts at the beginning of a string and stops when the first sequence matching the expression is found, where "...
-1
votes
3
answers
120
views
How to remove markdown links from headers with sed?
I'm trying to use sed to remove links like and leave just the title:
## [Some title](#some-title)
This is my command:
sed 's/^\(\#*\) *\[\([^\]]*\)\].*/\1 \2/'
I expect to have just the text without ...
0
votes
0
answers
57
views
Borg files in `~/.local/share/` occur a “dir_open: [Errno 13] Permission denied” error
General overview
With BorgBackup, I do a backup of my whole $HOME. However, some files (all located on ~/.local/share/) occur a rights error. Probably cause it’s ephemeral files belonging to root, I ...
1
vote
3
answers
392
views
How to delete lines with sed if they contain a phrase?
I tried this
sed -i '/^already satisfied$/d' loggocd.txt
but lines like this one
Requirement already satisfied: cryptography in /home/go/.pyenv/versions/3.9.1/lib/python3.9/site-packages
are not ...
-3
votes
2
answers
107
views
How to extract an unknown number of file lines between two regex patterns?
For example I have file with many of lines and it has such part
...
pattern1
line1
line2
line3
pattern2
...
how can I extract line[1-3] via one-liner command (awk or something like that)?
2
votes
4
answers
184
views
Grep (BRE) on surrounding delimiters w/o consuming the delimiter? Counting delimiter-separated strings between filename and extension
I have a dataset of images labeled/classified by characteristics, where an image can have more than one label. I want to count how many of each identifier I have. A toy dataset is created below, with ...
-1
votes
3
answers
221
views
grep capture from beginning until first 2 chars found
I have this list:
list="aa bb cc dd ee ff ab cd ef"
What I'm trying so far:
$ grep -o "^[^cd]*" <<<"$list"
aa bb
Expected output:
$ grep -o "^[^cd]*" ...
3
votes
3
answers
292
views
Sed - Help Matching with Multi-Line Match
I am fairly new to regexes and it appears I am missing something in my understanding. I have the following file, and I am trying to insert a value if it is missing.
The file looks like
[composefs]
...
6
votes
1
answer
181
views
Sudo regex 1.9.16p2 - not working
I installed sudo v1.9.16p2 on a RHEL 9.5 server which should support regex matching. As a test I created a test user (testuser) and created a (simple?) rule in /etc/sudoers for this test user that ...
1
vote
2
answers
509
views
sed in makefile is not working as expected when using regex
Got a makefile whith this command which convert folder names on ./cmd/ from snake_case to PascalCase
test:
@for f in $(shell ls ./cmd/); do \
echo $${f}; \
echo $${f} | sed -r 's/(^...
0
votes
5
answers
130
views
Match multiple vars across two lines and delete entire entry
MATCH1.MATCH2 {
always same MATCH3
}
All three MATCH(es) must match.
input:
foo.bar {
always same bus
}
1.2 {
always same 3
}
a.b {
always same c
}
i.ii {
always same iii
}
b.2 {
...
0
votes
4
answers
127
views
sed \(.*\) matches more characters than originally intended
I have a list of celebrities with their place names as,
Bill Gates Manhattan
Elon Musk California
Narendra Modi Delhi
Donald Trump Washington
I like to rewrite with the location name in the middle ...
1
vote
3
answers
108
views
Extracting "devname" from log message with re_extract
Can anyone help? I've exhausted my knowledge and troubleshooting skills trying to get this working.
Here is the example data from "msg":
date=2025-03-26 time=12:45:57 devname="this-is-...
1
vote
1
answer
308
views
Sed delete everything after a word
I'm new to sed, I have a string
lightdm --session-child 14 21
the two numbers can change and I would remove everything
after lightdm
I tried various way but none worked
sed 's/ --session-child*//'
...
0
votes
2
answers
111
views
How to Rsync anything with its name having exact number of consecutive digits
I have the following directories:
1
12
123
1234567
1234566
1234555
121
I want to rsync the directories with exactly 7 digits in the name. (That would be 1234567, 1234566, and 1234555 from the above ...
6
votes
3
answers
573
views
How to make Perl half/full width-insensitive regular expressions?
In Perl, /a/i matches both A and a, so I don't have to write /A|a/.
What is the easy way to write /4|4/ ?
Yes, I'm talking about
$ unicode 4 4|grep U+
U+FF14 FULLWIDTH DIGIT FOUR
U+0034 DIGIT FOUR
...
-1
votes
1
answer
85
views
Unix 'find' utility : Question about relation to Kleene Star in Mathematics
Does anyone know how the Unix find search -name queries with _? in leftmost column relate to Kleene Star as defined on Wikipedia? How can I accomplish the rows with _? using find?
. : empty ...
0
votes
2
answers
162
views
variable assignment doesn't create one same object at least for grep
The problem is as follows (Here I don't use find since it doesn't support double-asterisk wildcard **):
$ FILES=(foo/**/*.suffix bar/**/*.suffix2)
$ grep baz "${FILES[@]}" # works
# I use ...
2
votes
3
answers
329
views
How to highlight the matched regex pattern got by many regex exps disjoined with `||` in awk?
This highlight feature can be done easily in grep with --color. But grep can't do regular and invert match at the same time, e.g. grep foo -v bar ... (I need this feature since --color can be only ...
3
votes
1
answer
274
views
Replace accented characters with perl-rename
I'm standardizing the name of several files at once, so I wrote a regex for perl-rename:
perl-rename 'y/A-Z/a-z/; s/ã|á|â/a/g; s/é|ê/e/g; s/í/i/g; s/õ|ó/o/g; s/ú/u/g; s/ç/c/g; s/(?<=\d-)*\s/_/g; s/...
0
votes
3
answers
1k
views
regex, remove everything before first underscore
i have this string grafana-stack_alloy and require everything after the first underscore
alloy.
I need the result as a group $1. Tried this one, but it failed. (?:)?([^_]+)*$.
Can anyone help me to ...
1
vote
4
answers
252
views
Find lines in Vim that start one way and that don't end in another way
I'm trying to use Vim to find, via /, lines that start and end in specific ways. In particular, I'd be looking for lines that start with the character > and without the string RNA at the very end. ...
2
votes
3
answers
117
views
Printing a specific section everytime search results are matched
I have a pretty basic text file on a Linux machine that has stuff like Chapters, Dialogues and References.
This is what it looks like
Chapter: 1 One: Birds and Trees
Birds are beautiful and trees ...
8
votes
2
answers
1k
views
Why does the special character `?` need to be escaped in grep, but not `.` or `*`?
The only file in my current working directory is called test.txt and its content is simply:
This is a little test file.
Executing
grep -in * -e 'te.?t file'
gives no match.
But escaping ? works:
grep ...
0
votes
2
answers
165
views
Trying to sort in alphabetical order of last name but needs to be formatted ID, first name, last name
No matter what I try with cut, awk, sed, or grep, it orders by ID first only then last name then first name when it should go in order of last name so it instead should look like
f132b02: Kiara ...
0
votes
2
answers
67
views
Capture and print on the exact match from a file
I am trying to use grep to match only a specific part of a row in a file.
The file is a huge csv file with some columns containing json with commas so it is hard to figure out which column what I am ...
0
votes
2
answers
263
views
Detect and remove consecutive lines with the same information but with different amount of spaces in them
I posted a question about detect consecutive lines in a text, with the same words and the same order, the amount of spaces at leading, trailing, or between words may vary from one line to the other. ...
0
votes
4
answers
257
views
regex to detect two consecutive lines that have the same words in the same order, but different spaces at leading, trailing and between words
I want to find a regex or perl oneliner to detect two consecutive lines that have the same words in the same order, but the leading spaces, trailing spaces and amount of spaces between the words may ...
4
votes
4
answers
428
views
BASH - Find file with regex - Non-recursively delete number-only filenames in directory
I'm wanting to non-recursively delete all files in a directory where each filename contains only numbers, using only a single line of BASH.
I somehow accidentally ran a shell script with commented out ...
1
vote
2
answers
759
views
How to specify [[hints.enabled]] Alacritty section one matching hypelinks (open on mac) and second any file paths and open in vim
I am using Alacritty terminal for quite some time and it has a great feature to follow hyperlinks when clicking Ctrl+Shift+U. I am using MAC so a default program is open.
Iam trying to add one more ...
0
votes
1
answer
137
views
Why does this sed command not work with "?" and "+"?
I'm trying to filter IP addresses from the ip a command via sed. When I write inet6* it works. When I write inet6? it doesn't find the match.
With *:
$ ip a | sed -ne 's,^ *inet6* \([^ /]*\).*$,\1,p'
...
1
vote
1
answer
198
views
Manpage of 'regex(7)', from the shell
Today I have read two answers about which regular expressions are supported by less: Where is less search pattern reference?
Kusalananda mentioned re_format(7) and referenced its manpage as man 7 ...
3
votes
2
answers
416
views
Concise zsh regular expression parameter expansion to replace the last match of a pattern
Is there any concise zsh regular expression parameter expansion to replace the last match of a pattern?
I know a very ugly way of doing it, but I'd prefer a cleaner, more concise syntax:
# param, ...
0
votes
4
answers
153
views
grep use -v on one regexp and disable for another
i want to grep for lines containing a OR lines not containing l
example text file:
ant
beetle
cow
deer
eel
flamingo
giraffe
desired output:
ant
cow
deer
flamingo
giraffe
it would be nice to use ...
3
votes
7
answers
651
views
sed (or awk): print captured group or placeholder if it doesn't exist
So since GitHub removed the insights tab for non-premium accounts, I'm trying to locally list insertions and deletions to my git repository per day.
I figured out this way of printing what I want:
git ...
-1
votes
1
answer
44
views
How to search for multiple instances of a file with different directories [duplicate]
I'm working on a utility that needs to find specific files in an environment.
I have a filename that I need to determine the location of, however this filename can exist in several different ...
1
vote
3
answers
166
views
sed: To match a newline and spaces
I have a following file:
<head>
<title>this is a title</title>
<style>
here goes a style sheet
</style>
</head>
I need to strip the <title> element ...