wades
wades

Reputation: 947

How to supress default print in awk?

This is with gawk 4.0.0, running on Windows 7 with cygwin. The program is invoked like

gawk -f procjournal.gawk testdata

I have some data that looks like this:

"Date";"Type";"Amount";"Balance"
"6/11/2013 11:51:17 AM";"Transaction Tax";-427.5;399313884.46
"6/11/2013 11:51:17 AM";"Market Transaction";47500;399314311.96
"6/11/2013 11:12:42 AM";"Transaction Tax";-549.92;399266811.96
"6/11/2013 11:12:42 AM";"Market Transaction";61101.78;399267361.88

I want to extract the lines for transactions, strip the date part from the 1st field, and reformat the time stamp as a decimal. I thought I could do it with this awk program:

FS=";"
OFS=";"
/Market Transaction/ {
    split($1, itemdate, " ");
    tmp = itemdate[2];
    split(tmp, hms, ":");
    timestamp = hms[3] + (hms[2] * 60) + (hms[1] * 3600);
    if (itemdate[3] == "AM")
       timestamp += 12 * 3600;
    timestamp /= 3600.0;
    $1 = timestamp;
    print;
}

but my output looks like this:

"Date";"Type";"Amount";"Balance"
"Date";"Type";"Amount";"Balance"
"6/11/2013 11:51:17 AM";"Transaction Tax";-427.5;399313884.46
"6/11/2013 11:51:17 AM";"Transaction Tax";-427.5;399313884.46
"6/11/2013 11:51:17 AM";"Market Transaction";47500;399314311.96
"6/11/2013 11:51:17 AM";"Market Transaction";47500;399314311.96
11.8547;"Market Transaction";47500;399314311.96
"6/11/2013 11:12:42 AM";"Transaction Tax";-549.92;399266811.96
"6/11/2013 11:12:42 AM";"Transaction Tax";-549.92;399266811.96
"6/11/2013 11:12:42 AM";"Market Transaction";61101.78;399267361.88
"6/11/2013 11:12:42 AM";"Market Transaction";61101.78;399267361.88
11.2117;"Market Transaction";61101.78;399267361.88

Why are non-matching lines being printed, and how do I suppress that?

Upvotes: 5

Views: 2221

Answers (2)

glenn jackman
glenn jackman

Reputation: 247062

Not an answer to your question, but a different way to calculate the time:

if (match($1, /([0-9]?[0-9]):([0-9][0-9]):([0-9][0-9]) ([AP]M)/, a)) {
    $1 = a[1] + (a[2]*60 + a[3])/3600 + (a[4] == "PM" ? 12 : 0)
}

Upvotes: 0

jaypal singh
jaypal singh

Reputation: 77155

Change the script to include the separators in BEGIN block.

Script Content:

BEGIN {
FS=";"
OFS=";"
}
/Market Transaction/ {
    split($1, itemdate, " ");
    tmp = itemdate[2];
    split(tmp, hms, ":");
    timestamp = hms[3] + (hms[2] * 60) + (hms[1] * 3600);
    if (itemdate[3] == "AM")
       timestamp += 12 * 3600;
    timestamp /= 3600.0;
    $1 = timestamp;
    print;
}

Upvotes: 6

Related Questions