Questions tagged [regular-expression]

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

Filter by
Sorted by
Tagged with
0
votes
1answer
15 views

Multiline regex doesn't work

I'm trying to write a commit-msg hook for my git project that checks if commit message accord with a particular style guide. However, it looks like something works differently into the bash world ...
1
vote
1answer
30 views

sed command works with e flag but not with E

I have a string and I need to add #, in the beginning,i.e. convert [ -n "$ID" -a "$ID" -le 200 ] && return to #[ -n "$ID" -a "$ID" -le 200 ] && return. I can use below command: echo '[...
0
votes
2answers
25 views

removing a character in a certain context (using shell script)

So, I have a file with a list of names, like Thomas Newbury Calvin Lewis E. J. Frederickson Lamar Wojcik J.C. Lily Lillian Thomas And I'm eventually going to try and split these into a long list of ...
3
votes
1answer
168 views

Regular Expressions with `<` and `\?` strange matches

Assume, there is a simple test PHP script: <?php $a = ($argc == 2 ? $argv[1] : 10); for ($i = 0; $i < $a; $i++) { echo '.'; } echo PHP_EOL; Now, I do a grep or a conditional sed on the file: ...
0
votes
2answers
35 views

Replace characters after match with sed

I have a string: 172.16.1.5] "80 EE 73 2F 0B 40 I need to replace all the spaces after the ] " with a colon. Currently using sed -r 's/[ ]+/:/g' I get an output of 172.16.1.5]:"80:EE:73:2F:0B:40:, ...
0
votes
1answer
101 views

sed/awk/python replace all spaces between characters after string match

I want to delete all spaces between characters after an initial match. An example is exmpl 0 t h i s a r e t he spaces to d e l e t e exmpl 1 m o r e spaces to d e l exmpl 2 o r s m t h completely d ...
0
votes
0answers
48 views

grep the particular line with regex dataset

I have a dataset which contains regex like HELLO_[0-9]* and I have to grep HELLO_2018 from the above dataset and extract the particular line as my dataset contains thousands of regex patterns with ...
1
vote
1answer
81 views

setting variable from string in a file using sed and regex

UPDATE Edited the script slightly for a more specific regex, still same problem. Clarifying following questions, this is my exact script I want to get working (POC for a bigger script), test.txt is ...
2
votes
3answers
58 views

How to combine these two grep statements

I need to select the lines that are not empty lines and do not start with //. I can perform two separate grep commands as follows: To extract the lines that are not empty line: grep -v -E '^$' test....
0
votes
1answer
41 views

Zsh string pattern match to delete spaces

Trying to solve a code-golf problem using zsh, but my regex isn't working correctly. Requirement Given input string $1, delete all spaces immediately to the left of any ! character. Examples: ...
2
votes
2answers
900 views

Regular expressions with ExpectJ

I've been used the ExpectJ Java library to automate some jobs on a Linux server, however now I need to automate a job that sends a different text response for each execution. Here is the output: Last ...
0
votes
0answers
18 views

postfix regex conditions

I have a postfix body_checks that blocks all mails containing "daily_repport" : /daily_report/ REJECT The thing is that I want to allow mails containing that pattern "daily_repport" if it is to ...
1
vote
1answer
50 views

Replace text in big file using sed

I have a big file that contains a text will be replaced based on a regular expression passed to. The regular expression is tested and can find matching pattern, but when I use it with sed no text ...
1
vote
3answers
39 views

Using regular expression to get latest pip package version

I'm trying to parse the output of this pip install to get the latest package version. pip install package== ERROR: Could not find a version that satisfies the requirement package== (from ...
1
vote
1answer
26 views

using sed to replace string using info from matched string

I have strings in a text file with certain formats. I want to replace the word part of the string but keep the numeric identifier. For example, I have strings like L2_name1, L12_name6 but many of the ...
0
votes
1answer
42 views

I am having difficulties during manipulating log file with awk and shellscript(regex) [on hold]

I am very newbie in this field and very wondering around nowadays. I like to manipulate auth.log file into somehow easy strings to read through awk and bash shell script(regex with for or while loop), ...
4
votes
5answers
339 views

extract lines from bottom until regex match

I have this output. [root@linux ~]# cat /tmp/file.txt virt-top time 11:25:14 Host foo.example.com x86_64 32/32CPU 1200MHz 65501MB ID S RDRQ WRRQ RXBY TXBY %CPU %MEM TIME NAME 1 R 0 ...
0
votes
4answers
120 views

grep finding ipv4 as words instead of lines

I have to find the ipv4 in a file.The problem is if there are other words on the same line as the IP the script wont print it.Here is my script: #!/bin/bash if [ -e ip.txt ] then grep -E '^(([0-...
124
votes
5answers
131k views

How to use find command to search for multiple extensions

I can get all jpg images by using: find . -name "*.jpg" But how can I add png files to the results as well?
0
votes
2answers
53 views

Placing matching occurrence number to the left side of the occurrence itself

I have a file with multiple occurrences of the expression ".mp3". I want the first occurrence to become "1.mp3", the second occurrence to become "2.mp3", and so on. It would be something like awk '/ ...
6
votes
1answer
145 views

Determining the regular expression library supplied by the system

I was attempting to use the following regexp in less yesterday: ^\+1[[:space:]]*$, which worked for me in grep. This didn't work in less, so I checked the manpage to see what it does support, and ...
0
votes
2answers
27 views

Checking if string ends with a number throws “unexpected operator”

I'm trying to check wether the file name of the script I run ends with a number or not: #!/bin/sh name=$(basename "$0" .sh) [ $name =~ ^.[0-9]$ ] && numb=$(echo $name | sed 's/[^0-9]*//g') || ...
5
votes
2answers
14k views

“grep: Unmatched [” error when using regex

I'm trying to find a pattern similar to this: tail -n 100000 gateway.log | grep -B10 -A10 'Nov 22 11:13:56 Received Packet from [10.50.98.68' Where "11:13:56" could be any time. This is what I came ...
34
votes
4answers
41k views

Vim - how to replace one new line \n with two \n's

In vim editor, I want to replace a newline character (\n) with two new line characters (\n\n) using vim command mode. Input file content: This is my first line. This is second line. Command that I ...
0
votes
2answers
38 views

Replace a command with the result (command output) in the line using sed

Is it possible to replace a command extracted by some regex with its output using sed? For example, I have the next file: 2 + 2 = $$shell:echo 2 + 2 | bc$$ // and "some unescaped text' here how I ...
3
votes
2answers
88 views

Search For Three Consecutive Words

There are duplicates in my booklist (txt file) like the following - The Ideal Team Player The Ideal Team Player: How to Recognize and Cultivate The Three Essential Virtues Ideal Team Player: ...
-3
votes
1answer
49 views

How do I convert [*] into a regular expression? [closed]

Essentially I need to Ctrl+f (search for) the following string... [*] This means that I want to find everything that matches between (and including) the brackets. What do I substitute the "*" for ...
3
votes
3answers
93 views

Exact number of same characters in word-grep

My task is to find lines with egrep in which first word contains exactly three same letters. I've tried to use backreferences but only found a way to build pattern which finds words builded from 3 or ...
0
votes
0answers
21 views

How to match a prefix in Bash shell scripting using the `=~` operator in `[[`? [duplicate]

I want to match a prefix using the =~ operator in the [[ compound command. However, the below example yields "false" everytime it's executed, although man bash specifies that the RHS of =~ is a POSIX ...
0
votes
2answers
32 views

sed substitute one line into a part of the next line

I have input like this: *123456789 This is a <secret> Other stuff here. *987654321 This is a <secret> Other stuff here. where "Other stuff here." could be one more lines, but the number ...
16
votes
2answers
18k views

How to use regex as field separator in awk?

I'm trying to use regex as a field seperator in awk. From my reading this seems possible but I can't get the syntax right. rpm -qa | awk '{ 'FS == [0-9]' ; print $1 }' awk: cmd. line:1: { FS awk: ...
0
votes
2answers
83 views

Grep not understanding whitespace delimiter?

In the following example: apt-file search apache2.conf | grep -E "apache2.conf\b" Output: apache2: /etc/apache2/apache2.conf apache2-doc: /usr/share/doc/apache2-doc/examples/apache2/apache2.conf.gz ...
3
votes
3answers
44 views

regex from two files

I have two files that built of blocks of 4 lines, starting with a @. They are matching and in order. The files are too big (>10 GB) so I am putting here the first 3 blocks of each (12 lines). 1st: ...
1
vote
2answers
25 views

strange regex matching with grep/egrep

GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu) grep (GNU grep) 2.20 grep-2.20-3.el7.x86_64 Can someone explain this puzzle? I'm getting false matches with grep/egrep. echo "...
0
votes
2answers
64 views

How to list all mounts that start with a given prefix/root

I am trying to look through /proc/mounts and return all mounts that start with a certain prefix or base directory, eg. /run/media. Right now one prefix is fine, but in principle it would be nice to be ...
3
votes
3answers
73 views

Output only some part of a log line using grep

I have lines like these - /mnt/internal-app/logs/internal-app.log_2019-08-21.log.gz:2019-08-21 07:31:14,153 5458142 [XNIO-3 task-4] INFO c.c.p.i.m.ws.FileManger [FileName.java:1838] - UUIDs in this ...
3
votes
1answer
42 views

Bash sed replace double dollar sign $$ extended regular expressions

This command: echo '$$foo=bar' | sed -E "s/(\$\$foo=).*/\1$(echo hello)/" outputs: $$foo=bar If I change the sed's quotes to single and enclose the command in double quotes: echo '$$foo=bar' | ...
1
vote
1answer
6k views

Ansible, attempting to get specific regex pattern to work with lineinfile module

As per my question above: "Ansible, attempting to get specific regex pattern to work with lineinfile module" Essentially, I'm trying to get it to work in a single play as opposed to 2 separate plays, ...
2
votes
1answer
44 views

Using subset in sed for replace with regex

Consider some data that looks like this: "2019-12-12 00:00:01","2012-01-01 01:01:01" I wish to replace it so that they are valid datetime json values: "2019-12-12T00:00:01+01","2012-01-01T01:01:01+...
6
votes
5answers
290 views

How to refer to a regex group in awk regex?

How can I refer to a regex group in awk regex? For example, if I have a regex group (\w), how can I refer to it later in the same regex like (\w)\1? Does awk support this feature? Below example doesn'...
0
votes
2answers
37 views

Solaris - Get directory names that starts with a pattern

I'm working on Sun Solaris 10 OS, which is grep -o is not allowed with modern systems. I have the below list of directory I'm trying to fetch any directory starts with DATA[0-9] only DATA34016 ...
25
votes
7answers
18k views

How to select first occurrence between two patterns including them

How can I select first occurrence between two patterns including them. Preferably using sed or awk. I have: text something P1 something content1 content2 something P2 something text something P1 ...
0
votes
0answers
37 views

How can I make / \ replacement more efficient in console?

In a Windows/Linux environment, I'm having trouble with copying and pasting file system links to AND from! Right now, I use Gitbash (uses / for folder/subf ) which has been helpful. BUt it wants ...
2
votes
1answer
102 views

Sqlite: replace a string modelID1/modelID2/modelID3 with name1/name2/name3/

My goal is to read the macOS Photos.app database to find out the paths of the included photos. The album and the parent folder whose name is in the RKFolder table can be determined for each photo. ...
4
votes
1answer
49 views

Rename files and directories with French characters

I use the following command on Ubuntu with rename (installed with sudo apt-get rename) to rename all files which have the given characters in regex: find . -execdir rename 's/[^A-Za-z0-9_.@+,#!?:&...
1
vote
1answer
22 views

how to apply multiply regex-find & replace commands on one text file? [closed]

I have one textfile (written in markdown) to which I want to apply a bunch of regex in order to find and replace certain structures. Can you give me a hint how to approach this issue? Can I write a ...
1
vote
2answers
35 views

sed + delete all words that not contain string before the “dot string”

I castrated the following sed syntax in order to remove from file any host that not contain linux/LINUX sed -i '/linux\|LINUX/!d' file example linux01.moon.com solaris.moon.com UNIX.foot.com it ...
0
votes
1answer
73 views

Find and replace the columns of matched and non-matched lines with a character

I have a csv file with a bunch of numbers ranging from 500 to 500,000 and would like to replace the matches with 1 and mismatches by shifting the mismatched content to right simultaneously shifting ...
0
votes
2answers
49 views

Character class regex confusion

I'm trying to abbreviate a regex in a grep. I need to match exactly six spaces followed by an alpha character. This works: grep "^\s\s\s\s\s\s[[:alpha:]]" <filename> This does not: grep "^[[:...
0
votes
0answers
33 views

Large file processing performance

I did a simple performance test of csv processing a while ago, and want to share results with community, maybe you can point out, what test could be more precise and fair. Firstly i took out 42 MB ...