Gentoo Archives: gentoo-user

From: R0b0t1 <r030t1@×××××.com>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] OT: Extracting year from data, but honour empty lines
Date: Sat, 12 May 2018 02:12:37
Message-Id: CAAD4mYiC0CUwPCC4RD9rpQw5K9b-4O-zMjfjAJy-8J3W=7Cbfw@mail.gmail.com
In Reply to: [gentoo-user] OT: Extracting year from data, but honour empty lines by Daniel Frey
1 On Fri, May 11, 2018 at 6:16 PM, Daniel Frey <djqfrey@×××××.com> wrote:
2 > Hi all,
3 >
4 > I am trying to do something relatively simple and I've had something
5 > working in the past, but my brain just doesn't want to work today.
6 >
7 > I have a text file with the following (this is just a subset of about
8 > 2500 dates, and I don't want to edit these all by hand if I can avoid it):
9 >
10 > --- START ---
11 > December 2, 1994
12 > March 27, 1992
13 > June 4, 1994
14 > 1993
15 > January 11, 1992
16 > January 3, 1995
17 >
18 >
19 > March 12, 1993
20 > July 12, 1991
21 > May 17, 1991
22 > August 7, 1992
23 > December 23, 1994
24 > March 27, 1992
25 > March 1995
26 > --- END ---
27 >
28 > As you can see, there's no standard in the way the date is formatted.
29 > Some of them are also formatted YYYY-MM-DD and MM-DD-YYYY.
30 >
31 > I have a basic grep that I tossed together:
32 >
33 > grep -o '\([0-9]\{4\}\)'
34 >
35 > This does extract the year but yields the following:
36 >
37 > 1994
38 > 1992
39 > 1994
40 > 1993
41 > 1992
42 > 1995
43 > 1993
44 > 1991
45 > 1991
46 > 1992
47 > 1994
48 > 1992
49 > 1995
50 >
51 > As you can see, the two empty lines are removed but this will cause
52 > problems with data not lining up later on.
53 >
54 > Does anyone have a quick tip for my tired brain to make this work and
55 > just output a blank line if there's no match? I swear I did this months
56 > ago and had something working but I apparently didn't bother saving the
57 > script I made. Argh!
58 >
59 > Dan
60 >
61
62 Use awk or perl and when the line matches the pattern ^\s*$ print a
63 blank line. Otherwise, apply the normal pattern.
64
65 Cheers,
66 R0b0t1