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

learn more… | top users | synonyms (1)

0
votes
1answer
36 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
60 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
36 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
166 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
88 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
76 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
82 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
25 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
119 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
4k 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
163 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" '\(abc[...
1
vote
1answer
53 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
145 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
353 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
64 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
72 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
52 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
56 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
201 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
79 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" regex="^[0-9a-zA-Z\,\!\-\^\`@\{\}\[\],=\(\)....
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
2k 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 ".*/(test)[...
0
votes
2answers
33 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
94 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 's/[0-9]/...
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
47 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
67 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 /cxf/...
6
votes
2answers
428 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
43 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
56 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
60 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 '<pattern>...
2
votes
6answers
422 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
86 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
128 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
318 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 result,...
2
votes
2answers
69 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 ...
2
votes
2answers
84 views

Replacing matching text after two matching lines

I have a YAML file which includes a stanza looks this: admin::common::passwords: alice: password: '$6$oTQhLvN/4VFJPscD$8LYwUMSFi' bob: password: '$6$JKOtLF0wHeZfIskt$W/M5.ugDS' If the ...
1
vote
2answers
50 views

Help with regular expression in grep

someone can explain me why this regexp doesn't work in grep? ls -la ./ | grep -E '^d.*\<\..*\>$' In my (wrong) opinion this have to show me the line of the directory with a name that start ...
1
vote
2answers
111 views

Find works fine in command line, but won't work in script

I am trying to write a script on a Debian Jessie host. I am having an issue with trying to run the following command (with a twist) in a script: find ~/* -path ~/FileSniper* \ -prune -o \ -type f \( ...
2
votes
2answers
156 views

Delete row if two patterns are found in the same line

I'd like to delete a line from a text file (input.txt) if two patterns (string1and string2) are found on the same row, using sed. I'm trying: sed -i "/\b\(string1\|string2\)\b/d" input.txt, but ...
1
vote
2answers
48 views

Regex on Postfix using Centos

I have a Centos server running only postfix. I want to filter out mails containing word gratis or grátis or grétis or grâtis. The second letter is the problem. So I used this rejex: /^Subject:.*gr....
2
votes
4answers
50 views

How to append a line that does not start with '<' to the previous line

I have a log which contains XML lines. Sample format below: <head> <body> <line> asdasd</line> </body> </head> I want to scan the log file and ...
1
vote
2answers
33 views

Java comments regex: egrep “(/\*\* | /* | \*/ | \*\*/)” text.txt

I'm trying to extract lines starting or ending a Java comment: What I have is: egrep "(/** | /* | */ | **/)" text.txt I noticed that this works for all lines (such as /* comment */) except ...
0
votes
2answers
73 views

Problems with regex in grep

I'm trying to remove malware code from a site using a script, I'm having trouble getting the regex right for the malware part. What I want to do eventually is to use sed in order to find the ...
2
votes
1answer
85 views

sed regex for capture group between delimeters

I have a two line file that I'm trying to get some info out of for a bash script using sed. # File Comment PrefixForInformation {information to be captured} I need to get the information between ...
0
votes
0answers
15 views

UnionFS with filtering to determine default save location

I'm wanting to set up a UnionFS mount which will save all files/directories matching a certain regexp (or similar pattern-matching system) to one location and all other files/directories to another ...
1
vote
4answers
108 views

Grep for a number in line

Can someone help me to get the below two outputs? Input /usr/IBM/WebSphere/7.0/UpdateInstaller/java/jre/bin/java -Declipse.security -Dwas.status.socket=48101 /home/axg009/usr/IBM/WebSphere/8.5/...