A
A
Alexander Sklyarov2013-09-30 10:09:52
linux
Alexander Sklyarov, 2013-09-30 10:09:52

Regular for egrep

Good afternoon. Help me write a regular expression for a bash script.

We have at the entrance to egrep

From: =?utf-8?B?aGVscAo=?= <[email protected]>
Subject: =?utf-8?B?0J/RgNC40LLQtdGCINGF0LDQsdGALCDQvdGD0LbQvdCwINC/0L7QvNC+0YjRjCD=
=?utf-8? Qv9C + INGA0LDQsdC Bed and 0YLQtSDRgSBlZ3JlcC4g0JrQsNC6INC30LDRgdGC0LDQst +
=? = utf-8 C40YLRjCBlZ3JlcCwg0LfQsNGF0LLQsNGC0YvQstCw0YLRjCDQstGB0LUg0YHRg = Bed and??? the
to: = utf-8 B aGVscAo = = <[email protected]>? ???

outlet to get: the

Subject:? = UTF- 8? Bed and? 0J / RgNC40LLQtdGCINGF0LDQsdGALCDQvdGD0LbQvdCwINC / 0L7QvNC 0YjRjCD + =
=? utf-8? Bed and? Qv9C + INGA0LDQsdC 0YLQtSDRgSBlZ3JlcC4g0JrQsNC6INC30LDRgdGC0LDQst + =
=? utf-8? B? C40YLRjCBlZ3JlcCwg0LfQsNGF0LLQsNGC0YvQstCw0YLRjCDQstGB0LUg0YHRg =

egrep -io '^Subject:(\s*=\?(utf-8|windows-1251|koi-8r)\?(B|Q)\?[A-Za-z0-9=\+]+\ ?=)+' only returns the first line.
How to be?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
R
rakeev, 2013-09-30
@Voron095

Grep actually deals with line-by-line comparison. Install pcregrep.
Well, or if you absolutely need to get by with grep, then you can use the -P key.

D
dpirat, 2013-09-30
@dpirat

(Subject:)(.)*(\W)*(.)*(\W)*(.)*
Tested here: rubular.com . (http://rubular.com/r/wlhqJwbex6)
Hope it helps.

A
Alexey Akulovich, 2013-09-30
@AterCattus

And you can use awk for headers in any number of lines:

awk -F ':' 'BEGIN{key=""} {if ($2) {key=$1}; h[key][length(h[key])+1]=$0}END{for (i in h["Subject"]) print h["Subject"][i]}' /path/to/file

We build a dictionary of the form "From" => [strings], "Subject" => [strings], ..., and then display the lines by the desired key.

V
Victor Taran, 2013-11-27
@shambler81

here everything just
divide them into blocks
1 block everything that starts with
block 2 everything that starts with To:
(1block)(2block) return only $1
As a result, your regular expression will be reduced to a couple of characters
(^Subject:.*)(To:) return $2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question