~ersen/dotfiles

3c19d4d8c2fcac5fa84135b3c53177097043b205 — Oğuz Ersen a month ago 1e1305c
bin: convert some awk to perl
4 files changed, 32 insertions(+), 62 deletions(-)

D .local/bin/anagrams.awk
A .local/bin/anagrams.pl
D .local/bin/mocking.awk
A .local/bin/mocking.pl
D .local/bin/anagrams.awk => .local/bin/anagrams.awk +0 -45
@@ 1,45 0,0 @@
#!/usr/bin/gawk -f

BEGINFILE {
    delete words
}

{
    gsub(/[^[:alnum:][:blank:]]/, " ")

    $0 = tolower($0)

    if (NF == 0) {
        next
    }

    for (i = 1; i <= NF; ++i) {
        count = split($i, letters, "")
        key = ""

        asort(letters)

        for (j = 1; j <= count; ++j) {
            key = key letters[j]
        }

        words[key][$i] = $i
    }
}

ENDFILE {
    for (key in words) {
        count = asort(words[key])
        if (count == 1) {
            continue
        }

        printf "%s:", FILENAME

        for (i = 1; i <= count; ++i) {
            printf " %s", words[key][i]
        }

        printf "\n"
    }
}

A .local/bin/anagrams.pl => .local/bin/anagrams.pl +24 -0
@@ 0,0 1,24 @@
#!/usr/bin/perl

use strict;
use warnings;

my ( $key, @words, %anagrams );

while (<>) {
    @words = split /[\s_]+/, lc =~ s/[^\w]+/ /gr;

    next unless @words;

    for my $word (@words) {
        $key = join "", ( sort split //, $word );

        $anagrams{$key}{$word}++;
    }
}

for my $key ( keys %anagrams ) {
    @words = sort keys %{ $anagrams{$key} };

    print "@words\n" if @words > 1;
}

D .local/bin/mocking.awk => .local/bin/mocking.awk +0 -17
@@ 1,17 0,0 @@
#!/usr/bin/awk -f

BEGIN {
    srand()
}

{
    x = rand() < 0.5
    count = split($0, letters, "")
    out = ""

    for (i = 1; i <= count; ++i) {
        out = out ((++x % 2) ? tolower(letters[i]) : toupper(letters[i]))
    }

    print out
}

A .local/bin/mocking.pl => .local/bin/mocking.pl +8 -0
@@ 0,0 1,8 @@
#!/usr/bin/perl

use strict;
use warnings;

while (<>) {
    print join "", map { rand() < 0.5 ? lc : uc } split //;
}