Mombu the Computer Design Forum sponsored links

Go Back   Mombu the Computer Design Forum > Computer_Design > using grep to find one match per line
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 28th April 04:47
peter kahrel
External User
 
Posts: 1
Default using grep to find one match per line



GREP is case-sensitive, so "sin" doesn't find anything in "SG SIN Singapore". To search case-sensitively and whole words only, use (?i)\bsin\b

But your question isn't entirely clear. You start by asking "How do you get grep to return a single line even when multiple matches are found on the same line?", then you say "I don't want the search to stop after a single match, it should continue on successive lines". These two seem to contradich each other. Do you mean you want one match per line?

Peter
  Reply With Quote


  sponsored links


2 28th April 22:01
schedule_boy
External User
 
Posts: 1
Default using grep to find one match per line



Hi,

How do you get grep to return a single line even when multiple matches are found on the same line?

I tried the (?-m) and (?-s) without success.

eg. searching for the string "sin" in the following line should return only one result not 2.

SG SIN Singapore

I don't want the search to stop after a single match, it should continue on successive lines

Thanks,
SB
  Reply With Quote


  sponsored links


3 28th April 22:01
schedule_boy
External User
 
Posts: 1
Default using grep to find one match per line


Sorry for the unclear description.

I would like to get 1 match per line, and have grep return the whole line.

so a case insensitive search of

MY KUL Kuala Lumpar
SG SIN Singapore
ID JKT Jakarata

searching for "sin" would return the whole line "SG SIN Singapore" a single time.

Is that possible ?

Matthew
  Reply With Quote
4 28th April 22:01
External User
 
Posts: 1
Default using grep to find one match per line


Sure. Let GREP find an entire line at once:

^.*sin.*$


But it won't work, I'm afraid. You insist that looking for "sin" will find a match in "SG SIN Singapore"; and not one but two. It won't. Try it.

__
(..)
_\

Got that? So use the 'case insensitive' switch

(?i)^.*sin.*$


or (perhaps better, in this case), as you seem to search for the three letter codes, use

^.*\<SIN\>.*$
  Reply With Quote
5 28th April 22:02
schedule_boy
External User
 
Posts: 1
Default using grep to find one match per line


Thanks, that seems to work.

Is there a way tell grep to stop after the first match?
  Reply With Quote
6 28th April 22:02
External User
 
Posts: 1
Default using grep to find one match per line


Eh. N-

How do you want to 'stop' grep? In JS, calling

findGrep()


returns immediately, with the result of the search. That result is an array of found items, and the number of items indicate the number of occurrences found (0, 1, or more). There is nothing to stop.

You could use just the first found match of the results array in your script (I presume you need it for a certain 'use').
  Reply With Quote
7 2nd May 14:03
External User
 
Posts: 1
Default using grep to find one match per line


it seems needn't Grep.stop it likely simple,for(gerp) when mached display a form.
  Reply With Quote
Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666