The script awk-apply-to-record

awk

awk-apply-to-record.awk

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
{
    # print NR
    if (r == "" || NR == r) {
        if (NR == 1) {
            printf "%s", $0 |& cmd;
        } else {
            if (irs == "y") {
                printf "%s", RS
                printf "%s", $0 |& cmd;
            } else {
                printf "%s%s", RS, $0 |& cmd;
            }
        }

        close(cmd, "to");

        # This is needed because getline will not overwrite if nothing is read
        $0 = "";

        cmd |& getline $0;

        fflush(cmd);
        close(cmd);

    } else {
        if (NR > 1) {
            print RS
        }
    }

    print; system("");
}

awk-apply-to-record

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/bin/bash
export TTY

SELECTED_RECORD_ID=
while [ $# -gt 0 ]; do opt="$1"; case "$opt" in
    -R) {
        SELECTED_RECORD_ID="$2"
        shift
        shift
    }
    ;;

    # TODO
    -v) {
        INVERT=y
        shift
    }
    ;;

    -k|-irs) {
        # Do not put RS into the filter
        IGNORE_RS=y
        shift
    }
    ;;

    -r|-rrs) {
        # Remove the RS at the end
        REMOVE_RS=y
        shift
    }
    ;;

    *) break;
esac; done

rsep="$1"
rsepe="$(p "$rsep" | esc \\)"
filter_cmd="$2"


sn="$(basename "$0")"
test "$sn" = "awk-apply-to-records" && SELECTED_RECORD_ID=
test "$sn" = "atrs" && SELECTED_RECORD_ID=

gawk -v irs="$IGNORE_RS" -v r="$SELECTED_RECORD_ID" -v RS="$rsep" -v ORS="" -v cmd="$filter_cmd | sed -z 's/$rsepe/__RS_PLACEHOLDER__/'" -v FS=" " -v OFS=" " -f awk-apply-to-record | sed -z "s/__RS_PLACEHOLDER__/$sep/g" | {
    if test "$REMOVE_RS" = "y"; then
        sed -z "s/$rsep//g"
    else
        cat
    fi
}

Example

The first argument of awk-apply-to-record is the record delimiter.

The second argument specifies

1
nmap -p "22" "192.168.1.1/24" | awk-apply-to-record "\n\n" "tr '\n' _ | eipct $(aqf "grep -q open") | tr _ '\n'" | rosie-ips

The script filterre.pl

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env perl

# # Cannot use โœ“ in the regex because it is used in the placeholder

use strict;
use strict;
use IPC::Open3;
use Getopt::Std;

# declare the perl command line flags/options we want to allow
my %options=();
getopts("r:", \%options);

my $r = '\w+';

if (defined $options{r}) {
    $r = "$options{r}";
}

my $first = shift;

$| = 1; # make the current file handle (stdout) hot. This has the same effect as disabling buffering

my @cmd = ($first);

sub filter {
    # The regex match
    my $input = shift;

    my $pid = open3 my $stdin, my $stdout, '>&STDERR', @cmd;

    print $stdin "$input";
    close $stdin;

    my $alloutput;
    while (my $line = readline $stdout) {
        $alloutput .= $line;
    }

    waitpid $pid, 0;
    my $exit = $? >> 8;

    return $alloutput;
}

foreach my $line ( <STDIN> ) {
    chomp( $line );

    # Modify match and leave a placeholder to prevent changing the same thing
    # Repeat until no change
    while ($line =~ s/(?<!โœ“)($r)/"โœ“".filter($1)/ge) {
    }

    # Remove placeholders
    $line =~ s/โœ“//g;
    print "$line\n";
}

Example

Capitalise every word.

1
ps -ef | head -n 10 | patm -r "\w+" "tr '[:lower:]' '[:upper:]'"
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
UID        PID  PPID  C STIME TTY          TIME CMD
ROOT         1     0  0 14:50 ?        00:00:02 /SBIN/INIT SPLASH
ROOT         2     0  0 14:50 ?        00:00:00 [KTHREADD]
ROOT         4     2  0 14:50 ?        00:00:00 [KWORKER/0:0H]
ROOT         6     2  0 14:50 ?        00:00:00 [MM_PERCPU_WQ]
ROOT         7     2  0 14:50 ?        00:00:02 [KSOFTIRQD/0]
ROOT         8     2  0 14:50 ?        00:00:04 [RCU_SCHED]
ROOT         9     2  0 14:50 ?        00:00:00 [RCU_BH]
ROOT        10     2  0 14:50 ?        00:00:00 [MIGRATION/0]
ROOT        11     2  0 14:50 ?        00:00:00 [WATCHDOG/0]