Regex to match only first occurence with grep

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regex to match only first occurence with grep
# 1  
Old 08-28-2013
Regex to match only first occurence with grep

Hello to all,

How would be the correct regex to match only the first occurence of
the pattern 3.*6.

I'm trying with 3.*6 trying to match only 34rrte56, but with my current regex is matching 4rrte567890123456789123powiluur56. And if I try with ?
doesn't print anything

echo "1234rrte567890123456789123powiluur56789" | grep --color -e '3.*6'

Thanks in advance for any help.
# 2  
Old 08-28-2013
You can use a non-greedy expression to match only things between 3 and 6, but I can't see a way in grep to match only the first occurrence.

$ echo "1234rrte567890123456789123powiluur56789" | grep --color -m1 '3[^6]*6'

I probably wouldn't recommend this if the string is likely to contain formatting instructions:
$ printf "$(echo 1234rrte567890123456789123powiluur56789 | sed "s/3[^6]*6/\\\e[1;31m&\\\e[0m/")\n"

Last edited by Scott; 08-28-2013 at 02:11 AM..
# 3  
Old 08-28-2013
Hello Scott,

Thanks for your help. I want to do it in grep because I need to extract the matched strings using grep -o.

Actually, for the first example, I want to match only things that are between 3 and 6. That would be fine, not needed to match
only the first occurrence.

But for this new string, I want to match only the pattern 347 + something + 35 + 4 characters. And
After the patterns always follow 6789. But with the regex I'm trying is not printing anything.

echo "1234725493512216789012523475700354912350214678902002" | grep --color -e '347[^67]*35.{4}'

The match strings should be those 2 in red:

Maybe could be done with grep this.

Thanks in advance.

Last edited by Ophiuchus; 08-28-2013 at 02:58 AM..
# 4  
Old 08-28-2013
I tried

echo "12347254935122167890125234757003678912350214678902002" | grep -E '347[^67]*35.{4}'

and got the result as

# 5  
Old 08-28-2013
Hello krishmaths,

Thankss for the help.

It works for the first occurrence, but I realized that actually I need to match all
occurrences but separated as follow:


and not like this:

Do you think is possible with grep?

Thanks again.
# 6  
Old 08-28-2013
It is not directly possible with grep because it matches regular expressions line by line.

If you can break the line into multiple lines, say by inserting a newline after this pattern is found, then you can use grep to extract all the matches.
# 7  
Old 08-28-2013
I see that have the patterns in same line generate issues.

Do you know if it is possible to adapt the same regex to grep but directly from binary file?

Thanks again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sendmail K command regex: adding exclusion/negative lookahead to regex -a@MATCH

I'm trying to get some exclusions into our sendmail regular expression for the K command. The following configuration & regex works: LOCAL_CONFIG # Kcheckaddress regex -a@MATCH +<@+?\.++?\.(us|info|to|br|bid|cn|ru) LOCAL_RULESETS SLocal_check_mail # check address against various regex... (0 Replies)
Discussion started by: RobbieTheK
0 Replies

2. Shell Programming and Scripting

Match pattern and print the line number of occurence using awk

Hi, I have a simple problem but i guess stupid enough to figure it out. i have thousands rows of data. and i need to find match patterns of two columns and print the number of rows. for example: inputfile abd abp 123 abc abc 325 ndc ndc 451 mjk lkj... (3 Replies)
Discussion started by: redse171
3 Replies

3. Shell Programming and Scripting

HOW TO - Bash REGEX - Print Error when More then One Occurence is Found Consecutively?

Hello All, Bash Version: 4.1.10(1) I'm trying to "verify" some user input. The User input will contain the "Absolute Path" a "Command" and any "Options" of that Command. For Example, say the user's input is: user_input="/usr//local/myExample/check_process -p 'java' -w 10 -c 20" I... (6 Replies)
Discussion started by: mrm5102
6 Replies

4. Shell Programming and Scripting

Retrieve lines that match any occurence in a list of patterns

I have two files. The first containing a header and six columns of data. Example file 1: Number SNP ID dbSNP RS ID Chromosome Result_Call Physical Position 787066 SNP_A-8575395 RS6650104 1 NOCALL 564477 786872 SNP_A-8575125 RS10458597 1 AA ... (13 Replies)
Discussion started by: Selftaught
13 Replies

5. Shell Programming and Scripting

grep regex, match exact string which includes "/" anywhere on line.

I have a file that contains the 2 following lines (from /proc/mounts) /dev/sdc1 /mnt/backup2 xfs rw,relatime,attr2,noquota 0 0 /dev/sdb1 /mnt/backup xfs rw,relatime,attr2,noquota 0 0 I need to match the string in the second column exactly so that only one result is returned, e.g. > grep... (2 Replies)
Discussion started by: jelloir
2 Replies

6. UNIX for Dummies Questions & Answers

Regex to match when input is not a certain string (can't use grep -v)

Hey everyone, Basically, all I'm looking for is a way to regex for not a certain string. The regex I'm looking to avoid matching is: D222 i.e. an equivalent of: awk '!/D222/' The problem is that I use this in the following command in a Bash script: ls ${source_directory} | awk... (1 Reply)
Discussion started by: kdelok
1 Replies

7. Shell Programming and Scripting

Regex in grep to match all lines ending with a double quote (") OR a single quote (')

Hi, I've been trying to write a regex to use in egrep (in a shell script) that'll fetch the names of all the files that match a particular pattern. I expect to match the following line in a file: Name = "abc" The regex I'm using to match the same is: egrep -l '(^) *= *" ** *"$' /PATH_TO_SEARCH... (6 Replies)
Discussion started by: NanJ
6 Replies

8. UNIX for Dummies Questions & Answers

First occurence from grep

Hi , supoose i have a file in which a word is repeated so many times. I just want the firts occurence of that word through grep and it should not go to the next one means get the first occurence and stop there. Suggest me some solutions. Thanks Namish (10 Replies)
Discussion started by: namishtiwari
10 Replies

9. UNIX for Dummies Questions & Answers

grep the last occurence

is there a grep or awk one-line command to print the line of the last occurence of a string in a file? (3 Replies)
Discussion started by: apalex
3 Replies

10. UNIX for Dummies Questions & Answers

grep for certain occurence

I am using grep to pull out info from a file. The line I am searching for begins: START TIME - Tue Sep 11 16:40:00. There are mutiple lines of START TIME. I need the FIRST occurence ONLY. My grep is as follows: start="$( grep 'START TIME' filename | cut -c15-33)" If I echo or cat... (2 Replies)
Discussion started by: app4dxh
2 Replies
Login or Register to Ask a Question