dada
dada

Reputation: 1493

Regular expression given to grep

I would like to extract from a huge file looking like this :

.....
G:  Quantity 000001, removing 4 binary files
A: some stuff
.....
G: some other stuff
G: some infos
.....
G:  Quantity 000002, removing 1 binary files
....
A: some data
....
G:  Quantity 000003, removing 41 binary files
.....

all the lines "G: Quantity ??????, removing * binary files".

I'm writing this pattern with the bash syntax as it is the one I am mostly familiar with, but grep doesn't interpret my question marks and the asterisk as bash does. What is the corresponding grep syntax ?

The following syntax works :

grep "G:" filename | grep Quantity | grep removing

but it doesn't use any regex.

Upvotes: 0

Views: 224

Answers (1)

fedorqui
fedorqui

Reputation: 289825

In regex, the way to match any character is to use ..

If you want to match exactly N times any character, say .{N}. If you want to match at least one, say .+. Being this case where you want to match digits, it may be best to say [0-9] instead of a too generic .:

grep -E 'G:  Quantity [0-9]{6}, removing [0-9]+ binary files' file

This command returns:

G:  Quantity 000001, removing 4 binary files
G:  Quantity 000002, removing 1 binary files
G:  Quantity 000003, removing 41 binary files

Upvotes: 2

Related Questions