regex - get part of a string which contains given substring -


in bash have string , part (separator being white space characters) contains substring. have

list="some string substring want match" 

and substring "ubst". i'd string "substring" returned. (in case of multiple matches should return first substring matches.)

the parts of string compiler flags. can contain special characters (but no white space).

using awk can this:

str="some string substring want match" awk -v s='ubst' -v rs=' ' '$0 ~ s' <<< "$str" substring 
  • -v rs=' ' set record separator space breaking each space separated word individual record.
  • $0 ~ s return word when word matched given search term

ps: if want print first match use:

awk -v s='ubst' -v rs=' ' '$0 ~ s{print; exit}' <<< "$str" 

just academic exercise if 1 wants single grep use pcre regex:

grep -op '^(?:(?!\w*ubst).)*\k\w*ubst\w*' <<< "$str" 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -