regex - How to match a pattern with unrelated charaters/blank-space between two fixed strings? -


i'm trying match string in files begins imports: [ , not contain sharedmodule. can have number or spaces, line-breaks, or other characters (words) in between 2 strings. i've been trying find with:

grep 'imports: \[[.*\s*]*sharedmodule' */*.module.ts 

but cannot find files have 'sharedmodule' in it. thought process .* find words , \s find blank space characters, , character class * selector allow show in order.

  1. can use character classes skip variable number of unrelated lines/characters?
  2. how negate statement returns lines without 'sharedmodule'?
  3. the goal append 'sharedmodule' import array wherever not exist.

thanks! (i'm new , 1 thing i've learned far is: regular expressions hard)

sample match:

imports: [   ionicpagemodule.forchild(formpage),   dynamicformcomponentmodule,   sharedmodule ], 

should not match but

imports: [   ionicpagemodule.forchild(leadershippage), ], 

should.

grep doesn't process multiline strings default. available gnu grep -z option regex bit more complex.

you may better off using gnu awk solution custom rs (record separator):

awk -v rs='imports:[[:blank:]]*\\[[^]]*\\],[[:space:]]+' 'rt !~ /sharedmodule/{ors=rt} 1' file  imports: [   ionicpagemodule.forchild(leadershippage), ], 

where file content this:

cat file imports: [   ionicpagemodule.forchild(formpage),   dynamicformcomponentmodule,   sharedmodule ],  imports: [   ionicpagemodule.forchild(leadershippage), ], 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -