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

range extraction then based on a condtion

32 views
Skip to first unread message

peter...@techemail.com

unread,
Jun 5, 2013, 9:19:10 AM6/5/13
to
Hello,

I am trying to extract a date range from a file and from that output display records when col2 is equal to PED.
I can do this using 2 awk statements, but would like to combined it in one statement.
How can I do thi?

thanks
PS

here is a a shortened file:
2013-02-10,PED
2013-02-11,GPS
2013-02-12,GPS
2013-02-13,PED
2013-02-14,GPS
2013-02-15,GPS


first awk:
awk -F"," '/^2013-02-12/,/^2013-02-14/ {print}' testdate.txt >temp
2013-02-12,GPS
2013-02-13,PED
2013-02-14,GPS



second awk:
awk -F"," '$2=="PED" END {print }' temp
2013-02-13,PED

Aaron Sawyer

unread,
Jun 5, 2013, 12:16:04 PM6/5/13
to
In article <cdb57d70-deb0-4860...@googlegroups.com>,
peter...@techemail.com says...
>
> Hello,
>
> I am trying to extract a date range from a file and from that output
> display records when col2 is equal to PED. I can do this using 2 awk
> statements, but would like to combined it in one statement. How can I
> do this?
>
> thanks
> PS
>
> here is a a shortened file:
> 2013-02-10,PED
> 2013-02-11,GPS
> 2013-02-12,GPS
> 2013-02-13,PED
> 2013-02-14,GPS
> 2013-02-15,GPS
>
>
> first awk:
> awk -F"," '/^2013-02-12/,/^2013-02-14/ {print}' testdate.txt >temp
> 2013-02-12,GPS
> 2013-02-13,PED
> 2013-02-14,GPS
>
>
>
> second awk:
> awk -F"," '$2=="PED" END {print }' temp
> 2013-02-13,PED


awk/gawk currently rejects my attempts to combine patterns, so I did the
field 2 filter in the action:

$ cat <<\EOF | awk -F"," '
/^2013-02-12/,/^2013-02-14/ { if ( $2 == "PED" ) print; }
'
2013-02-10,PED
2013-02-11,GPS
2013-02-12,GPS
2013-02-13,PED
2013-02-14,GPS
2013-02-15,GPS
EOF

2013-02-13,PED
$

Regards,
=Aaron

Kenny McCormack

unread,
Jun 5, 2013, 12:33:16 PM6/5/13
to
In article <MPG.2c193c60d...@nntp.aioe.org>,
Aaron Sawyer <Aaron....@Deltaware.com> wrote:
...
>
>$ cat <<\EOF | awk -F"," '
>/^2013-02-12/,/^2013-02-14/ { if ( $2 == "PED" ) print; }
>'

Of course, you will all of:

1) UUOC
2) UUOQ
3) UUOS

But other than that...

P.S. The manual does say that the range selection functionality cannot be
combined with any other pattern syntax - which is a real pain, because one
usually wants to do exactly that. It renders the range thingie virtually
useless in serious code (without serious contortions to emulate that which
one cannot do directly).

Also, I suspect that the range selection could be replaced with:

/^2013-02-1[234]/

in this specific instance.

And then, of course, with:

/^2013-02-1[234]/ && $2 == "PED"

--
Given Bush and his insanely expensive wars (*), that we will be paying for
for generations to come, the only possible response a sensible person need
ever give, when a GOPer/TeaBagger says anything about "deficits", is a
polite snicker.

(*) Obvious money transfers between the taxpayers and Bush's moneyed
interests. Someday, we'll actually figure out a way to have a war where the
money just gets moved around and nobody (on either side) gets injured or
killed. That will be an accomplishment of which we will be justly proud.

peter...@techemail.com

unread,
Jun 6, 2013, 2:19:50 AM6/6/13
to
Thanks, a better solution than my original one
PS
0 new messages