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

learn more… | top users | synonyms (1)

4
votes
6answers
122 views

Append something to each list in a file

I have a file, lists.txt, that looks like this: // stuff at beginning of file var list1 = new Array(); i = 0; list1[i++] = 'a'; list1[i++] = 'b'; ... list1[i++] = 'z'; var list2 = new Array(); i = ...
1
vote
0answers
19 views

Interpreting regex wildcards with grep [duplicate]

This is a really simple question, but even after reading about wildcards/regex, I can't seem to grasp entirely what the differences between these commands are: grep . test.txt this one will look ...
1
vote
2answers
27 views

Regex for surrounding every word that contains & with $…$

I am thinking what is a robust approach for this. I try to match unsuccessfully by: [A-Za-z]*&[A-Za-z]* Which returns: so the mistake in \\&\hskip environment. How can you match ...
5
votes
3answers
190 views

History of Bash globbing

Is there a historical reason why Bash "globbing" and regular expressions are not identical? For example, I believe that in Bash [1-2]* matches anything that starts with a 1 or a 2 followed by anything ...
1
vote
2answers
54 views

How to do operation on individual matches in a full line

I have a Verilog line like this: if (i2_0&!(i2_1)) (posedge i0_0 => (o:1'b1))=(0, 0); How can I convert it to a line like: if (i2_0 == 1'b1 & i2_1 == 1'b0) (posedge i0_0 => (o:1'b1))=(0, 0); ...
40
votes
6answers
54k views

How can I use sed to replace a multi-line string?

I've noticed that, if I add \n to a pattern for substituting using sed, it does not match. Example: $ cat > alpha.txt This is a test Please do not be alarmed $ sed -i'.original' 's/a test\nPlease ...
1
vote
1answer
53 views

what is meant by “^$1” in shell script?

#!/bin/sh #emp3.sh: using if and else # if grep "^$1" /etc/passwd 2>dev/null then echo "pattern found - job over" else echo "pattern not found" fi in the above programme what is "^$1" and ...
1
vote
2answers
27 views

To Regex Pandoc Tables

Minimum Data to regex \documentclass{article} \begin{document} ------------------------------------------------------------- A B C D Header Aligned Aligned ...
0
votes
1answer
38 views

grep searching groups

I'm trying to learn to use grep groups, like sed \1\2\3, but have a problem. For example I filtering /etc/services file to separate all ports. What I do: ~$ grep -E '[0-9]{1,5}/(tcp|udp)' ...
2
votes
0answers
17 views

Why does find not match repeat / quantifier expressions with emacs regular expressions? [duplicate]

I have a file 'abcd' in my current dir. Using find -regex '\.\/[a-d]\{4\}' gets me no results. While the regex works fine in emacs itself. find, however, does find my file when I add -regextype ...
4
votes
3answers
179 views

RegExp testing with ash shell (BusyBox)

I need to do a RegExp pattern test on a certain bit of user input. This is the pattern I need to test the value against. ^([a-z]{2,3})\-([a-z][a-z]*[0-9]*)\-(\d+)$ An example match would be: ...
0
votes
3answers
32 views

How to match and replace an attribute value inside a tag

I am trying to replace the value of following node using sed command. <KeyValue key="EMPTY_SEARCH_CRITERIA" type="String" value="CS_CODE" comment="Configured empty search criteria."/> I am ...
0
votes
1answer
34 views

How to replace multiple occurrences of a pattern with sed

I use the following command to remove surrounding spaces between array keys that are strings or integers in PHP files: sed -ri "s/\[ ([0-9]+|(\x27|\x22).*(\x27|\x22)) \]/[\1]/g" file.php I thought I ...
6
votes
1answer
137 views

Why can't I escape the braces “]” in sed?

Question: Why can't I escape the "]"? $ printf "*#$@~\!&*()[]\n" *#~\!&*()[] $ printf "*#$@~\!&*()[]\n" | sed 's/[*#$@~!&*()\[\]]//g' *#~\!&*() $
2
votes
2answers
43 views

Can't get GNU grep 2.12 to handle LF character in regex expression

I've been at it for over an hour, but can't find why it's not working. I'm searching an HTML document for "\nSome text". Simple enough. But he following commands won't work: grep -Po ...
0
votes
1answer
35 views

Use sed to only replace namespace in an element that has a parent element with a given attribute value

I want to replace the namespace in an xml file using sed (I've given up trying to do it from ant, where it is required). The main issue is that that there is a repeating list of elements - not all of ...
2
votes
1answer
18 views

Seeing all regex matches in a file in Bash

I'm in bash and I have a file, I want to find all matches of a regex in it and have them printed. How do I do that? Followup: Assume I want to print only unique matches, i.e. if a match appears ...
2
votes
2answers
88 views

Extract substrings from each line. These substrings are in comma separated, “name=value” format

I have lines in a file that are comma separated. There are no column headers, it's mostly comma separated 'name=value' pairs. Here is some test data: ...
2
votes
2answers
95 views

How to escape correctly this word on vim?

I want to substitute this word ${ARRAY1[@]} with $1 on vim substitution command I did :%s:\$\{ARRAY1[\@]\}:\$1:g But give me error about number of repetition,i try also "" but doesn't work How to ...
7
votes
3answers
141 views

How to use [\w]+ in regular expression in sed?

I am on Windows, but I guess my question is still rightly placed here. C:\Users\User>grep --version GNU grep 2.6.3 C:\Users\User>sed --version GNU sed version 4.2.1 I noticed that the ...
1
vote
2answers
45 views

Delete text from a file using regular expressions

I have lots of text in a file. I want to delete all text that are between symbols === and <!!!>. E.g. Text ===some comments<!!!> lala ===aaa<!!!>" should be Text lala after running ...
0
votes
3answers
31 views

To grep version number from Wordpress plugin folder

What will be grep regex to extract version number like Version: 3.1.5 * Version: 3.1.5 Will output 3.1.5 But It should not catch likes MIME-Version: 1.0\n Here is my grep command grep ...
0
votes
3answers
41 views

What characters should I escape while modifying a file using sed? [duplicate]

I am using the following sed command(with word boundary): sed "7 s/\<<Filevalue=.*/>\>/<Filevalue="true"/>/" < config.xml And I am getting this error: sed: command garbled: 7 ...
1
vote
2answers
113 views

sed: problem while replacing string using wildcard

I am trying to replace a string using the following command: sed -e "s/\<$5\>/$6/g" "$2/$1" > "$4/$3" $5-> "hostname=*" $6-> "hostname=int1" $2/$1-> "source file path" $4/$3-> ...
1
vote
1answer
30 views

nginx location regex with proxypass

I am quite new to nginx so excuse me in advance if i am asking something obvious. What i am trying to do is to proxypass to another server a request that comes to my nginx server in the form of: ...
9
votes
7answers
970 views

How to merge text of alphabetic lines with the numeric lines in shell?

I Have a file that has text like this: AAAA BBBB CCCC DDDD 1234 5678 9012 3456 EEEE 7890 etc... And i want to match up the Alphabetic lines with the Numeric lines so they are like this: ...
0
votes
3answers
24 views

To improve this regex about digit separation

Code m/([+-]?\d+\.\d+)[\s\t]+([+-]?\d+\.\d+)/ Data 22.585698 3601.767090 D:\measurements\LabCourse_2015\6\90deg\image0001_23_mT.png 20.051883 3580.268555 ...
0
votes
1answer
24 views

Filtering Log Output on Separate Lines

I did a recursive grep in a directory to find all files that would match as follows grep -ER "match_string1|match_string2" /path/to/dir/ What I get as output is: ...
1
vote
1answer
43 views

How to write a regex to look for strings with fixed length with at least one occurrence of a character

I've been doing some reading on regular expressions and thought I had everything covered, until recently. I've had to filter out all strings of length 5 which end with the letter t and contain at ...
0
votes
0answers
28 views

Regex for a non-fixed pattern

I am trying to come up with the right regex (compatible with python) that would match a file that contained sample input as follows: On the third day of Christmas my true love sent to me: three ...
6
votes
4answers
147 views

Extracting subset of lines of a file based on regex for first & last line

I have a large text file and I only want to look at some of the lines. The first line I want matches a regex R, and when the line matches the regex S, I don't care about that line, or any following ...
4
votes
1answer
83 views

How to tell “grep -w” what characters make up “words”?

$ printf 'asf .test. afd\nasaf foo-test asfdads\n' asf .test. afd asaf foo-test asfdads $ printf 'asf .test. afd\nasaf foo-test asfdads\n' | grep -w test asf .test. afd asaf foo-test asfdads ...
13
votes
6answers
388 views

/bin/dash: check whether $1 is a number

What would be the best way to check whether $1 is an integer in /bin/dash ? In bash, I could do: [[ $1 =~ ^([0-9]+)$ ]] But that does not seem to be POSIX compliant and dash does not support that
7
votes
3answers
2k views

Print all lines which not containing punctuation

I want a regex pattern which can print all lines which not containing punctuation : Input : .This is line 1 This is ! line 2 This is line (3) This is line 4 Output: ( should be) This is line 4 ...
3
votes
1answer
22 views

Find repository names from gitolite info output

How do I extract the names from the gitolite info command output, for further piping into a script? I'm writing a migration script to migrate all my repositories from gitolite to a Gitlab server. ...
2
votes
1answer
37 views

grep with regular expression

I want to match all the lines that are of the below pattern in a file using grep. Abc::xyz() [18-Feb-15 12:09:16] I came up with below regex with grep , but it is not returning match. Please help ...
4
votes
4answers
306 views

How do I extract parts of a string to variables?

I have a line from some file like this: attempting to create a 512^3 level (with Dirichlet BC) using a 16^3 grid of 32^3 boxes and 800 tasks... I want to extract the 512^3, 16^3, 32^3 and 800 four ...
1
vote
1answer
36 views

sed exception handling of escaped characters

I want to delete anything that follows a % to the end of the line. Using cat /tmp/foo.txt | sed 's/%.*$//' works great with one exception: I want to ignore any escaped percent signs \%. So with ...
2
votes
1answer
48 views

Using fixed strings in regular expressions?

I am writing a bash script which accepts a list of CSV files as arguments and outputs e-mail addresses only found in the first file. To accomplish this, for each record in the first CSV file I look ...
11
votes
7answers
17k views

How can I “grep” patterns across multiple lines?

It seems I am misusing grep/egrep. I was trying to search for strings in multiple line and could not find a match while I know that what I'm looking for should match. Originally I thought that my ...
1
vote
1answer
133 views

Sed command that would ignore any commented match

I'm trying to create a sed command using regex in order to substitute something in a text file only if it is not commented but I'm running into some troubles due to my almost non-existent knowledge of ...
2
votes
2answers
39 views

Perl non-greedy regex matching more than it should

I have the following text from which I need to remove some wdiff output. text='Иса Мәсіхтің елшісі Петірден [-(-] осы күнәкар дүниеде [-)-] жат жерлік болып, Понти, Ғалатия, [-Қападоқия, Азия және ...
0
votes
1answer
82 views

Bash detect ascii art [closed]

For bash script, is there a easy way to detect if a text contains ascii art? An example of ascii art:
2
votes
0answers
95 views

Bash regular expression comparison fail for hex byte \x01

Bash regular expression fails to compare correctly for $'\x01' when End-Of-String char $ is used. All other byte values (seem to) compare correctly. Using GNU bash 4.1.5(1). Is this a bug, or is ...
0
votes
1answer
42 views

What is the email matching regex in basic regex for grep?

I created a text file and put some email addresses in it. Then I used grep to find them. Indeed it worked: # pattern="^[a-zA-Z0-9]+@[a-zA-Z0-9]+\.[a-z]{2,}" # grep -E $pattern regexfile but only as ...
0
votes
1answer
45 views

Vim: Count all occurrences (including multiple on the same line) of char or word

I try to count all occurrences of a char or word in a file with Vim. This means multiple occurrences of the same word in the same line should be taken into account as well. This has been discussed in ...
9
votes
3answers
733 views

Strange behaviour of “ls -a | grep ^\.”

I wanted to list the content of a pwd and display only file starting with dot. I tried ls -a | grep ^\. but I cannot figure out why the output contains also the files which do not start with dot. For ...
1
vote
1answer
48 views

Find only in directories n levels deep

I use this command to find and run all istances of wp-cron.php I have for each site I have: find /var/www/ -name 'wp-cron.php' -exec php -q "{}" \; Since my websites tree is: ...
0
votes
1answer
26 views

Interactive less search with wildcards [duplicate]

I'm using less to view a large file and I want to search for a pattern with wildcards — e.g., " x 1??" where ? is any character. Does less support that, and if so how would I do that ...
5
votes
5answers
372 views

How to replace a word with new line

I have a text file with following data and each row ends with |END|. T|somthing|something|END|T|something2|something2|END| I am tryig to replace |END| with \n new line with sed. sed ...