Examples

Unix timestamp

Formats a Unix timestamp (1443214640) as a human-readable string.

py1 -i 'time/ctime' 'P(ctime(1443214640))'

To do so we import ctime from the time module and print it.

Count blank lines

Count the number of blank lines in a file.

py1 -b 'c=0' -l 'if not L: c += 1' -e 'P(c)'

Here we define an acumulator variable and increment it when the line satisfies a criteria.

Show lines matching a regexp

Show lines matching the regexp ‘$a+^’.

py1 -l 'if M("$a+^", L): P(L)'

Here we use the M matching function to match the regexp.

Count blank lines again

Count the number of blank lines in a file.

py1 -e 'P(sum(1 if l else 0 for l in F))'

Here we do not set a per-line statement and instead have sum iterate over F.

Group by

Given a file of ‘$name $value’, with name being repeated, sum the values for each name.

py1 -b 'd=defaultdict(int)' -l 'd[W[0]] += int(W[1])'
    -e 'for n, v in d: P(n, v)'