Regular expressions

Other topics

Regular expressions

The search pattern can also be a regular expression. Running:

grep '^[A-Z]' someFile.txt

When someFile.txt contains:

fred 14 m foo
sam 68 m bar
christina 83 f baz
bob 22 m qux
Sam 41 m quux

Will produce the output:

Sam 41 m quux

since this is the only line in someFile.txt starting with an upper case letter.

Look behind

Given the following file:

hello how are you
i am fine
let's go, you!
let's go, baby!

grep with look-behind allows to print only some parts:

$ grep -Po "(?<=let's go, ).*" file
you!
baby!

In this case, it matches what occurs after "let's go, ".

Contributors

Topic Id: 4183

Example Ids: 6989,14635

This site is not affiliated with any of the contributors.