Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Extracting data in a file

24 views
Skip to first unread message

contr...@gmail.com

unread,
Apr 12, 2013, 7:09:48 PM4/12/13
to
Hi,
I´m looking for any way to get all lines in a text file having date between 04/11/13 16:00 and 04/12/13 18:00.

This is the text file:

387241 ORA_ENAMEL_DFM_GHA_DZ Backup Done 0 04/12/13 18:15 04/12/13 18:15 000:00:36 send-pwd-alastar 16540 A 100
387240 ORA_ADMMANAGER_GFM_DA Backup Done 0 04/12/13 18:14 04/12/13 18:14 000:00:25 sent-pwm-alastar 927 A 100


Thanks.

Barry Margolin

unread,
Apr 12, 2013, 9:02:59 PM4/12/13
to
In article <dc3cf10f-7232-4ba3...@googlegroups.com>,
There are two sets of date/time columns in each row, which should be
used?

Anyway, the general approach I'd use is with awk. I'd use split to
rearrange the date and time into the form YYMMDDHHMM, then compare that
with the endpoints of the time period.

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

contracer

unread,
Apr 12, 2013, 9:28:35 PM4/12/13
to
On Apr 12, 10:02 pm, Barry Margolin <bar...@alum.mit.edu> wrote:
> In article <dc3cf10f-7232-4ba3...@googlegroups.com>,
I need get lines using first date´s data.
Is there any way to use a perl command to get these lines ?
Anything like below:

perl -e 'use POSIX;print strftime "%d/%m/%Y/%H:%M",localtime time;'

Thanks

Luuk

unread,
Apr 13, 2013, 12:13:36 PM4/13/13
to
Why perl, you where looking for 'any way'.... ;)

file.awk:
BEGIN {
from = mktime("2013/04/11 16:00:00");
to = mktime ("2013/04/12 18:00:00");
}
{
a = "20" substr($6,7,2) " " substr($6,1,2) " " substr($6,4,2);
a = a " " substr($7,1,2) " " substr($7,4,2) " 00";
a = mktime(a);
if ((a>= from) && (a<= to)) {
print $0;
}
}

$ awk -f file.awk file


contr...@gmail.com

unread,
Apr 13, 2013, 1:16:52 PM4/13/13
to
Thanks a lot !

Janis Papanagnou

unread,
Apr 13, 2013, 1:18:48 PM4/13/13
to
On 13.04.2013 01:09, contr...@gmail.com wrote:
> Hi,
> I�m looking for any way to get all lines in a text file having date between 04/11/13 16:00 and 04/12/13 18:00.

Your date representation is ambiguous. I suggest to fix your data.
If you feel that this is not possible, then please always specify
in your postings what those numbers mean; which one is supposed to
be the year, which one the month, and which one the day.

Generally this is a primitive task if the date components are sorted
and the date format regular...

awk '$6" "$7 >= "2013-11-04 16:00" && $6" "$7 <= "2013-12-04 18:00"' file

If you are insisting in using pathological date formats you have to
take the burden and extract the respective parts using substr(), and
concatenate those parts before comparison.

Janis

Janis Papanagnou

unread,
Apr 13, 2013, 1:22:21 PM4/13/13
to
I thought that mktime() would require blanks as separators inside the
date-spec and time-spec.

> }
> {
> a = "20" substr($6,7,2) " " substr($6,1,2) " " substr($6,4,2);
> a = a " " substr($7,1,2) " " substr($7,4,2) " 00";
> a = mktime(a);

Using mktime() is unnecessary if you compose your strings in a regular
way with year first, followed by month, and finally day.

> if ((a>= from) && (a<= to)) {
> print $0;
> }

You can formulate that as a simple awk condition (with default action);
there's no need for imperative code.

Janis

Luuk

unread,
Apr 13, 2013, 1:43:30 PM4/13/13
to
Yes, you are right, i think you even said that to me in another post a
couple of years back ;)

(But i'm not going to search for IF you did that, and WHEN you did that)

;)

0 new messages