Egrep - Only Match First Occurrence


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Egrep - Only Match First Occurrence
# 1  
Old 04-30-2014
Egrep - Only Match First Occurrence

Code:
echo 'String#1 and String#2' | egrep -o -m 1 'String#.{1}'
String#1
String#2

I'm trying to just match the first occurrence of 'String#' + 1 character. I thought the "-m 1" switch would do that for me. Instead I get both occurrences. Can somebody provide some insight?

Thanks!
# 2  
Old 04-30-2014
Code:
echo 'String#1 and String#2' | awk '{for(i = 1; i <= NF; i++) {if($i ~ "^String#.$") {print $i; exit}}}'

This User Gave Thanks to SriniShoo For This Post:
# 3  
Old 04-30-2014
The {1} seems redundant, if you leave it off it will also assume just one.

Code:
$ echo 'String#1 and String#2' | awk 'match($0, /String#./) { print substr($0, RSTART, RLENGTH); }'

String#1

$

This User Gave Thanks to Corona688 For This Post:
# 4  
Old 04-30-2014
1.
Code:
man egrep

sais:
Quote:
-m NUM, --max-count=NUM
Stop reading a file after NUM matching lines.
so does not have an effect if your input is only 1 line.
Then
Quote:
-o, --only-matching
Show only the part of a matching line that matches PATTERN.
and the String#. matches twice, so both matches can be expected.
It looks like there is no way to have it stop at the first match within a line.
In this forum you can always expect an awk solution Smilie
Code:
echo 'String#1 and String#2' | awk 'BEGIN {RS=FS} $1~search {print $1; exit}' search="String#."

The exit means: exit after the first match.
BTW currently GNU awk and mawk and nawk do not know the {n} operator.
While {1} meaning "1 instance of the preceding character" can always be omitted, but {2} you need to write as ..
This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 04-30-2014
Just add a space at the end of the pattern:

Code:
echo 'String#1 and String#2' | egrep -o -m 1 'String#.{1} '
String#1

This User Gave Thanks to in2nix4life For This Post:
# 6  
Old 05-02-2014
Thanks for the reply's! Script complete! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed print from last occurrence match until the end of last occurrence match

Hi, i have file file.txt with data like: START 03:11:30 a 03:11:40 b END START 03:13:30 eee 03:13:35 fff END jjjjjjjjjjjjjjjjjjjjj START 03:14:30 eee 03:15:30 fff END ggggggggggg iiiiiiiiiiiiiiiiiiiiiiiii I want the below output START (13 Replies)
Discussion started by: Jyotshna
13 Replies

2. Shell Programming and Scripting

awk command to get file content until 2 occurrence of pattern match

AWK command to get file content until 3 occurrence of pattern match, INPUT FILE: JMS_BODY_FIELD:JMSText = <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <custOptIn xmlns="http://com/walm/ta/cu/ccs/xml2"> <person>Romi</person> <appName>SAP</appName> </custOptIn> ... (4 Replies)
Discussion started by: prince1987
4 Replies

3. UNIX for Dummies Questions & Answers

Code for exact match to count occurrence

Hi all, I have an input file as below. I would like to count the occurrence of pattern matching 8th field for each line. Input: field_01 field_02 field_03 field_04 field_05 field_06 field_07 field_08 TA T TA T TA TA TA... (3 Replies)
Discussion started by: huiyee1
3 Replies

4. UNIX for Dummies Questions & Answers

[Solved] Replace first occurrence after match

hey guys, i have been trying to work this thing out with sed with no luck :confused: i m looking for a way to replace only the first occurrence after a match for example : Cat Realized what you gotta do Dog Realized what you gotta do Sheep Realized what you gotta do Wolf Realized... (6 Replies)
Discussion started by: boaz733
6 Replies

5. Shell Programming and Scripting

Perl match multiple numbers from a variable similar to egrep

I want to match the number exactly from the variable which has multiple numbers seperated by pipe symbol similar to search in egrep.below is the code which i tried #!/usr/bin/perl my $searchnum = $ARGV; my $num = "148|1|0|256"; print $num; if ($searchnum =~ /$num/) { print "found"; }... (2 Replies)
Discussion started by: kar_333
2 Replies

6. Shell Programming and Scripting

sed print from last occurrence match until the end of file

Hi, i have file f1.txt with data like: CHECK a b CHECK c d CHECK e f JOB_START .... I want to match the last occurrence of 'CHECK' until the end of the file. I can use awk: awk '/^CHECK/ { buf = "" } { buf = buf "\n" $0 } END { print buf }' f1.txt | tail +2Is there a cleaner way of... (2 Replies)
Discussion started by: ysrini
2 Replies

7. Shell Programming and Scripting

SED to replace exact match, not first occurrence.

Lets say I have file.txt: (Product:Price:QuantityAvailable) (: as delimiter) Chocolate:5:5 Banana:33:3 I am doing a edit/update function. I want to change the Quantity Available, so I tried using the SED command to replace 5, but my Price which is also 5 is changed instead. (for the Banana... (13 Replies)
Discussion started by: andylbh
13 Replies

8. Shell Programming and Scripting

last occurrence of a match through multiple files

Hi all, I have a lot of files with extension ".o" and I would like to extract the 10th line after (last) occurrence of a given string in each of the files. I tried: $ grep "string_to_look_for" *.o -A 10 | tail -1 but it gives the occurrence in the last file with extension .o ... (1 Reply)
Discussion started by: f_o_555
1 Replies

9. Shell Programming and Scripting

egrep 4th line from match

Can some one help me to print 4th line before the match using egrep or grep command options. i have a very large file and i need to search the entire file, look for the match (key word) and print 4th line before the matched key word. (9 Replies)
Discussion started by: ramana117
9 Replies

10. Shell Programming and Scripting

Print last occurrence if first field match

Hi All, I have an input below. If the term in the 1st column is equal, print the last row which 1st column is equal.In the below example, it's " 0001 k= 27 " and " 0004 k= 6 " (depicted in bold). Those terms in 1st column which are not repetitive are to be printed as well. Can any body help me... (9 Replies)
Discussion started by: Raynon
9 Replies
Login or Register to Ask a Question