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

learn more… | top users | synonyms (1)

4
votes
2answers
875 views

Bash regex capture group

I'm trying to match multiple alphanumeric values (this number could vary) from a string and save them to a bash capture group array. However, I'm only getting the first match: mystring1='<link ...
6
votes
2answers
198 views

Match the next pattern which is less than the previous match

I've got a file with QR-code lines and I want to grep for only those whose subsequences do not increase in length. Example: This one is good because the next sequence is less or same as previous: ...
1
vote
3answers
198 views

How to match two lines at once in vim

I have a file that contains records with tab separated format but in two lines, I want to arrange records in one line with tab separated values using vim editor pattern match. Ex. Abc PQR ...
0
votes
2answers
60 views

Parameter expansion in grep

To retrieve strings of the following pattern from a text file populated with multiple entries: entry=G_1234 entry=G_5678 entry=G_2345 The following grep statement doesn't provide any result grep ...
4
votes
3answers
175 views

How to replace text in a column

I have a huge file (more than 2 GB) where data is like below. 12,324,32342,E:fsdsf,23432,34534,45345,324 13,3224,342,E:werwefsdsf,23432,34534,45345,324 121,3244,33442,E:,23432,34534,45345,324 ...
3
votes
2answers
65 views

Replace only on the first matching line with sed

Using BSD sed (no GNU extensions), how can I perform an operation similar to the example provided below, but where instead of the line number, the replacement is performed on the first line in which a ...
5
votes
3answers
246 views

egrep regular expression - same word in the beginning and end

I want to find all the lines that have the same word in the beginning and in the end of the line. For example: goodword fgdlakj 3t sfkl 43lk fkl goodword bad sfa;lk3t dgk;gs 34;kl ...
5
votes
3answers
267 views

OR in `expr match`

I'm confused as to why this does not match: expr match Unauthenticated123 '^(Unauthenticated|Authenticated).*' it outputs 0.
2
votes
3answers
63 views

How to extract particular field from rows from a file, based on matching subsequent rows

I am trying to replicate a workplace problem. I have a xml file like below [~]$ less -N sample.xml 1 <SOURCE BUSINESSNAME ="" NAME ="TABLE1" FOO="ABCD"..... > 2 <SOURCEFIELD ...
0
votes
4answers
46 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: ...
14
votes
1answer
744 views

How to “grep” for line length *not* in a given range?

NOTE: This question is the complement of this Q&A: How to "grep" for line length in a given range? How can we grep for lines that have less than 8 or more than 63 characters, none ...
0
votes
1answer
34 views

Regex on multiple files

Very new to regex and have a directory of files that I would like run this regex on but don't know how. help would be great. This is the regex: (?<=#).*
7
votes
5answers
1k views

How to find lines containing more than 100 characters and contains “if”?

So, probably I should use grep for this. As long as I need recursive search I should use grep -r. But then I don't know what should I do next ;) How can I do that?
1
vote
2answers
56 views

Use of \$ at end of variable name passed to grep

I was going through one of the shell scripts and found a line: ls $dir | grep -c "$NAME.${var}\$" I understand that $ would expand the variables NAME and var, but is it required to put \$ at the ...
1
vote
1answer
32 views

How do I keep a part of the pattern matched and use it to replace on BSD sed?

I have found a soultion for GNU sed, and I also found something like \1, but the Terminal tells me it's "not defined in RE". But what I want to do is this: If I have a string that looks like this ...
1
vote
3answers
96 views

How to sort blocks of lines with a specific line from the block?

I have a file with the data like this BEGIN hello2 5 world1 END BEGIN hello4 2 world5 END BEGIN hello6 4 END I want to sort the lines in the following manner based on the number inside the block. ...
1
vote
1answer
73 views

Add text between multiple lines with sed if previous line does not match pattern

I'm struggling to add <dl> tags around a definition list with sed, and perhaps there is an easier way to do it (I'd love to know). I would like to search a file for any lines that contain a ...
4
votes
2answers
74 views

sed string with bad chars

I have a random string with ugly chars in it: ÓˇÌ„ˇ™ÌÓ‹ÍÙ› ‹„ı That chars have to be eliminated. The Whitelist consist of: a-zA-Z0-9 -_* + ß ä ü ö () % @ € & = . and Space My first Script ...
0
votes
1answer
78 views

Regular expression not matching all possibilities

I am having a little trouble with a regular expression ... I am trying to parse an HTML file looking for certain text between two <br> tags ... between these <br>(text goes here)<br> ...
0
votes
2answers
23 views

using egrep & regex

Suppose I have the following text file called inputFile: hello HELLO Hello World wOrld asdgfsafd abc 123 1 3 5 a b and I want to print out the lines containing only one word(any sequence with ...
3
votes
2answers
105 views

Find Files not an regex whitelist

I have a huge Folder with thousands of Files. Some Files have some Characters in it, which are not allowed. (UTF-8 signs) So I have a white list of allowed characters and a beginning of a bash-script ...
16
votes
3answers
2k views

Vim - how to replace one new line \n with two \n's

In vim editor, I want to replace a newline character (\n) with two new line characters (\n\n) using vim command mode. Input file content: This is my first line. This is second line. Command that I ...
2
votes
2answers
145 views

Role switch of special characters in bash

On what basis does the role of asterisk keep changing? CASE 1: var1=abcd-1234-defg echo ${var1#*-*} # RESULT: 1234-defg CASE 2: stringZ=abcABC123ABCabc echo `expr match "$stringZ" ...
1
vote
1answer
44 views

Find value for a given key using grep in the string

How can I grep SSID value from the string below using grep? SSID: Home, BSSID: aa:bb:cc:dd:ee:ff I tried grep -oh "SSID:.*," and other combinations but to no avail. The reluctant ? quantifer ...
0
votes
1answer
108 views

Find regex for timestamp in filename not working

I want find mail files in user maildir, without mailfilter, dovecot-index etc. All mail files have unix timestamp in its filenames, I think it is the easiest way. I used this regex: .*\/\d{10}\..*. ...
1
vote
1answer
277 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
3answers
61 views

significance behind escaping parentheses in expr match

Can anybody give me the difference between the following two commands in the context of escaping parentheses, or refer some document that would clear my doubt. string=abcABC123ABCabc $ echo `expr ...
2
votes
1answer
67 views

Find and replace using parts of the found string in the replace string

Let foo be any string. I have to search a file for all occurrences of \LeftLabel{foo} and replace each occurrence with \LeftLabel{\textsf{foo}} but only if foo does not start with a $ ...
2
votes
1answer
49 views

Explanation for 'sed'

I have a huge text file called helo.lst in which every line starts with the format: #=GS N4TFW2_FUSC1/96-161 AC N4TFW21 I'm only interested in the N4TFW2_FUSC1 part. I need to extract this ...
1
vote
1answer
47 views

check if row contains characters

I have file that looks like This row contains characters this row to *-=?§/ this is the lat row of the file how can i only print only rows that contains some characters aka all but ...
0
votes
2answers
17 views

using matches of regex in vi global substitution

I am trying to edit a text file using vi where I want to replace the matches of [a-z],[A-Z] with: {matching value of [a-z]}{a space instead of a comma}{matching value of [A-Z]}. For example: ...
0
votes
4answers
130 views

regex - find invalid character

Using regex='^[]0-9a-zA-Z,!^`@{}=().;/~_|[-]*$' in below script file I am checking if a given string has any invalid characters or not. str="$1" regex='^[]0-9a-zA-Z,!^`@{}=().;/~_|[-]+$' if [[ ...
0
votes
1answer
63 views

Regex - script check for allowed characters

I need to check a string and make sure that it does not contain any character outside list of given characters. I wrote one script to test this: str="$1" ...
1
vote
1answer
56 views

What does a backslash represent when searching for decimals

I want to understand what the backslash represents in this command. grep "\.900983" table I know what the command does, it searches for the 900983 value in table, I'm just not sure of the purpose of ...
1
vote
1answer
847 views

Using regex quantifier with find [duplicate]

I've got a set of files in a directory, all of the format test[0-9][0-9].txt. If I run find . -regex ".*/(test)[0-9][0-9]\.txt" then all the files are shown, but if I run find . -regex ...
0
votes
2answers
32 views

finding how many options ls have

Just for practise i want to find out how many options does ls command have. I have written following piece of code man ls | grep -o -i '\-[a-z0-9]$' Why does this piece of code only printed -C and ...
3
votes
1answer
89 views

sed -e 's/^[0-9]//' does not work for the first line

The following is the text I want to parse with sed (Mac OS X 10.11.1 bash) 1 00:25:43,959 --> 00:25:46,502 Here you are, sir. Main level, please. I can delete the first line with sed -e ...
0
votes
0answers
48 views

Finding word with centain length containing characters from interval

I am trying to print all words that are 3 chararcers long and those characters are from [a-s] interval. I have tried following grep '\<[a-s]\{3\}\>' file.txt which outputs nothing , i have ...
1
vote
1answer
46 views

usage of apostrofe in grep rexExp

I have recently started learning regExp and found nice example of it in shell using grep to extract only anchor tags url e.g <a href="unix.stackexchange.com"></a> grep - 'href="[^"]*"' ...
1
vote
3answers
60 views

Is it possible to make sed doing some logic in the replacement part (regex group)

I have few sed command: to extract relevant information My file sample.log (format is ncsa.log) looks like: 2012_04_01_filename.log:29874:192.168.1.12 - - [16/Aug/2012:12:54:21 +0000] "GET ...
6
votes
2answers
416 views

Is sed \{x,y\} range greedy?

I would like to know if, for instance, \{x,y\} in sed will try to match as much or as little as possible characters. Also, can someone explain to me the bellow unexpected behaviour of sed? echo ...
1
vote
3answers
40 views

How to grep a heading in a manpage containing control characters

I have a manpage, and I want to find something in a specific section. Grep is having a hard time, and I figured out the issue was headings have control characters in them, e.g. SEE ALSO is really: ...
1
vote
1answer
47 views

REGEX and GREP — Search string including optional and static words

I am trying to formulate a regex expression for use with grep such that I find a static word present at the beginning of each line, followed by only one word from a list of options. That is, I ...
2
votes
1answer
34 views

Regex to match = followed by a newline so they both be deleted

I am working with the raw source content of a mail.app message in OSX, but results it gives me the text in quoted printable MIME Email encoding. so I need to remove all those strange characters to get ...
2
votes
1answer
55 views

How to use BRE or ERE regex to match a pattern which does not have a specific string

How to use a posix BRE or a posix ERE regex to match a line that doesn't contain 834 at any place in string? I want to find grep '!<pattern>' file which will be equal to grep -v ...
2
votes
6answers
340 views

Extracting IP address from a text and store it in a variable

I have a text file named abd shown below. 48878 128.206.6.136 34782 128.206.6.137 12817 23.234.22.106 I want to extract only IP address from the text and store it in a variable and use for other ...
1
vote
2answers
78 views

bash regex: asterisk metacharacter kills redundant newline characters

To test the basic functioning of the asterisk metacharacter in search operations using grep, I used a file (regex.txt) with the following contents: $ cat regex.txt 1 11 111 1111 11111 $ There ...
1
vote
3answers
119 views

How to grep all words starting with digit?

I am wondering how to grep all words starting with digit. So far, I have made this: egrep "^[[:digit:]]" tst But it prints me just a lines starting with digit. Thanks in advance with help.
2
votes
1answer
311 views

shell: The =~ operator does not work as expected

Consider the following script: #! /bin/bash line="confusing" if [[ $line =~ [[:lower:]]* ]] then echo "matches!" else echo "does not match." fi While the above produce the expected ...
2
votes
2answers
57 views

How to escape metacharacters for egrep like metaquote from Perl?

Perl has a function called metaquote() to escape all special characters for a regular expression. Is there an equivalent technique for egrep? Example: If I am searching for the string abc.def.ghi, I ...