Gentoo Archives: gentoo-user

From: Andrew Udvare <audvare@×××××.com>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] OT: Extracting year from data, but honour empty lines
Date: Fri, 11 May 2018 23:24:31
Message-Id: 8374B67E-A10D-40AD-A858-BE662F32F74E@gmail.com
In Reply to: [gentoo-user] OT: Extracting year from data, but honour empty lines by Daniel Frey
1 > On May 11, 2018, at 7:16 PM, Daniel Frey <djqfrey@×××××.com> wrote:
2 >
3 > Hi all,
4 >
5 > I am trying to do something relatively simple and I've had something
6 > working in the past, but my brain just doesn't want to work today.
7 >
8 > I have a text file with the following (this is just a subset of about
9 > 2500 dates, and I don't want to edit these all by hand if I can avoid it):
10 >
11 > --- START ---
12 > December 2, 1994
13 > March 27, 1992
14 > June 4, 1994
15 > 1993
16 > January 11, 1992
17 > January 3, 1995
18 >
19 >
20 > March 12, 1993
21 > July 12, 1991
22 > May 17, 1991
23 > August 7, 1992
24 > December 23, 1994
25 > March 27, 1992
26 > March 1995
27 > --- END —
28
29 While loop in Bash? This is slower but it will do it:
30
31 while IFS=$’\n’ read -r line; do
32 if [ -z “$line” ]; then echo; fi
33 grep -o '\([0-9]\{4\}\)’ <<< “$line”
34 done < input_file
35
36 I would consider using a human date string parsing library in another language, such as Python’s datetime where you can specify the formats, loop to check for any and if nothing matches output a blank line.
37
38 Andrew