3 ms·
Most of the basic text processing ones are shorter in and Awk and when they're not shorter, they are still clearer: no cryptic command line options, no gratuito
by kazinator 10y ago
Most of the basic text processing ones are shorter in and Awk and when they're not shorter, they are still clearer: no cryptic command line options, no gratuitous line noise:
# Double space a file
perl -pe '$\="\n"'
perl -pe 'BEGIN { $\="\n" }'
perl -pe '$_ .= "\n"'
perl -pe 's/$/\n/'
perl -nE 'say'
awk 'BEGIN { ORS="\n\n" }' # output record separator
awk 'print; print "\n"'
awk '$0=$0"\n"'
txr -e '(awk ((set rec `@rec\n`)))' # Awk macro in Lisp!
# Double space a file, except the blank lines
perl -pe '$_ .= "\n" unless /^$/'
perl -pe '$_ .= "\n" if /\S/'
awk 'BEGIN { ORS="\n\n" } /./'
awk '/./ { print; print "\n" )'
awk '/./&&$0=$0"\n"'
txr -e '(awk (#/./ (set rec `@rec\n`) (prn)))'
# Remove all consecutive blank lines, leaving just one
perl -00 -pe ''
perl -00pe0
awk -v RS= -v ORS="\n\n" 1 # RS=<blank> -> Awk paragraph mode
txr -e '(awk (:set rs nil ors "\n\n") (t))'
# Number all lines in a file
perl -pe '$_ = "$. $_"'
awk '{ print FNR, $0 }'
txr -e '(awk (t (prn fnr rec)))'
# Print the total number of lines in a file (emulate wc -l)
perl -lne 'END { print $. }'
awk 'END { print FNR }'
# Find the total number of fields (words) on each line
perl -alne 'print scalar @F'
awk '{print NF}'
# elides loop if cond/action clauses absent so (nil) needed
txr -e '(awk (nil) (:end (prn fnr)))'
# Print the last 10 lines of a file (emulate tail -10)
perl -ne 'push @a, $_; @a = @a[@a-10..$#a]; END { print @a }'
awk '{ ln[FNR%10]=$0 }
END { for(i=FNR-9;i<=FNR;i++)
if (i > 0) print ln[i%10] }'
txr -e '(awk (:let l)
(t (push rec l)
(del [l 10]))
(:end (tprint (nreverse l))))'
txr -e '(awk (:let l)
(t (set [l 10..10] (list rec))
(del [l 0..-10]))
(:end (tprint l)))'
# (real way)
txr -t '[(get-lines) -10..:]' < in
txr -t '(last (get-lines) 10)' < in