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
votes
1answer
48 views

How to grep for a particular string?

I am trying to get a string that matches the following pattern: starts with foo and finishes with bar. grep -e "^foo*bar$"
4
votes
3answers
260 views

Extract substring according to regexp with sed or grep

In a (BSD) UNIX environment, I would like to capture a specific substring using a regular expression. Assume that the dmesg command output would include the following line: pass2: <Marvell ...
1
vote
0answers
53 views

sed command is replacing the replaced text?

I am writing a bash script to rename icons. Specifically I am trying to change "icon-close-m" to "icon-close icon-size-medium" in HTML files. (though it could contain any substring besides "close", as ...
1
vote
1answer
34 views

Why is this regex not working in awk?

I have a text file as such: Attribute 1.............. : attribute value Encode Date............................. : JUL 2007 Attribute 22076.......... : attribute value` I want to extract the JUL ...
0
votes
1answer
37 views

Multiple replacements using regular expressions in python

Can I use regex in python to have multiple types of substitutions? Like in this string "hello, this is me" I want to replace 'hello' with 'hi' and 'this' with 'its'. Can I do it in a single line? Or ...
0
votes
1answer
22 views

How to specify regex quantifiers with mawk?

I'm familiar with the concept of specified bounded regex quantifiers as follows: Quantifier Legend Example Sample Match {3} Exactly three times \D{3} ABC {2,4} ...
0
votes
2answers
44 views

Grep in bash issues

Sample string: --000000000000f104a6057d4a0a10 Here I need to get for sure: "--" Same files also contain a lot of similar strings; ------=_, --=_ , -----------, --< which currently also get ...
0
votes
2answers
24 views

split file lines by regex delimeter

I want to split each line from input file by a non-alphanumeric regex \W and print all the split chunks in the output file like so: Input file: www.wifi.in.ua YI-HondBrychka Output file: www wifi ...
0
votes
2answers
52 views

Can't replace my regex matches

I can filter files, I can can stream the matches of my regex ... However, I need to remove exactly that, from a large file. Regex:^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$ sed ...
0
votes
1answer
69 views

Regex expression for IP address/CIDR in bash

I want to check if the user input is a valid IP address/CIDR in bash and I am using regular expressions to do that. So valid CIDR should be 0-32 and for IP from (1-254).(1-255).(1-255).(1-255)/(1-32) ...
1
vote
2answers
94 views

How to mass rename files with regular expressions [closed]

I have a bunch of folders in a directory with files in them. Most of the filenames have region codes (retropie), and the scrubber will not match roms with single letter region codes (u,j,e,etc.). I ...
0
votes
4answers
88 views

grep IPs and port number into one line

Using grep and cut I have returned the IP and ports of the nmap scan result which looks like this: 192.168.1.221 80 443 192.168.1.223 25 Desired output: 192.168.1.221:80 192.168.1.221:443 192.168....
1
vote
2answers
44 views

grep for single back slash

I am trying to grep for the mount output to see the options for the root directory. Why the following pattern (whitespace before and after a slash) is not working (with and without extended grep) /...
2
votes
4answers
85 views

Removing (possibly nested) text quotes in command line

I need to parse large amounts text in command line and replace all (possibly nested) text quotes with spaces. Quotes are marked with specific syntax: [quote=username]quoted text[/quote]. Example ...
0
votes
0answers
29 views

Commenting out a specific host configuration in a Nagios configuration file

I have lots of Nagios configuration files like the one below in a directory, and I need to remove a host from monitoring. define host{ host_name nmo use aix-server alias hostgroups +...
1
vote
5answers
47 views

remove all lines with nullbytes/corrupt data

So I recovered a text file from an old hdd, but I failed to completely recover all of the data. The data that wasn't correctly recovered has returned as null bytes. How can I remove every line from ...
2
votes
2answers
41 views

how to find the real path of folder with regular expression

presto is distributed SQL query engine , and when we installed the presto then it should be created the following home folder /application/DB/presto/presto-server-0.216 so we can say that in ...
0
votes
1answer
43 views

Bash regex and IFS split

I have the following problem: I want to extract text that is inside brackets from a string (with or without the brackets). My string looks like this: STR="[1] [2][345] [678 9] foo bar" I initially ...
0
votes
0answers
44 views

Remove all words from a file except certain words

I have a file which represent a list of SQLs where caluses. I want to remove everything that is not the columns names, and, or and where(case insensitive). In addition if it can also remove new lines ...
0
votes
1answer
57 views

sed: escaped square closing bracket does not match

Goal: replace ![alttext](url.png) with <img src="url.png" alt="alttext">. The ! and (...) parts work fine, it's the [...] part that I can't get to work. As a simple example, I want to match ...
0
votes
2answers
82 views

Use FIND with MV to rename files. Delete the found string then prepending it to the filename?

find . -type f -name '*authors-name-string*' -exec bash -c 'mv "$0" "${0/authors-name-string/}"' {} \; I am trying to rename files nested in several layers of directories that contain an author's ...
-1
votes
2answers
41 views

* in awk (metacharacters)

Can you explain me how * works? I have an input: U 8649 275 Asia Canada 3852 25 North America China 3705 1032 Asia USA 3615 237 North America India 1267 746 -Asia ...
4
votes
1answer
378 views

Awk - metacharacters

What is the difference between [+-] and [-+]. It seems to me doing the same thing. And what does [A-Za-z-]+ do? Or I tried awk '/[^-]/' contries and I expected nothing as an output.
6
votes
4answers
398 views

Why does regex `“\.pdf”` match `/…/pdf…/…` in gawk, but not in mawk?

From How can I extract only the pid column and only the pathname column in the lsof output? awk '{ for (i=9; i<=NF; i++) { if ($i ~ "string" && $1 != "wineserv" && $5 == "REG" &...
1
vote
0answers
81 views

How to improve this 'sed' search & replace command?

How to search and replace recursively through multiple files in a directory, using essential tools installed in mostly any Debian/Ubuntu machine? There are multiple answers in Stack* where to find ...
-1
votes
1answer
49 views

How to grep out all rows not matching the given pattern

I have csv files received on Linux, they come with their own different patterns, one example here: $ head test.csv wampproduct,wamp_date,wampregion,region_search_phrase,wamp,date_pull,end_of_month_dt ...
0
votes
1answer
52 views

Loop through many folders and do calculations with files with similar pattern in bash

I have several folders each containing several .nc files. I want to loop through all folders and for each folder do some calculations (mergetime) using cdo on files that have similar pattern. So far I ...
0
votes
3answers
85 views

Regex that matches this list of files?

I had a user on our shared host who let their Wordpress install fall victim of an exploit that allowed for arbitrary file creation as the apache user. The files generated are obviously generated with ...
11
votes
3answers
2k views

Correct regex not working in grep

I have this regex: (?<=prefix).*$ which returns any character following string "prefix" and it works fine on any online regex engines (e.g. https://regex101.com). The problem is when I use that ...
0
votes
3answers
75 views

Modifying a shell variable with regex (bash)

I have a shell variable for example. a="big little man". How do I use regex in bash print out the variable with only the middle word capitalized? (big LITTLE man) I can do it by separating the ...
2
votes
1answer
26 views

Using OR within regex pattern of grep [duplicate]

I use the following pattern for regex match in grep (zgrep, as searching within compressed files) as zgrep -P '(?<=start).{20,120}(?=end)' * I need to add some terms with OR operator in the ...
1
vote
2answers
68 views

RegEx Question on Linux - Sorting Date [closed]

It is possible to ignore some of the fields in an regex expression. The issue is that I have an output of a sorted files from an FTP server. The problem is that FTP does not list the year from the ...
2
votes
2answers
30 views

procmailrc-Killfile for recipient addresses?

I want to block mails with certain recipient addresses using procmailrc (temporary addresses that now only receive spam). I found a code sample for using a killfile/blacklist, but it only applies to ...
0
votes
0answers
38 views

Change text color in PDF files

I want to change some Text-Coloring by regex search in one or more PDFs. for example i want to change the color of all appearances of "A[0-9]+.[0-9]" like A120.1, A600.0 and so on. is it even possible ...
0
votes
4answers
65 views

grep not working as expected

Given a file, "test.log" with the following contents: line1 Patient 123 45566 line2 Patient 432 line3 Patient 234 456 line4 Patient 321 line5 I am trying to select line 2 and line 4 with this ...
0
votes
2answers
42 views

Batch rename a lot of file using a number find in filename

I have hundred of file name with a number tag in filename like this: AAAA #12 SSSS.TXT BBB #231 CDF.TXT CDFSDAAAA #1 AAAASS.TXT and i want to rename so: #012 - AAAA #12 SSSS.TXT #231 - BBB #231 CDF....
0
votes
2answers
89 views

Regular expression to remove all punctuation except commas in regex awk

I have a variable local= "[ 'service center','New' ]" I have used the following awk code to remove the square braces and single quotes, local=gensub(/[\[\]']+/, "", "g", local); Expected O/P ...
1
vote
2answers
124 views

Reading in variables from a file to use in a bash script strips out \

So I am loading in a properties file to define some variables. In this example imagine it is a string mark!slash\ending\ called INPUT. INPUT is declared by loading in the properties file using: . ./...
1
vote
1answer
39 views

sed - why this script is incorrect

I'm reading the RegEx chapter in Classic Shell Scripting and types the example code. Here is the command: which changes the path "/home/tolstoy" to "/home/lt". $ echo /home/tolstoy | sed 's;\(/home\)...
2
votes
1answer
274 views

Regex search list of emails

So I have a long list of email and I want to search these based on just a few sections of the email string. I want to try and search these emails more efficently so I want to match the start of the ...
0
votes
2answers
60 views

How can sed replace in the range of nth line regex specified?

I am to replace using sed the 3th (or nth to be more general) occurrence in respective line with specified match regex. so far as i can is only to do the first: $cat file # Golden dictionary is a ...
1
vote
0answers
52 views

I need to search ps output for a match and then recursively for a second match [closed]

I need to find two matches in the output from ps. I am searching with ps -ef |grep mysql for: /bin/sh /usr/bin/mysqld_safe --defaults-file=/data/mysql/master/agis_core/etc/my.cnf after this match ...
2
votes
1answer
41 views

Replacing bad file, directory and subdirectory names using a regex pattern

I have a bunch of directory, sub-directory, and filenames that were created in Linux with the following pattern: YYYY - MM - DD T HH : MM : SS (I added spaces for clarity but no spaces are in the ...
2
votes
2answers
429 views

How can I do an “or” search with find? [duplicate]

Essentially, I want to know how to run 2 (or more) find commands in one - an "or" search rather than an "and": find . -name "*.pem" find . -name "*.crt"
0
votes
3answers
81 views

Non-greedy matching in sed [closed]

In a bash script, I have the following variable: file_name='this_is_the_hart_part.csv' Using var2=$(echo $file_name | sed -e 's/_{2}\(.*\)_{3}/\1/') I want to extract the substring "the" (between ...
-1
votes
2answers
71 views

grep pattern next two lines and discard the pattern with one line

I have a file something like this: dn: danan cn: danian cn: danian1 dn: danian2 cn: danian2 dn: danian3 cn: danian3 cn: danian4 all I want to do is grep the pattern "dn:" and print just the ...
0
votes
1answer
45 views

What are Interval Regular Expression and Tagged Regular Expression?

I was recently reading about sed when I encountered this topic. I need examples to understand the topic better.
-1
votes
1answer
31 views

how to capture details from nc

by the following command we can identify which ip are using port 2181 and the relevant port echo stat | nc localhost 2181 Zookeeper version: 3.4.6-91--1, built on 01/04/2018 10:34 GMT Clients: /182....
0
votes
0answers
36 views

How can I regex match-and-replace _all_ on-screen text?

I need a way to replace an on-screen pattern with another string, regardless of what places that text on the screen. Maybe I can live without user input being replaced, but anything that comes from ...
1
vote
3answers
93 views

Can't run parallel zgrep with regex

When I run this: find . -name "my.log*.gz" -print | \ parallel zgrep -e "\(text1\|text2\).*Exception" to find all lines that match /(text1|text2).*Exception/ in all files in the folder, I get this ...