User Tools

Site Tools


python_cli

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
python_cli [2021/02/05 23:32] – created rajupython_cli [2021/02/28 06:27] (current) – [sort lines by length] raju
Line 4: Line 4:
 python -c "import sys; print(','.join(line.strip() for line in sys.stdin))" python -c "import sys; print(','.join(line.strip() for line in sys.stdin))"
 </code> </code>
 +
 +==== sort lines by length ====
 +<code>
 +import fileinput
 +print("".join(sorted(fileinput.input(), key=len)))
 +</code>
 +
 +On the command line:
 +<code>
 +% cat random_snippets.txt | python -c 'import fileinput; print("".join(sorted(fileinput.input(), key=len)))'
 +</code>
 +
 +Ref:- https://stackoverflow.com/questions/34401683/how-to-use-awk-to-sort-lines-by-length/
 +
 +See also:
 +  * https://www.google.com/books/edition/The_Python_3_Standard_Library_by_Example/KtcnDwAAQBAJ?hl=en&gbpv=0 - The Python 3 Standard Library by Example - Doug Hellmann - 2017 . This book gives many sample scripts that make use of fileinput; The scripts are very practical in nature and explained well.
 +
 +==== sql string in clause ====
 +<code>
 +cat junk3.txt | \
 +python -c "import sys; print('\'', '\', \''.join(line.strip() for line in sys.stdin), '\'', sep='')"
 +</code>
 +
python_cli.1612567952.txt.gz · Last modified: 2021/02/05 23:32 by raju