All Questions
Tagged with regex or regular-expression
2,843 questions
1
vote
1
answer
153
views
How to run a command that requires input/output location on all folders in a directory, while correspondingly naming the new output folders?
For context, I have some experience with Unix and basic commands, but I'm seriously struggling to conceptualize more complex manipulations of files. Essentially, I have this command I want to run from ...
1
vote
3
answers
162
views
Why does this Perl Regex one-liner not respect the \U escape sequence?
Why does this Perl Regex one-liner not make the full result upper case using the \U modifier?
I expect MY_NICE_WORD.
$ perl -pe 's/(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])/\U$1_$2\E/g' <(...
0
votes
1
answer
136
views
Batch rename files by padding numeric part of filenames with leading zeros
I try to figure out how to batch rename files using Fish so that filenames like image1.png and image10.png will be replaced with image001.png and image010.png, that is, to pad numbers with leading ...
-1
votes
3
answers
178
views
Remove duplicate but uncomplete strings of text
I have a hard time figuring out how to remove duplicate but incomplete strings of text. No success using perl, awk or sed.
I need to transform:
a b
a b c
a b c d
a b c d e
a b c d x
a b c d z
into
a ...
1
vote
1
answer
269
views
Is there one regex tool following merely BRE and throwing errors when undefined?
I am reading Software Design for Flexibility (SDF). Exercise 2.10 asks to implement the translator for both BRE and ERE. The book uses grep to test the translated string like (a.c) in ERE to capture ...
0
votes
1
answer
153
views
What regex should I use to get inotifywait --include to trigger upon the creation of a file with a known fixed filename?
I want to use inotifywait to run code upon the creation of a file with a known fixed filename. This answer correctly explains that I cannot run inotifywait directly against a file that does not yet ...
-1
votes
1
answer
171
views
Taking only a four-digit number as user input
In a shell script, I'm reading user input into a variable. Before I can do anything with it, I need to confirm if the input is a 4-digit number. If not, I want to exit the program without doing ...
2
votes
2
answers
1k
views
What does '\($*\)' mean in sed regular expression in a makefile?
I encounter the following sed command in make's manual.
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g'
And it has the following effect (this is an example in the manual):
main.o : main.c defs.h
# is turned into:...
1
vote
1
answer
280
views
Select files in ranger using ranger
I am a bit confused about using regex in ranger (Arch Linux). I have a folder of files that look like this:
'sometexthere - I. - sometexthere'
'sometexthere - II. - sometexthere'
'sometexthere - III. -...
3
votes
2
answers
680
views
How do quotes work in bash regex regarding reserved characters?
Is there any special bash reserved characters in bash regex expression?
ex:
eg: if [[ $url =~ ^https:\/\/www\.youtube\.com\/playlist\?(.+&)?list= ]]; then echo "URL matches the regex"; ...
3
votes
1
answer
727
views
Match any character except a specific one at the start of a string, or any whitespace
I have a command mapping ("ESC-P") in my .vimrc which underlines the current line, leaving whitespace or # intact.
map ^[p :.t.^M:s/[^[:space:]#]/-/g^M
If I run this on a line of text, it ...
-2
votes
4
answers
271
views
Getting output of /usr/bin/time in CSV format
I am using gawk to parse the output of macos' /usr/bin/time into CSV format as shown below. The problem is that gawk is returning the 'involuntary context switches' value for 'voluntary context ...
1
vote
1
answer
271
views
How to find file names with non-printable characters using BSD `find -regex`?
Some folders on macOS have custom icons that are stored in a file named Icon?, where the ? is actually a CR character, and only prints as "?" in most cases (in Terminal and Finder).
But when ...
0
votes
0
answers
32
views
Bash regular expressions with the character classes are not matching [duplicate]
I am trying out the example 6.8 Testing with Regular Expressions from Bash Cookbook. I am reproducing the example here.
My folder contents are as,
$ ls
Ludwig Van Beethoven - 01 - Allegro.ogg
Ludwig ...
0
votes
2
answers
1k
views
Open specific URLs in certain app?
How can I tell xdg-open to open, say, YouTube URLs in a specific app, but use my default browser for all other URLs?
0
votes
2
answers
104
views
How can I only execute sed commands after so many pattern matches in macOS?
I have a file that contains the same pattern multiple times. I am trying to get sed to perform a series of operations only for the second occurrence of that pattern. The file looks like this:
pattern
...
0
votes
1
answer
301
views
How to search in a pdf or text file by a regex pattern and output locations of matches?
Is there a pdf viewer that allows me to search its text by a regex expression?
In case that I haven't found one, I am thinking about extracting the text and layout from a pdf file by
less my.pdf > ...
1
vote
1
answer
282
views
How to regex/escape backslash in Debian (Gnome) Text Editor?
In Gnome's Text Editor (gnome-text-editor aka gted from the gnome-text-editor Debian package), I cannot get replacement of starting lines (-) at newlines by \item.
I escape the item but does not work....
1
vote
2
answers
56
views
change newline with space on certain condition with sed
I have the following text in a file:
#1713251479
sh lowbat.sh
#1713251495
nvim lowbat.sh
#1713252186
sh lowbat.sh
and I want to put the Unix timestamp inline with the command like this:
#1713251479 ...
-1
votes
1
answer
117
views
awk inside function causing errors
I'm trying to add the following to my .bashrc:
cpu_info() {
top -b -n1 | grep Cpu(s) | awk '{print $2 + $4}'
}
but bash is telling me:
bash: .bashrc: line 131: syntax error near unexpected token `...
0
votes
1
answer
85
views
grep command regexp behaviour, is there a workaround? [duplicate]
I wanted to extract from a text all capitalized words that do not start a sentence or follow a period.
I managed to do it with a combination of sed and grep but got stuck for a while with grep's ...
0
votes
1
answer
194
views
AWK: regex is not processed when passed as a variable
Any suggestions? I read: Escape Sequences (The GNU Awk User’s Guide)
Representative Example
$ Filesystem='/dev/mapper/vgubuntu-Media'
$ FilesystemRegex="$(echo "${Filesystem}" | sed &...
1
vote
1
answer
411
views
Regex pattern in sed to match hexadecimal address of fixed length [duplicate]
I would like to match a hexadecimal address of fixed length using a regex pattern in sed. For example:
0x000000010b2e993c
0x is always present followed by the sixteen-character hexadecimal number. ...
0
votes
3
answers
775
views
regex in grep vs sed
I am trying to find and replace all lines in a text file that start with a single space and then any characters.
Finally got the regex to work on grep grep -E '^ .*$*' Contacts.vcf >> t.txt
Then ...
1
vote
1
answer
109
views
What does "/$" mean to "grep"?
I'm using these two codes below.
ls -lap . | grep -v "/$"
ls -lap . | grep -v "/"
These two give me different outputs in some cases. What does $ mean in that context?
0
votes
1
answer
164
views
regex: how come the trademark symbol matches to a-z?
Sorry if this is a repeat or basic question but it is hard to search for a ™. I'm writing a script to remove weird characters from file names.
How come the trade mark symbol ™ matches [^a-z] ???
$ ...
0
votes
1
answer
143
views
Using find with regular expression
I try to understand how to use the find command with regular expressions (macOS 14.4.1, Zsh 5.9).
I have a directory with files such as note-red.png, note-yellow.png, (and other colors), folder-red....
0
votes
1
answer
592
views
using ripgrep with regex including literal dollar symbol
Suppose I have a script containing something like
VAR=${VAR1%.*}
I'm looking for a command along the lines of
rg "${.*%"
to find it, but I can't get anywhere near.
all of these fail
rg &...
-1
votes
5
answers
116
views
multiline grep search into separate files per occurence
I have a file as following:
example.txt
-1
15
1 0 0 11 -1.0000E+001 1.0000E+001 -1.0000E+001
2 0 0 11 1.0000E+001 1.0000E+001 ...
-1
votes
1
answer
61
views
I can't grep some inputrc string
bind -p |grep -E "\\e.\":" work
but
bind -p |grep -E "\\e\\C-.\":" don't work
I tried a lot of combination
0
votes
2
answers
190
views
Are there reasons why \b is not supported in bash regexs? [duplicate]
AFAIK the \b metasequence indicating "word boundaries" is not supported in bash:
if [[ $foo =~ .*\bWORD\b.* ]]; then
Are there reasons why this is not supported?
Imagine I write a patch/...
0
votes
1
answer
56
views
Using sed to filter output of rsync --itemize-changes -i [duplicate]
After using rsync --itemize-changes -i --recursive --checksum --dry-run to compare the contents of two drives, I got this kind of result:
2024/03/12 08:31:01 [15248] .d Backups/Phone/Pictures/...
2
votes
5
answers
469
views
Search and replace regular expressions [closed]
I have a .txt file which is a collection of numbers.
It looks like this:
[...]
6 2
7 999
8 105
9 78
10 45
11 6666
[...]
up to a 4-digit index.
Now I want to import the numbers as a list in python.
...
2
votes
1
answer
131
views
AIX - BASH REGEX INTO IF STATEMENTS = Segmentation fault (coredump)
Hello everyone and thanks for your support!
I'm facing a problem while using REGEX inside a bash script through =~ operand.
Environment:
GNU bash, version 4.1.7(1)-release (powerpc-ibm-aix5.1.0.0)
...
-4
votes
4
answers
583
views
Why does grep ignore the leading '.'?
I am trying to find a string in a web page downloaded w/ curl. I am using grep to find the string matching a regular expression pattern.
Following is the string I am trying to find:
./download/file....
-1
votes
5
answers
222
views
add empty line before every line that contains certain characters
I have a lot of markdown files that contains something like this:
* header A
- item 1
- item 2
** sub-header A1
** sub-header A2
* header B
- item 1
- item 2
** sub-header B1
** sub-...
0
votes
3
answers
109
views
Edit inside an HTML tag with ed(1)
Consider my humble hello.html file, edited with mighty ed:
$ ed hello.html
28
,p
<title>Hello world!</title>
What's your general approach to edit inside that title HTML tag (bonus if you ...
0
votes
1
answer
658
views
Ansible - Regex in Fetch?
I'm building an ansible playbook to copy a checklist from this file and moving it to another file.
I'm relatively new to ansible. Originally, I used the Copy plugin but saw Fetch may be more up my ...
0
votes
2
answers
186
views
Excluding two kind of directories using regular expressions with grep
This SE answer made me realize I can exclude directories using regular expressions with grep. But it's not working for me when using |:
results=$(grep --color=always -rnF "$searchTerm" . --...
1
vote
5
answers
162
views
sed command to replace a word within a line following a pattern
I'm working with a file that looks like the following, containing with over 50,000 lines of gene IDs followed by their sequence:
gene_A:3342234 CTCTTTCTTTTACGCCT
gene_A:1244-5205 CTCTTTCTTTTACGCCT
...
1
vote
6
answers
304
views
GREP matching 6-7 digits within other data
I am trying to create a Regular Expression that will match a string of digits that is 6 or 7 digits long.
[0-9]{6,7} This works but i dont want a match in strings that are longer than 7 digits.
Please ...
3
votes
2
answers
275
views
sed word boundaries: replace command macros with the commands
I inherited a body of code where "command macros" are used in place of the underlying command to be run, e.g., $TR is used in place of tr. I want to use sed to convert the command macros ...
0
votes
1
answer
954
views
In a bash script, using =~, how do I match the regex ^#.*${new_user}?
Title basically says it all, but I can't figure it out for the life of me and getting confused by bash regular expression matching. This is the if statement I have in my bash script, but it keeps ...
6
votes
1
answer
341
views
Why can't I print this regex range with ed(1)?
I'm totally mystified why I can't print this range with ed when there are two ranges—see file2.tf file—; but I can print when there is only one range—see file1.tf file—and I can print with the gsed (...
0
votes
1
answer
89
views
Meaning of reg expression in drop.conf (Suricata)
I have this question since time ago, and I'm not able to understand it.
In the sample config file for Suricata drop rules, drop.conf, appears this sample line:
# Rules matching specifiers in this file ...
0
votes
1
answer
409
views
Why does grep not find MAC addresses and host names in Linux?
I have a file mac-hosts containing MAC addresses and their associated host names:
e4:5f:01:21:79:01 PF3
e4:5f:01:21:79:03 PF3-BR0
e4:5f:01:21:79:be PF2
e4:5f:01:21:79:c0 PF2-BR0
I want to get a count ...
2
votes
2
answers
210
views
best / any way to count escapes in glibc regex(7) / GNU sed / grep / egrep Regular Expressions?
Given bash environment variable settings :
$ declare -g bs=$'\\' bsbs=$'\\\\' q="'";
This Regular Expression will correctly match a sequence of single quote-("'")-ed text ,
where ...
1
vote
1
answer
54
views
Search and replace content
PIN A
1 1:3 0:8
0 0:0
PIN B
1 1:0
0 0:0
Z Z:0
PIN C
1 1:3 0:8
0 0:0
Z Z:0
I would like to change the content on PIN A & PIN C only without affecting PIN B to
0 0:3
Z 0:3
Can't seem to find a ...
0
votes
2
answers
182
views
Sed - How to replace two strings, but preserving what is in between them?
I am writing a shell script to convert some DokuWiki pages to MediaWiki format and vice versa. I'm having some troubles with footnotes.
There is a plugin for DokuWiki that adds extended features to ...
3
votes
1
answer
1k
views
Start of the line with regex in Ubuntu 22.04's gedit text editor
I try to use regex such as ^ to find the start of the line in the gedit editor in Ubuntu 22.04 but it doesn't work. Also for $ as the end of the line.
Other regular expressions such as \n,\d,\s work ...