Penguin
Note: You are viewing an old revision of this page. View the current version.

Many oneliners rely on the magic of Perl's -i switch, which means when files supplied on the commandline are opened they are edited in place, and if an extension was passed, a backup copy with that extension will be made. -e specifies the Perl code to run. See perlrun(1) for any other switches used here - in particular, -n and -p make powerful allies for -i.

The unsurpassed power of Perl's RegularExpression flavour contributes a great deal to the usefulness of nearly every oneliner, so you will also want to read the perlretut(1) and perlre(1) manpages to learn about it.

Removing empty lines from a file

perl -ni.bak -e'/\S/ && print' file1 file2

In Shell
for FILE in file1 file2 ; do mv "$F"{,.bak} ; sed '/^ *$/d' < "$F.bak" > "$F" ; done

Collapse consecutive blank lines to a single one

perl -00 -pi.bak -e1 file1 file2

Note the use of 1 as a no-op piece of Perl code. In this case, the -00 and -p switches already do all the work, so only a dummy needs to be supplied.

Binary dump of a string

perl -e 'printf "%08b\n", $_ for unpack "C*", shift' 'My String'

Replace literal "\n" and "\t" in a file with newlines and tabs

cat $file | perl -ne 's/\\n/\012/g; s/\\t/\011/g; print'

(This has a useless use of "cat", and uses perl -n (for no print) and then does an explicit print.) This could be rewritten as
perl -pe 's@\\n@\012@g; s@\\t@\011@g' < $file

(you can use any punctuation as the separator in an s/// command, and if you have \ chars in your regexp then doing this can increase clarity.)


AddToMe