Stack Exchange Network

Stack Exchange network consists of 174 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.

0
votes
1answer
28 views

sed/awk file data manipulation

I have many text files prepended with digits like this: 12 some text here some text here some text here Or sometimes like this: 123 text here some more not-so-interesting text here some text here ...
1
vote
2answers
28 views

Regex, delete all characters after the last occurrence of “/”

I have a list of pathnames like this in a file: /path/to/directory/one.txt /longer/path/to/some/directory/two.py /path/with spaces/in/it/three.sh I want to delete all characters after the last ...
0
votes
2answers
38 views

Replace same line in file 1 from file 2 with conditon

File 1: 19a9s c9019 5777 File 2: 99a9s 89019 10919 Expected output 19a9s 89019 5777 So in file 1 on line#2 it's start with letter c. which i would like to use it as condition if the line start ...
0
votes
2answers
49 views

Regex for sed/awk command

I would like to change the file content which i specified like below.I want to comment out all the lines which contains ".class" except the class name i provide. @MyClass1.MyClass2(value = { ...
1
vote
2answers
119 views

How can I use a bash variable as a regex argument to gsub() and match()?

I have a script #! /bin/bash awk -v pat="$1" '{ if(match($0, pat)) {print $0} }' "$3" awk -v pat="$1" -v rep="$2" '{gsub(pat, rep); print $0}' "$3" I would like to run it like myscript '\.\.+' ' ' ...
0
votes
1answer
22 views

What is the correct syntax for rsyslog's re_match()?

I'm trying to filter unwanted messages from a cron job (systemd) from rsyslog output. However rsyslog always complains about the second argument of re_match(). The filter rule I have is: if $...
3
votes
2answers
270 views

regex to add missing quotes

I am trying to add missing quotes at the ends of some lines in a text file. I find that the regex [^\"]$ suffices to find lines with missing terminal doublequotes and so tried the following ...
4
votes
4answers
362 views

sed: replacing entries in the /etc/fstab

I'm in the process of hardening some of our systems. As part of that hardening process, I need to update a few entries in the /etc/fstab to limit the capabilities of some of the various partitions. ...
5
votes
2answers
134 views

Perl command line regex to modify all pattern matches

I'm trying to use some arithmetic over the matched patterns in perl command line. I'm able to do it for one match but not for all. str="a1b2c3" perl -pe 's/\d+/$&+1/e' <<<"$str" a2b2c3 ...
1
vote
4answers
39 views

Bash regex whitespace before match

I'm trying to match a line in a textfile with if [[ ${regel} =~ ([\s][CN][G]{2}[A]{2}[T]) ]]; I also tried instead of /s to use /A and /b couple examples of things I tried: if [[ ${regel} =~ (\A[CN]...
1
vote
2answers
47 views

Matching exact number of digits in a string

Given a string, how can I find out that it contains exactly 7 consecutive digits number? Such as 1505082 or 1505082_CSE_322 but not 15050821 or 15050821_CSE_322 I've tried grep -Eq [0-9]{7} <&...
-1
votes
3answers
31 views

Removing first forward slash from string

I'm trying to remove the first / from a string in a shell script. For example file:///path/to/file and output to file://path/to/file
-2
votes
0answers
32 views

Splitting text files based on a semi-regular expression

I have a number of pretty large text file that I want to split into a bunch of smaller files (the number of files will vary file to file). All of them follow a the same pattern: "id": 999999,...
0
votes
0answers
29 views

What will match this regexp - ^[-w._()]+$? [closed]

As the title says, could I get a few strings that will match this regexp ^[-w._()]+$ Better still, a brief explanation - thanks! The reason I'm asking is that I'm trying to create a new docker ...
0
votes
0answers
21 views

How to iterate pattern by other one?

I have the bellow output. I try to iterate same times the Date and Time: Month Day Year HH:MM:SS for each Application n.n string found it. =========================================================...
1
vote
1answer
32 views

Debian: Remove redundant package terminology

When I install a LAMP server environment on Debians I use this code: apt-get upgrade lamp-server^ php-cli php-curl php-mbstring php-mcrypt php-gd python-certbot-apache -y If I add more php-X ...
0
votes
5answers
82 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=...
1
vote
1answer
22 views

Excluding certain files from moving but move hidden ones too

In my deployment script I have such as code to exclude certain files. shopt -s extglob mv !(deploy.sh|file1|file2) ~/$public It works almost well but ignores hidden files so they are not moved. How ...
-3
votes
2answers
71 views

Is pattern `^-0+([0-9]+)` the same as `^-0+([1-9]+)`?

Jeff wrote at errors in parameter expansion on filenames named after negative numbers My best direction at this point is to do something like [[ $b =~ ^-0+([0-9]+)$ ]] && b=-${BASH_REMATCH[...
1
vote
1answer
29 views

Getting email addresses from a file using grep with Regex not working?

So i have a file that contains emails like this : theirEmail@domain.com:theirUsername theirEmail@domain.com:theirUsername ... i want to extract emails that start with PA and then follow up with 6 ...
7
votes
4answers
934 views

BASH: Check in /etc/shadow if user password is locked

Objective: Check in /etc/shadow if user password is locked, i.e. if the first character in the 2nd field in /etc/shadow, which contains the user's hashed password, is an exclamation mark ('!') ...
0
votes
0answers
21 views

For a sed insertion, the line inserted does not have spaces, how can I add spaces? [duplicate]

#!/bin/bash sed -i\.backup $' /def test/ i\\ @unittest.skip("skipping") ' inputfile.txt If inputfile.txt contains class A(): def test_function(def) After running the script I would like to ...
0
votes
1answer
13 views

replace first semicolon occurence

Hi I have a logfile and I want to replace the first semicolon occurence, like: head1;head2;head3;head4;head5 blabb;blabl;;blab;blabl;;bla Desired output: head1;head2;head3;head4;head5 blabb;...
0
votes
3answers
68 views

Insert a line break before a numeric field or before an alphanumeric field that is just after a numeric field

I have this line word1 word2 1234 4567 word3 8901 word4 word5 2541 5142 word5 I want to split this line in order to insert a line break before a numeric field or before an alphanumeric field that is ...
0
votes
0answers
8 views

RegEx throws Error : Template variables could not be initialized [migrated]

I am using the below regex : ^(?!.*(ab1-canary|cd1-staging|cf-ab1-canary)).*$ ; This works well in python but fails to do so in GO language. Could you please help me in getting the right regex for GO. ...
0
votes
1answer
49 views

Syntax for regular expression searching for DNA codons

I have to write a script for an assignment which will take the file name as a command and output every unique 3 base codon in the file and how many times it occurs in descending order. The script has ...
0
votes
1answer
33 views

Bash check reg expresion one or more times

I want to check if the variable 'X' is in the following parttern [a-zA-Z0-9]+:[0-9]+:[0-9]+, I check it with if [[ "${X}" =~ [a-zA-Z0-9]+:[0-9]+:[0-9]+ ]] Now I want to add and option that this ...
0
votes
1answer
33 views

Find and replace a pattern using sed or awk [closed]

I have a file with multiple occurrences (more than 1000) of: REF*SY*111111111, REF*SY*777777777, REF*SY*145485450...etc. I need all the data be replaced with a standard output: REF*SY*111111111,REF*...
0
votes
2answers
25 views

extract field from file using sed or awk

I have bash script to collect all hardware info but following memory info piece is missing, so this is what i am trying to do. Following command give you status of DIMM memory module, [root@Linux ~...
4
votes
1answer
301 views

/(.+)\n\1/ works but /(.*)\n\1/ doesn't when they should both work

I was playing around with sed after answering another question and I noticed that .+ and .* are not giving the same result when they both match multiple characters in a context address. The following ...
0
votes
1answer
30 views

How to negate Bash's Regular Expression Matching Operator

Bash has the =~ regular expression matching operator. Here is an example of using it: #!/bin/bash input=$1 if [[ "$input" =~ "[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]" ]] # ^ ...
1
vote
4answers
47 views

RegEx for matching strings in 2nd and 5th columns using grep

I have a file (test.txt) that contains string such as: name1,6.5.8.12,specs1,version1,['66.55.89.12'] name2,19.13.13.159,specs2,version2,['13.13.173.59'; '14.19.232.42'] name3,141.101.196.40,specs3,...
0
votes
1answer
47 views

Grepping on du -h for all the files that are larger than 1GB

So my disk is full, I am trying to grep for all the files larger than 1G. I tried searching for the word Sort in the man page of DU, but found nothing. du -h . | grep -E "^\d*\.\d*G" and also this. ...
0
votes
2answers
48 views

Find and replace a word in all files based on file pattern

I have a variable called "User": "x-73098" in multiple files and the file names like astro_root_sys-dev.txt. How to find all the files which have "User": "x-73098" variable and replace that ...
3
votes
5answers
232 views

sed on OS X - extract all text that is between square brackets

Given this stream: [foo] 123 [bar] [gar] dsa [har] 345 [uf] 88 [gc] 43 [br] I want to process this with sed (or anything else) so the output would be: foo bar gar har uf gc br I tried cat myfile | ...
0
votes
3answers
49 views

Extract a string out of key, value from a text file, and assign to a new variable

Need a script to iterate through below file to read keys, values and extract values eg. foo_key1, foo.com, /tmp/worker${Build_Number} out of key and values assign them to another variable (eg. A="...
-1
votes
1answer
30 views

How to get specific files in SFTP using regex

I am trying to get files from specific range date (11-OCT to 15-OCT) using SFTP via SecureCRT: File name: PEM_CUST_REC_G0X_20181016XXXXXX.gz *20181016 represents the file date (YYYYMMDD). X ...
0
votes
2answers
26 views

find specific files and copy them to a directory in linux

abc-1.0.zip kncb-1.0.zip jenk-1.1.zip I want to find all the above files dynamically and copy them to a directory except jenk-1.1.zip. I have tried the below command but it also copies file(jenk*....
0
votes
1answer
13 views

`lvs -a` — filtering by name with regex

Is it possible to filter output of lvs -a using lvs command (I know that it is possible with grep, however I look for some approach with lvm only). I mean listing only snapshosts and/or filter by ...
0
votes
1answer
47 views

how to move a file after identifying it with ls | egrep , or “egrep” vs “find”

I need to move all images with a specific filename string to a specific directory. This is an example filename facility_92+SOURCE1+SOURCE1.0 After the 'facility_' there can be 1 to 5 digits. The ...
0
votes
2answers
88 views

Using regular expression for rm and ls

QUESTION: What is happening here and how do I fix it? PROBLEM: When I do: $ls | grep -P "file[1-9]{3}.txt|file[1-9]{2}.txt" I get: file11.txt file12.txt file13.txt file14.txt file15.txt file16....
-1
votes
2answers
41 views

How to save a number from a text file in python? [closed]

I have a text file, from which I have to extract a number, which is in the line called "width:" It is like: width: 3432 Now I am trying: import re file = ...
1
vote
1answer
46 views

Regex - Capture string following timestamp

Looking for an efficient Regex to capture the string following the time stamp in the following: <38>Oct 10 14:32:29 UAT01 <86>Oct 10 14:32:29 Test04 <13>Oct 10 14:35:09 Dev02 <...
0
votes
2answers
26 views

Comparing directory files based on filename

I currently have a directory of files that looks something like this: abcd.txt abcd_.txt qrst.txt qrst_.txt wxyz.txt wxyz_.txt In theory, every line in abcde_.txt should be contained in abcd.txt, ...
0
votes
2answers
32 views

Regex to separate capital letter words from title case words?

I have a string formatted like this: Meaningless Garbage I Don't Want INFO I'M LOOKING FOR The meaningless garbage and the info I'm looking for are of variable length, however the info I'm looking ...
1
vote
1answer
36 views

remove all spaces from specific field separated by comma

Input: 11 22 33 44,1 2,2 Output 11223344,1 2,2 I'm looking to remove all spaces in field number 1, which is before the first comma.
0
votes
1answer
30 views

log processing/entry deletion, multi-pipe (grep sed grep), log traversed twice. can this solution be improved?

Is there a cleaner way? A single pass of the large input.txt file would be preferred. Solution so far: grep -e "\[.*fred" input.txt |sed 's/.*\[\([^]]*\)\].*/\1/g' |grep -vf /dev/stdin input.txt ...
0
votes
0answers
59 views

grep: conflicting matchers specified

I'm using find with xargs and grep to find text which we need to translate in our application. find . -name \*.vue -not -path "./vendor/*" -not -path "./node_modules/*" -not -path "./public/*" -not -...
0
votes
2answers
58 views

Trying to learn regex, to find files of a certain length?

I have a file that has a list of countries of various lengths. This file is called countries and is in my working directory. I want to run a the grep command that tells me which countries have nine ...
1
vote
1answer
47 views

How do I extract overlapping character patterns with regex?

Is it possible to create a regular expression that would match overlapping character patterns? For example, I'd like to match abc, bcd, cde and def from string abcdef. If it is not possible, can you ...