Hacker News new | past | comments | ask | show | jobs | submit login
Fascination with AWK (maximullaris.com)
31 points by benhoyt on March 23, 2023 | hide | past | favorite | 3 comments



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])


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.


Awk and ChatGPT were made for each other.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: