regex - how to select lines containing several words using sed? -


i learning using sed in unix. have file many lines , wanna delete lines except lines containing strings(e.g) alex, eva , tom. think can use

sed '/alex|eva|tom/!d' filename 

however find doesn't work, cannot match line. match "alex|eva|tom"... only

sed '/alex/!d' filename 

works.

anyone know how select lines containing more 1 words using sed?

plus, parenthesis "sed '/(alex)|(eva)|(tom)/!d' file" doesn't work, , wanna line containing 3 words.

delete lines except lines containing strings(e.g) alex, eva , tom

as worded you're asking preserve lines containing words samples preserve lines containing any. in case "all" wasn't misspeak: regular expressions can't express any-order searches, fortunately sed lets run multiple matches:

sed -n '/alex/{/eva/{/tom/p}}' 

or delete them serially:

sed '/alex/!d; /eva/!d; /tom/!d' 

the above works on gnu/anything systems, bsd-based userlands you'll have insert bunch of newlines or pass them separate expressions:

sed -n '/alex/ {           /eva/ {             /tom/ p         }      }' 

or

sed -e '/alex/!d' -e '/eva/!d' -e '/tom/!d' 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -