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
The results are in! See what nearly 90,000 developers picked as their most loved, dreaded, and desired coding languages and more in the 2019 Developer Survey.

Questions tagged [regular-expression]

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

-2
votes
2answers
41 views

Unix shell : substring with regular expression

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
0answers
31 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
28 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 ...
0
votes
0answers
46 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
4answers
52 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 ...
-1
votes
1answer
29 views

\back slash charectar and . in grep

I am confused about the backslash character \ and single quotations inside grep. From my understanding it preserves the literal meaning. For example if I do echo 'This is \., it gives me This is \. ...
2
votes
1answer
171 views

Regex in IF condition in awk

I have awk script file as below. I need to add another condition in the if statement to check if the string contains atleast one alphabet. How can I add the extra condition to the present if statement?...
0
votes
2answers
34 views

grep STOP regex replacing -A option

I'm looking/searching for a way to make grep stop when finding the "|_" specific string after having found the search string. for example, i want to print ONLY the lines starting 2 lines above "...
3
votes
2answers
693 views

Merge two greps into single one

I need to extract an ID from the output of another command. Currently my extracting command looks like: someID=$(command | grep -oP '(?:^Successfully\sbuilt\s)([\da-z]{12}$)' | grep -oP '([a-z\d]{12})...
3
votes
2answers
56 views

Using regular expressions in “less”

I'm trying to use a regular expression in the man page of Bash by using less. I press / in less to enter a pattern, and I type z and press the Enter. I expected it to not match upper-case z (Z), but ...
3
votes
0answers
44 views

Renaming files with vidir (moreutils)

I'm trying to use vidir from the moreutils package and I have the following issue: When I load the files I want to rename with vidir like this: $ vidir or like this: $ find -type f | vidir --...
0
votes
2answers
39 views

Need regex to replace the whole line, but the last optional character

It sounds like it should be trivial task, but I cannot figure it out, so I decided to ask. I need to replace the whole line, but optional last character. For instance, I want: abcd to become efgh but ...
0
votes
0answers
24 views

Rename multiple files, removing text before prefix [duplicate]

I have 70 ish csv files that look something like: C03-10dfg-PI-35.csv C03-fgfg-PI-36.cvs C03-dfdf-PI-37.csv .... .... .... I want to rename all files to remove everything before PI. Outputs: PI-35....
0
votes
3answers
49 views

In a bash shell script, how to rename multiple files, keeping only the numbers and the extension of the original file names?

I have a directory with a bunch of files with names like this: generic name- dd -01 _.txt generic name- dd -02 _.txt generic name- dd -03 _.txt generic name- dd -04 _.txt And I want to rename all of ...
4
votes
2answers
94 views

When and why did ^ and $ take on their meanings of “beginning of line” and “end of line?”

Many command-line tools (grep, flex, etc.) use the ^ symbol to denote "beginning of line" and the $ symbol to denote "end of line." When did this convention arise? It seems perfectly reasonable to ...
-2
votes
1answer
53 views

How to match string in file

I'm trying to reproduce this code in Python to Bash import re w = open("filetest.txt") for item in re.findall(r'STRING:\s*(.+)"', w.read()): print item In Bash, but I do not know if it's correct, ...
-2
votes
1answer
41 views

grep to find lines that contain six or more non-vowel characters in a row

Using grep and regular expressions, I need to output all lines in a file that contain at least six characters in a row that aren't vowels. This includes any consonants, punctuation, numbers, etc.
0
votes
3answers
62 views

How to test if a folder exists using regular expression in Bash?

as all know we can check easily if folder exists by the following bash command [[ -d /opt/presto-server ]] && echo folder exist but when we have folder as /opt/presto-server-32.34 then how ...
2
votes
1answer
35 views

Recursively find directories but do not print subdirectories of matches

Considering the following directory structure: dir1 [domain.com] foo dir1 subdir1 subdir2 FooFile dir2 [domain.com] foo dir2 subdir3 [domain.com] foo dir3 subdir4 ...
1
vote
2answers
49 views

append brackets to all constants beginning with L:: where it not exist on end

I have many PHP Files where I need to add on end "()" to every constant beginning with "L::" which don't have on end "(". It should by applied to all *.php including subfolders) : L::WHAT_EVER <?=...
0
votes
2answers
76 views

how to deal with sed if Argument list too long

Trying to apply the following command: sudo sed -i "s/auth-user-pass/auth-user-pass pass.txt/g" *.ovpn But i keep getting the following error: sudo: unable to execute /bin/sed: Argument list too ...
-2
votes
1answer
41 views

Is there any initiative to transition all shell globs into standard regex?

I see many imagination between shell globs (wildcards) syntax and standard regex. Is there (or was there) any initiative to transition all shell globs into standard regex? By that I mean, using ...
0
votes
4answers
51 views

how to let part of the regex (that is variable) be matched literally, ignoring control characters?

I got some filenames containing characters that are regex special control ones. I need to prepare a regex that considers all these characters literally. Simplified test case: strFilenameOnDB="...
-1
votes
3answers
70 views

What does the following number-processing sed code mean?

I came across a code in script which add ',' after every 3 digits from backwards. the code only considers numeric data. Following is the code. sed 's/\(^\|[^0-9.]\)\([0-9]\+\)\([0-9]\{3\}\)/\1\2,\3/...
-1
votes
1answer
53 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
291 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
58 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
37 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
42 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
25 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
47 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
27 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
53 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
98 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
103 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
90 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
72 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
90 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
30 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
50 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
46 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
66 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
45 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
74 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
95 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
381 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.
7
votes
4answers
415 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
82 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
52 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 ...