Questions tagged [regular-expression]
Regular expressions are a means of matching a pattern of characters within a string.
2,736
questions
1
vote
2
answers
40
views
How do I make sed delete a block of indented text below a non-indented line that also contains particular regexp, but not beyond?
I've been fighting with GPT, the sed documentation, and regex for almost my entire day. I'm trying to parse through files that contain cisco prefix-list configurations and print out the lists that ...
-1
votes
0
answers
50
views
Unescaped left brace in regex is illegal here in regex [closed]
I'm trying to remove duplicates in my Nagios-files on Rocky 8.8.
But the grep command shows this error:
Unescaped left brace in regex is illegal here in regex.
Well there are many { in the Nagios-...
-2
votes
0
answers
54
views
Malicious code - find in database linuux
I'm looking to locate a what looks look a base 64 malicious code. The code changes everytime it's loaded so I'm unable to search for anything specific, however, the amount of characters seem to be ...
-1
votes
2
answers
58
views
allow display and or execution of web files but not direct url access using htaccess
What I want to Achieve is:
I want to be able to display the (mp4 | jpg | ico) file types and execute the (js | css) file types on the browser only on my domain but I dont want them to directly access ...
4
votes
5
answers
575
views
Replace a block of spaces with a comma
I have a file in txt format that I want to convert to csv. Between each field there are blocks of spaces. The amount of spaces is not the same between every field.
Is there a command with sed or awk ...
1
vote
2
answers
43
views
Shell one-liner for batch renaming music albums folders according to regexp – “zsh: missing identifier after `+'”
My music library consists of folders named after the following pattern :
METAGENRE - Subgenre(s) - ARTIST - Album (year)
I want to switch the year and album "tags" in the folders' names for ...
-1
votes
1
answer
87
views
Bash built-in regex vs sed and grep commands
Bash has built-in regex for pattern matching. Sed and egrep commands can also do this.
What is the benefit to choose the built-in vs commands? I would like to know which one is faster and other ...
1
vote
1
answer
34
views
get PID by name
I'm running an autopilot on a closed environment running Linux, and I've got a kernel thread whose priority I need to change. I'd prefer not to patch the kernel for this. This thread is created early ...
-4
votes
4
answers
94
views
For lines matching in the first column, create a "X see also Y" list using second column
For wikidata I want to create QuickStatments to assign P1889 values for forts with identical names. This can be simplified to.
For a file with:
Fort George,Q12
Fort George,Q56
Fort George,Q678
Fort ...
0
votes
1
answer
22
views
nano unhighlight string if a quote is escaped
I'm trying to unhighlight string if one of the quotes is escaped.
sh.nanorc regex is as follows:
color brightyellow ""(\\.|[^"])*""
I'm having success with the closing quote ...
1
vote
1
answer
51
views
Regular expression to search contents that contain qr in it?
grep -rlw . -e '%QR%'
I am doing something like this. Anything could be before QR and anything could be after QR. It could be nothing as well.
I am searching for file names containing QR inside in ...
1
vote
4
answers
64
views
Find strings with several exclude-constraints on the beginning of the line
If I want to find all the "double-quoted" strings in files, I would use:
grep -rnw . -e "\".*\""
but I want to exclude all the lines that begin with some text. For ...
0
votes
1
answer
75
views
Transforming a filename with 'tr' using RegEx doesn't work
I want to use the tr command to rename something like filename.ext to someName.ext.
To do that I've tried
echo "filename.ext" | tr -c ".a-z" "someName"`
to replace the ...
5
votes
4
answers
906
views
Select everything between two timestamps in a log file
awk '/10:..:/, /13:..:/' server.log > /tmp/awktmp
I tried this command as someone gave me. It worked for me to find logs between 10:00 AM to 13:00 PM but I don't understand it fully.
Please tell ...
0
votes
1
answer
43
views
how to match all-caps words execpt for a specific words in regex
I have a large number of files that contain all caps words that I want to delete, but also there are a small list of those words I want to keep in the files, how can I do it in regex.
for example:
...
2
votes
2
answers
263
views
Optional group never matches
The following is a simplified version of a problem I'm struggling with. I have this file:
1 H 200 OK.Content-Length: 2422 x
2 H 403 Forbidden.z
and I tried to get sed to match the HTTP Status and - ...
1
vote
2
answers
50
views
find combine many names and paths in one command
I have a rather complex set of files to be found and reacted upon (e.g. paths copied to a text file):
For example:
find / \( -iname "*ssh*" -or -iname "*scp*" \) \( -not -path -&...
0
votes
1
answer
55
views
Is KornShell pattern a regular expression?
KornShell pattern extends POSIX shell "glob" patterns (i.e. *, ?, etc.) with repetition specifiers such as *(...), +(...), as well as negation among many nice features.
In a hobbist project, ...
0
votes
0
answers
30
views
Why locate command doesn't recognize `|` meta character as basic regex?
When i use extended regex on locate command work well:
└─$ locate --regex "(\.rar|\.zip)"
/home/kali/Downloads/linux.zip
/usr/lib/jvm/java-...
0
votes
2
answers
158
views
Regular expression for string starting and ending with brackets
I have a text file infile.txt that contains the following string:
[ A ]
1
2
[ B ]
3
[ C
4
5
[ D ]
I wish to use both grep and sed to print the lines that start with [ and end with ]. Thus my desired ...
4
votes
5
answers
934
views
Cut the release versions from file in linux
I have a file that contains a list of OS packages with their release versions. I only need the package names and don't need the release versions.
How can I achieve this using Linux commands or any ...
1
vote
1
answer
54
views
sed command with five slashes
Any idea what this is supposed to do?
grep -lr . | xargs sed -i '' -e '/ifcfg-wlan0/!s/wlan0/<name of the device>/g'
I've never seen a sed command with five slashes, and I can't find any ...
0
votes
1
answer
127
views
How to install pcre2-tools on Rocky Linux
I am on RL v9.1. The package seems to be there (pcre2-tools-10.40-2.el9.x86_64.rpm) but yum doesn't see it:
yum install pcre2-tools
runner_gitlab-runner 1.4 kB/s | 1.0 kB 00:00
...
2
votes
3
answers
170
views
How do you match zero or one character in the "-name" option of the "find" command?
For instance, want to match all files in this directory and sub-directories that end with .ly or .ily. So far, find . -name "*.*ly" -print is the closest I can get but this could ...
0
votes
4
answers
198
views
Remove hostnames from URL with sed/awk
I got lists of urls with different domains and I want the hostname removed with sed, awk or something similar and only keep the path. There are no urls with port or username@password in it.
input:
...
1
vote
5
answers
70
views
Sed - match up to, but not including, pattern, with newlines included
I have an input that is in a multi-fasta format. Essentially:
>header1
AAAAAATTTTCCCCGGGG
AAAATGTGTGTCTCTCTC
ATAGGATGAGT
>header2
GGGGTGTGTGATGTAGTA
AAAATGTGTGTCTCTCTC
ATAGGATGAGT
....
>...
-2
votes
1
answer
23
views
How to delete the whole row when it starts with 'H' in a comma separarted csv file [closed]
How to delete the whole row when it starts with 'H' in a comma separated csv file for below example,
H,NYR,257,10,11,000, TESTING,055,000,2023-05-10
D,NYR,257,10,11,000, TESTING,055,000,2023-05-10
H,...
3
votes
4
answers
520
views
How to use "grep" to get the gpg key fingerprint values?
If the output of gpg --list-secret-keys --keyid-format=long is
pub rsa3072 2023-01-13 [SC] [expires: 2025-01-12]
D8524A558964E86Cxxxx93111270xxxxxxxx7A74
uid [ultimate] demo(demo) <...
0
votes
1
answer
50
views
How to find the text between the nth occurence of a character and occurence n+1 with perl-rename?
perl-rename 1.14-1
Arch Linux: linux 6.2.12.arch1-1
I have a directory/folder with several subdirectories, all are named by one of the following patterns
Status quo of existing patterns
something-...
4
votes
4
answers
625
views
grep multiple patterns with differing number of matching groups?
I'm trying to tally insertions and deletions from git diffs.
I have the following, which when piped one or more strings of the form "4 files changed, 629607 insertions(+), 123 deletions(-)" ...
0
votes
4
answers
121
views
Removing trailing newline from grep output
I'm looking for way to remove new line from grep output, I use
grep -oP "changed: \[([^]]+)\] =>|'path': '([^']*)'" /path/to/file
The output is like:
changed: [hostname1] =>
'path': '/...
2
votes
0
answers
73
views
Matching Japanese regex (simple ranges) in bash doesn't work as intended
I am pretty sure my regexes are fine but they don't work with bash. I crafted them myself using https://unicode.org/charts/. As you will see, they work properly with awk.
Here are the ranges to spare ...
0
votes
2
answers
190
views
Bash posix regex optional groups
Trying to match some Gentoo package naming components from strings in forms category/name-version, due to its complexity, I have come to this:
if [[ "$1" =~ ^([<>]?=?)(([^\/]+)\/)?([^[:...
4
votes
5
answers
476
views
Bulk rename directories with spaces
I have a bunch of files and directories containing white spaces (several of them in each file name) and I'm trying to remove them. I am plagged with the 'dumb' rename that does not understand regex. ...
0
votes
2
answers
126
views
Regular Expression Bracket Expression with bash
I want to write a regular expression that only allows a user to enter an input which contains alphanumeric characters, underscores, periods, dashes, and plus signs.
In a bash script, I have:
VALID=&...
0
votes
1
answer
58
views
Why am I getting this error when running "find -E -regex" on macos 12.6.x?
As part of a script to clean up folders on our server, I've written a find regex (on Macos 12.6.x) that will find directories that are named as "YYYY_MM_DD" and then change the name of the ...
0
votes
1
answer
82
views
Find files (ignoring extension) that may contain spaces, based on a supplied text file containing this list
With my limited knowledge of Linux tools, I hit a wall.
Because my list of files contains spaces (space character), the "find" command fails to find any hits, but does not produce any errors....
0
votes
3
answers
103
views
Find a string enclosed in spaces or coming from the beginning or end with grep
How can I match some string enclosed in spaces or coming from the beginning or end?
I need match for -someword in the following sentences: word1 -someword word2, -someword word1, word1 -someword, -...
7
votes
1
answer
1k
views
Does Bourne Shell have a regex validator?
I am on a closed network (i.e. no connectivity to the internet).
I have a bourne shell script that asks for the user to enter a regular expression for use with grep -P.
Generally speaking, I like to ...
-1
votes
1
answer
101
views
How to leave only the first word in each line with sed, cut or awk?
I have a big list with the following format:
info1 info2 info3 info4
Each line has these four fields. How can I use regular expressions to remove all the words except the first word for every line?
3
votes
1
answer
108
views
sed's greedy match shouldn't match this string, but does
From what I've read online, it's hard to understand the behaviour of a non-greedy match (lazy match) in sed. I am puzzled by what seems to be a lazy match.
In ~/tmp/tmp.txt, I have one line with 3 ...
2
votes
5
answers
149
views
Extract lines from indented output
I'd like to parse parses the output below (from ddcutil) in a shell command:
Model: MQ780
MCCS version: 2.1
Commands:
Op Code: 01 (VCP Request)
Op Code: 02 (VCP Response)
Op Code: 03 (VCP Set)...
0
votes
2
answers
72
views
Unix. Run script across multiple dirs on specific files, where pathname has regex
I want to make a bash script (split.sh) that iterates across multiple dirs with same suffix, and then runs a function for specific files within them. I am almost there:
#!/bin/bash
path="/mypath/...
1
vote
1
answer
111
views
How can I rename files recursively using a regular expression
I have hundreds of files on a hard drive with many directories and sub directories which start with:
1.1, 1.2, 1.3, 2.1, 3.1, 9.1, 10.1, 10.2, Etc Etc...
The problem is that when I list them they ...
0
votes
1
answer
144
views
Using Bash "=~" operator with WHILE instead of UNTIL loop
I'm writing some shell script which asks the user for a domain name. I'd like to prompt the user for input in a loop, and write an error message, if the input is not a valid domain name.
Basically, ...
0
votes
2
answers
77
views
Remove and add at the same time with sed
I have an variable in my script which I want to change:
read -p "Enter the name of city: " city
if [[ $city =~ Liverpool ]]; then
sed -i -e "/\$type\_of\_city =/ s/= .*/= ${city}/&...
3
votes
3
answers
186
views
Print a line matching pattern1 unless next line contains pattern2
I want to match lines that have foo unless the next line contains bar. So given a file containing:
1 foo 1
foo 2
baz bar bap
only 1 foo 1 would print. I got this to work using a negative lookahead /...
0
votes
4
answers
86
views
Find two lines with unknown number of lines between them
I have a text file with output like this:
file_0108.json
2023-02-22T01:15:05.531+0000 connected to: mongodb://[**REDACTED**]@localhost
2023-02-22T01:15:08.531+0000 [######..................] db....
0
votes
1
answer
95
views
Skip printing on line matching regex pattern
How can I skip printing on the line that matches pattern. For remaining lines display with the corresponding printf until a new line matches another pattern.
Want I want is the Wht:, Grn:, and Blu: ...
0
votes
2
answers
198
views
Bash check if item in list not behaving as expected
I am trying to write a script which has a condition based on a variable appearing in a list:
#!/bin/bash
LIST=`ls`
function listcontains() {
[[ $1 =~ (^|[[:space:]])$2($|[[:space:]]) ]] &&...