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.

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
95 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=...
0
votes
0answers
30 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' $...
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
81 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
23 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
30 views

Advantage of elasticsearch over grep when trying to search keyword in logs files

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
46 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
45 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
30 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
3k 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
864 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 ...
0
votes
2answers
56 views

Which regular expression string ignores single comment lines from a source code file? [closed]

I'm trying to parse a plain text file or a source code file for which I need to build a regular expression to detect single line comments. I'm trying the following one without success: re = sprintf("...
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
47 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
263 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
87 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
18 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
160k 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
31 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
120 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
66 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
34 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
428 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
24 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
91 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
34 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
426 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 ...
0
votes
2answers
54 views

RegExp: Trying to understand it. Making a query

I was running a few tests and thought of one I can see myself using frequently. So I have a few files around that I would usually source as they are common in a lot of things that I would do. I was ...
1
vote
2answers
307 views

Overcoming newlines with grep -o

I have a file similar to such. SampleOutput { "1": { "state": "RUNNING", "stats": { "name": "some-file", "uris": [ "some-example.com" ], ...
-3
votes
2answers
52 views

Unix shell : substring with regular expression [closed]

I have this text: 1234 I want to use a regex to select all except the first digit (in this case 1) - so a pattern that selects 234 How can I do this? I can't use variables, only regex. And also I ...
0
votes
4answers
62 views

How to extract the CSV file name from each line of this file

I have a file which contains lines like those: -rw-rw-rw 1 root root 6379 May 24 2016 test1.CSV -rw-rw-rw 1 root root 23249 May 25 2016 test2.CSV -rw-rw-rw 1 root root 2995 May 26 2016 test3.CSV ...
3
votes
2answers
484 views

Looking for duplicate instances of a tag in a file

Mutliple snippets of code exist in a file similar to the following: <blah>Spread the peanut butter <ramout assot="f0123_fun10" bapel="2 or 6"/> on good looking bread <ramout assot="...
0
votes
0answers
36 views

sed command to insert between two strings that are returned from another sed command

I have a YAML file that's about 25K lines and I have about 450 instances where I need to insert between two specific lines. Here's an example of the file entries: - changeSet: id: ...
1
vote
2answers
42 views

Regular expressions in grep

The man page for grep reads (emphasis mine) REGULAR EXPRESSIONS A regular expression is a pattern that describes a set of strings. Regular expressions are constructed analogously to ...