Python One Liner
See:
One-liner is preferred to run from
python -c 'ONE_LINER'
FILE SPACING:
- double space a file
import sys; print "\n".join(line for line in sys.stdin)
- double space a file which already has blank lines in it. Output file should contain no more than one blank line between lines of text.
import sys; print "\n".join(line for line in sys.stdin if not len(line) == 0 )
- triple space a file
import sys; print "\n\n".join(line for line in sys.stdin)
NUMBERING AND CALCULATIONS:
- precede each line by its line number FOR THAT FILE (left alignment). Using a tab (\t) instead of space will preserve margins.
import sys; print "".join("%d\t%s"%(n + 1, line) for (n, line) in enumerate(sys.stdin))
- precede each line by its line number FOR ALL FILES TOGETHER, with tab.
import sys,glob; map(lambda file: sys.stdout.write("".join("%d\t%s"%(n + 1, line) for(n, line) in enumerate(open(file)))), glob.glob("*"));
- number each line of a file (number on left, right-aligned) Double the percent signs if typing from the DOS command prompt.
import sys; print "".join("%5d %s"%(n + 1, line) for (n, line) in enumerate(sys.stdin))
- number each line of file, but only print numbers if line is not blank. Remember caveats about Unix treatment of \r (mentioned above)
import sys; print "".join("%5d %s"%(n + 1, line) for (n, line) in enumerate(sys.stdin) if len(line)>0)
- count lines (emulates “wc -l”)
import sys; print sum(1 for line in sys.stdin)
To be continued…