Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [regular-expression]

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

5
votes
1answer
104 views

bash regexp matching fails in [[ ]]

I would like to check whether a user is in a certain group via [[ "$(getent group groupname)" =~ \busername\b ]] but this does not work (bash 5.0.3) although the following works: getent group ...
1
vote
1answer
22 views

Complex regex sed replacement not working but not throwing errors

I am working in CentOS 7 trying to do find / sed one liners to fix a ton of files. Specifically two in a row: First, to add "ignoreOlderThan = 14d" immediately after every [monitor://...] (working) ...
1
vote
2answers
24 views

sed: -e expression #1, char 35: unterminated `s' command

I have a text file with a line like this: SET @@GLOBAL.GTID_PURGED='abae51b1-e1dc-11e8-bfd0-5665b72f6318:1-12559259'; Using sed, I would like to remove that line. I have it partially working in that ...
0
votes
2answers
37 views

bash + regular expression + capture java version [on hold]

how to capture the jdk1.8.0_112 under /usr/jdk64 /usr/jdk64/jdk1.8.0_112/ while jdk1.8.0_112 is example and could be any other version as jdk1.7.0_112 jdk3.8.2_112 jdk1.8.0_117 jdk1.2.0_112 jdk2....
-2
votes
1answer
61 views

Bash Script general questions

The first part of what i am mucking around with simply creates a folder entitled command_manuals that contain text file copies of all the commands available at the bash shell, the filenames which are ...
-2
votes
0answers
20 views

Python line match [on hold]

I'm trying to collect the error report from client servers and store it in master server. If an error is reported then it is updated in a file in master server. This error depends on the type of error ...
-1
votes
1answer
53 views

How do I exclude pre tags from HTML minification using sed?

I am trying to minify HTML using sed My problem: I don't want to minify anything inside <pre> tags, but can't make it work.. Here is what I am using: sed ':a;N;$!ba;s@>\s*<@><@g' $...
0
votes
1answer
40 views

Grep: Find 3 consecutive char and then follow another char or end of the line

I want to find 3 consecutive characters and then another character or the end of the line. I tried this: egrep '(.)\1{2}' file but I need less results.
8
votes
2answers
4k views

Why does '[a-z]*' match non-alphabetical strings?

I have a file alphanum with these two lines: 123 abc this is a line I am confused as to why, when I run sed 's/[a-z]*/SUB/' alphanum, I get the following output: SUB123 abc SUB is a line I was ...
1
vote
3answers
1k views

Printing Characters after a Colon

How do I print anything after the colon? Input: color:white,name:green so I would like to print anything after : Output: white,green
0
votes
5answers
96 views

AWK from text with modification

I have the following file: hello there. let's try&ACCT=1&bla bla bla&EX=1118&anything ss &SERIAL=1011&DEAL=NO BLA BLA BLA&TOM=1&ACCT=2&LSLSLSLSL&none=1&EX=...
2
votes
3answers
4k views

Why is my regex not working using sed in bash script on Mac OSX?

I am trying to capture CBX-1234 from the string CBX-1234--CBX-5678 or CBX-12345--CBX-5678. I am using a bash script on Mac OSX using sed to run the regular expression. string="CBX-1234--CBX-5678" ...
0
votes
4answers
95 views

How to represent greater than or equal 3600 in regex

I wrote this regex to match numbers greater than or equal 3600. This is my attempt. However, I am not sure if it is complete: grep -P '36[0-9]+[0-9]+[0-9]*' test.txt I mean positive decimal integer ...
1
vote
1answer
2k views

Substring extraction with expr

I was working on extraction of sub-string from the beginning & at the end of the string. I experimented with and with out ".*" $expr "ab1cd1efgfedcbaAAAA" : ".*\(1[a-l]*\)" # Result: 1efgfedcba ...
0
votes
0answers
36 views

build regexp that does not depend on the order of input data

In my script I'm dumping some out some data and grep with regular expression rules, which assume that the data come in certain order. However I've found that the order of data may vary. So instead of:...
0
votes
1answer
47 views

Advantage of elasticsearch over grep when trying to search keyword in logs files [on hold]

I know elasticsearch is good for full text search but when it comes to analyze logs, say nginx logs what is the advantage of using it over grep (with tail, awk, etc.) ? Quite of often I dig through ...
0
votes
0answers
48 views

How can I find in a log a specific timestamp [duplicate]

This is something I really need on a daily basis and it's really hard sometimes I need to get logs from a specific date time from a log file. This is an example of the file INFO | jvm 1 | main ...
0
votes
4answers
46 views

how to capture IP address from info

by the following api we can get the info about master1/2 machines curl -sH "X-Requested-By: ambari" -u "admin"":""admin" -i http://192.23.39.2:8080/api/v1/hosts?fields=Hosts/host_name,Hosts/ip | ...
0
votes
3answers
31 views

Sed not outputting matches from regex to file

I have a file (data.txt) which contains strings like this: [?1h= => ["AD070517", "AD070518", : ESCESCOOBB "AD070809", "NE0000013", "NE0000014", : ESCESC[[66~~ "LG100085-097", "LG100085-098", ] ...
1
vote
3answers
4k views

Matching numbers with regex in case statement

I want to check whether an argument to a shell script is a whole number (i.e., a non-negative integer: 0, 1, 2, 3, …, 17, …, 42, …, etc, but not 3.1416 or −5) expressed in decimal (so nothing like ...
0
votes
1answer
16 views

How to express beginning and end of line in sed

I want to use sed to replace every '} that comes at the end of the line to "}. I wrote: sed -i -e "s/'}$/\"}$/g" But this replaces it to: "}$. What is wrong? How can I use regex in my sed to express ...
1
vote
2answers
15 views

Use multiple expressions in grep consumer

I have these bash functions filter_history(){ grep -v '^cd[^ ]|^ls[^ ]' } get_clean_bash_history(){ cat "$HOME/.bash_history" | filter_history } I am trying to get the bash history but filter ...
2
votes
2answers
867 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 ...
2
votes
1answer
94 views

Replace all items that are not belong to characters and numbers by ' '

I want to replace all symbols like '.','-','/' and so on by ' ' I triedsed 's/[[!:alnum:]]/ /g' example.txt but it does not work and for sed 's/[,;\.\-\/()_]/ /g' example.txt, I found that I cannot ...
1
vote
3answers
81 views

Why does `^[ ]{0,}` not work with linux grep?

This is my sample text. grep w, grep ^w and grep '^[ ]w' are working just fine. [user@linux ~]$ grep w text.txt whitespace 0 whitespace 1 whitespace 2 [user@linux ~]$ [user@linux ~]$ grep ^w text....
0
votes
1answer
31 views

Using Sed in a for loop with a regular expression

I am trying to modify a json file using a for loop to loop over a list of sample names and use sed to fix those sample names. Right now a subset of the input file is; "WA82671", "WA8267-...
1
vote
3answers
48 views

sed command using regular expressions to replace the major minor and release number

I am using Sed to replace the major, minor and release number in version. In my case: version=10.2.0.1 (version=major.minor.release.buildnumber) I don't want to change buildnumber in this ...
0
votes
3answers
26 views

Using awk to print 2nd column and rest of the text from #

I have these kind of lines in my config files, bindsym $mod+F2 exec gnome-terminal #Open terminal bindsym $mod+p exec command /some/path" #Popup Dictionary bindsym $mod+Mod1+l exec --no-startup-...
4
votes
1answer
86 views

Bash Script - Get result of `(.*)` (dot star) in grep regex

Let's say I have the following JSON string in a Bash script variable, DATA: { "id": 10, "name": "Person 1", "note": "This is a test" } I need to get the value of the name field. I have ...
1
vote
2answers
269 views

exec command in bash loop for webscrapping

Here is a simple script which is curling https://unix.stackexchange.com/ and storing the result into an array, which is working fine. #!/usr/local/bin/bash [ -f pgtoscrap ] && { rm pgtoscrap; ...
4
votes
2answers
88 views

Extract Sub-Directory Path from Partially Known Directory

Lets say I've got the following directory structure: base/ | +-- app | | | +-- main | | | +-- sub | | | +-- first | | | | | +-- tib1.ear | ...
2
votes
2answers
34 views

how do I find paraphraphs with forgotten punctuation

Take the example file this is line one of a paragraph that continues here and finishes with a full stop as it should. Now we have a second paragraph that continues in a new line, but the full stop ...
0
votes
1answer
19 views

How to match either `[` or `]` (brackets) with regular expression?

I am aware that pattern [abc] matches a single character that is either a or b or c in the regular expressions, but how can I match for either [ or ]? I don't know how to escape them properly.
83
votes
8answers
161k views

How to run grep with multiple AND patterns?

I would like to get the multi pattern match with implicit AND between patterns, i.e. equivalent to running several greps in a sequence: grep pattern1 | grep pattern2 | ... So how to convert it to ...
0
votes
3answers
47 views

Unintended line breaks in csv file

I have a csv file with some unintended line breaks. The file looks more or less like this: col1; col2; col3; col4 1a; 1b; 1c; 1d 2a; 2b; 2c ;2d # this should be ...
0
votes
4answers
240 views

How to remove a first character of that particular sentences from a list of output [duplicate]

grep /var/log /etc/rsyslog.conf | awk '{print $2}' Output: /var/log/messages /var/log/secure -/var/log/maillog /var/log/cron -/var/log/spooler /var/log/boot.log I want the result to be: /var/log/...
0
votes
1answer
43 views

sed / grep randomly fails to trim a string

I am using a bash script that will load content from a text file and extract data accordingly. Problem is that sometimes extracted data is preceded by a dot. Code: function str_extract() { pattern=...
0
votes
1answer
32 views

awk: illegal primary in regular expression vault-(?!etcd|operator)

Need to improve a regex for awk. It works on regex101. /vault-(?!etcd|operator)/ Should only match: vault-etcd-abc123 vault-operator-def456 vault-ghi789 <-- Tried: awk '/vault-(?!etcd|operator)...
1
vote
1answer
5k 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, ...
3
votes
1answer
123 views

'regex' and 'name' directives in find

What sort of regex syntax does -name option of find command expect? I was of the impression that it would be the same as for -regex but that doesn't seem to be the case. $ mkdir test && cd ...
0
votes
1answer
67 views

Complex string replace - multiple files, multiple different strings, must include certain text

I have a number of XML files (Atlassian Confluence space exports FYI), that I need to parse through to replace certain strings, as long as the line begins with a set string. Here is an example of a ...
0
votes
1answer
36 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
4answers
85 views

How can I delete everything but an alphanumeric pattern?

I have a file that has a lot of junk and special characters as well. I want to keep a particular alphanumeric pattern and ignore everything else - e.g AB123456789 - I want to extract only this keyword ...
4
votes
4answers
434 views

Use sed to encapsulate the first word of each paragraph with <i> </i>?

So obviously I am trying italicize the first word of each paragraph in a huge document. I think its relatively easy to add the prefix but how do I delineate just the first word for the postfix instead ...
1
vote
2answers
25 views

How to insert Line break regex

I have a file which contains one line like this test1.CSV test2.CSV test3.CSV test4.CSV... I want to put a line break after each .CSV file name like so test1.CSV test2.CSV test3.CSV test4.CSV ...
3
votes
3answers
94 views

Extract lines that have two or more dots

I need to extract (or count) the lines (in a file) that have two or more dots. The lines should not start with dot (it’s OK if they end with a dot), and there must not be two dots in a row (i.e., the ...
0
votes
1answer
37 views

How to capture two regex

In this command, I want to extract strings that are like *.com or *.net, where * is not a dot. I wrote this: grep -P -E '^[^.]+\.com$ |^[^.]+\.net$' example.csv I get this error: grep: conflicting ...
0
votes
1answer
52 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 ...
1
vote
3answers
431 views

Using sed to substitute a string containing variable integers

I have a text file and want to use sed to replace the following string (including the "s): " rel="lightbox[1846]" title=" with # The numbers between [ and ] are variable and change. I want to ...
0
votes
2answers
189 views

Making a Procmail regex recipe for malicious email body text

At work we recently began receiving emails with what looks like malicious code contained in the email body. I'm trying to create a procmail recipe that will use regex to match only the non-word ...