Skip to main content

Questions tagged [regular-expression]

Regular expressions are a means of matching a pattern of characters within a string.

Filter by
Sorted by
Tagged with
-1 votes
1 answer
46 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 ...
Rishikesh Negi's user avatar
2 votes
2 answers
840 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:...
liginity's user avatar
  • 143
1 vote
1 answer
28 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. -...
mor3dr3ad's user avatar
3 votes
2 answers
286 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"; ...
Nor.Z's user avatar
  • 133
3 votes
1 answer
66 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 ...
Swordgeek's user avatar
-2 votes
4 answers
139 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 ...
jonathannah's user avatar
1 vote
1 answer
48 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 ...
Thomas Tempelmann's user avatar
0 votes
0 answers
26 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 ...
Saravana's user avatar
  • 101
0 votes
1 answer
37 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?
Geremia's user avatar
  • 1,185
0 votes
2 answers
50 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 ...
Felipe Evaristo's user avatar
0 votes
1 answer
78 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 > ...
Tim's user avatar
  • 103k
1 vote
1 answer
42 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....
Léo Léopold Hertz 준영's user avatar
1 vote
2 answers
28 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 ...
Yuki San's user avatar
-1 votes
1 answer
53 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 `...
Mathew's user avatar
  • 243
0 votes
1 answer
48 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 ...
Pikko's user avatar
  • 16
0 votes
1 answer
121 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 &...
Porcupine's user avatar
  • 2,056
0 votes
1 answer
47 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. ...
Max MacLeod's user avatar
0 votes
3 answers
89 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 ...
atclaus's user avatar
  • 101
1 vote
1 answer
86 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?
shersher's user avatar
0 votes
1 answer
79 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] ??? $ ...
codywohlers's user avatar
0 votes
1 answer
44 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....
jsx97's user avatar
  • 71
0 votes
1 answer
42 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 &...
njamescouk's user avatar
-1 votes
5 answers
71 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 ...
Honza S.'s user avatar
  • 101
-1 votes
1 answer
42 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
user3634569's user avatar
0 votes
2 answers
51 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/...
guettli's user avatar
  • 1,449
0 votes
1 answer
21 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/...
the_redburn's user avatar
2 votes
5 answers
428 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. ...
Lereu's user avatar
  • 133
2 votes
1 answer
82 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) ...
omar.c's user avatar
  • 23
-4 votes
4 answers
212 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....
Seamus's user avatar
  • 3,062
-1 votes
5 answers
148 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-...
GhostOrder's user avatar
0 votes
3 answers
76 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 ...
mbigras's user avatar
  • 3,170
0 votes
1 answer
228 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 ...
shottypop's user avatar
0 votes
2 answers
55 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" . --...
wyc's user avatar
  • 143
1 vote
5 answers
109 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 ...
bryophyta's user avatar
1 vote
6 answers
162 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 ...
pt-t's user avatar
  • 25
2 votes
2 answers
222 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 ...
FKEinternet's user avatar
-1 votes
1 answer
79 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 ...
geckels1's user avatar
  • 163
6 votes
1 answer
310 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 (...
mbigras's user avatar
  • 3,170
0 votes
1 answer
38 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 ...
Ecofintech's user avatar
0 votes
1 answer
137 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 ...
FKEinternet's user avatar
2 votes
2 answers
171 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 ...
JVD's user avatar
  • 115
1 vote
1 answer
46 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 ...
JSL's user avatar
  • 11
0 votes
2 answers
134 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 ...
Daniel's user avatar
  • 363
0 votes
1 answer
455 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 ...
trilisser's user avatar
  • 103
1 vote
1 answer
78 views

How to output regex matches as many-to-one combinations?

I'm trying to match two different text patterns in a single file and combine their results. Specifically, I have source code whose structure I want to understand by mapping events to their handlers, e....
Jan Wilamowski's user avatar
-1 votes
4 answers
260 views

AWK match exact value from comma separated column in brackets

So I need to find an exact number match from one column in a file. The input files is similar to the following: 33.xx 1111 0 0 0 some+text 12:12:12.9999 9999:090909 88 [11,22,33] 2020-12-12 12:12:...
Scott Nipp's user avatar
0 votes
1 answer
109 views

Whats the meaning of this sed command sed '/^[[:space:]]*$/d'

I came across this sed command in a script to compare a machine's local binaries to GTFObins binaries. for i in $(curl -s https://gtfobins.github.io/ | html2text | cut -d" " -f1 | sed '/^...
M4raa's user avatar
  • 13
0 votes
1 answer
321 views

Rename file in Mac OS Terminal using Regex or translate from Windows Script

I would like help with a shell script for macOS to rename files with a certain pattern. The script would handle only one file, not multiple. Here are three examples: a. Public Talks_ (189) Walking ...
Allex Valentin's user avatar
0 votes
2 answers
86 views

Why does bash regex fail for meta characters? [duplicate]

I am trying to do a bash script to check if input file name is of format name.json. The regx in bash doesn't work but it works with grep. file t1.sh echo "$1" regx="^[\w\d-]+[.]json$&...
preetam's user avatar
  • 117
1 vote
5 answers
126 views

How might I swap the remaining parts of lines (with the exception of first characters) with sed?

I have a file, fr.fc that represents flashcards used by hardv that I want to use to generate with sed a similar file, fr-swapped.fc %% This is a card set for hardv(1) %% Created at Feb 1, 1997 NEXT ...
John Smith's user avatar

1
2 3 4 5
56