top | item 35281089

Fascination with AWK

31 points| benhoyt | 3 years ago |maximullaris.com | reply

3 comments

order
[+] prosaic-hacker|3 years ago|reply
Perl and Python have syntax options that are similar to the style of awk. The style was described to me as filter thinking. These were not tested on real data.

The awk program translates to

#!/usr/bin/perl

while (<>) {

    push @freq, (split)[2..-1] if /Frequencies/;

    push @fc, (reverse(split))[3..-1] if /Frc consts/;

    push @ir, (reverse(split))[3..-1] if /IR Inten/;
}

print "@freq[$_ - 1] $fc[$_] $ir[$_]\n" for 1..@freq;

and in python

#!/usr/bin/env python3

import sys

freq = []

fc = []

ir = []

for line in sys.stdin:

    fields = line.split()

    if "Frequencies" in line:

        freq.extend(fields[2:])

    elif "Frc consts" in line:

        fc.extend(reversed(fields[3:]))

    elif "IR Inten" in line:

        ir.extend(reversed(fields[3:]))
for i in range(len(freq)):

    print(freq[i], fc[i], ir[i])
[+] elenaferrantes|3 years ago|reply
I always use it in pipelines to extract columns. I often use it for summations of numbers. Sometimes for more complex tasks. Get the job done.