Grimoire-
Command
.es

GNU+Linux command memo

Pype : pipe data to Python

Process a piped data flow with Python.

1. Installation

1.1. Shell alias definition

Tested on ZSH :

alias pype='python3 -c "import re,sys,time; [print(eval(sys.argv[1]), end=\"\") for line in sys.stdin]"'

2. Examples

2.1. Lowerize

$ echo "ET VOILÀ" | pype "line.lower()"
et voilà

2.2. HTMLize

alias htmlize='pype "line.encode(\"ascii\", \"xmlcharrefreplace\").decode(\"UTF-8\")"'
$ echo "Et voilà" | htmlize
Et voil\à

2.3. Humanize timestamp

alias humanize_timestamp='pype "time.strftime(\"%Y-%m-%d %H:%M:%S\", time.localtime(int(line)))"'
$ humanize_timestamp <<< '1516817552'
2018-01-24 19:12:32

2.4. Simple mass renaming based on regexp matching

$ ls *0
sc_IMG_2572.JPG?attredirects=0  sc_IMG_2580.JPG?attredirects=0  sc_IMG_2587.JPG?attredirects=0
$ ls *0 | pype 're.sub("^((.*)\?att.*)$", "mv \g<0> \g<2>", line)'
mv sc_IMG_2572.JPG?attredirects=0 sc_IMG_2572.JPG
mv sc_IMG_2573.JPG?attredirects=0 sc_IMG_2573.JPG
mv sc_IMG_2575.JPG?attredirects=0 sc_IMG_2575.JPG

Note that you still need to copy/paste the built mv commands to actually execute them.

2.5. Mass rename with field re-ordering

$ ls 1._Hazem-TAL55-1.pdf 1-Lavallee-TAL52-2-2011.pdf 1-Marsic-TAL53-2.pdf 1.Maynard-TAL55-2.pdf
$ ls | pype 're.sub("^[^T].*(?:TAL)?.{,2}(\d(\d))-(\d).*\.pdf$",
	"mv \g<0> TAL-201\g<2>-\g<1>-\g<3>-\g<0>")'
mv 1._Hazem-TAL55-1.pdf TAL-2015-55-1-1._Hazem-TAL55-1.pdf
mv 1-Lavallee-TAL52-2-2011.pdf TAL-2012-52-2-1-Lavallee-TAL52-2-2011.pdf
mv 1-Marsic-TAL53-2.pdf TAL-2013-53-2-1-Marsic-TAL53-2.pdf
mv 1.Maynard-TAL55-2.pdf TAL-2015-55-2-1.Maynard-TAL55-2.pdf

To be continued