Blog Archive

Tuesday, September 27, 2016

Monday, September 26, 2016

unix - Transpose a file in bash - Stack Overflow

http://stackoverflow.com/questions/1729824/transpose-a-file-in-bash



python -c "import sys; print('\n'.join(' '.join(c) for c in zip(*(l.split() for l in sys.stdin.readlines() if l.strip()))))" < input > output





A Python solution:
python -c "import sys; print('\n'.join(' '.join(c) for c in zip(*(l.split() for l in sys.stdin.readlines() if l.strip()))))" < input > output
The above is based on the following:
import sys

for c in zip(*(l.split() for l in sys.stdin.readlines() if l.strip())):
    print(' '.join(c))
This code does assume that every line has the same number of columns (no padding is performed).
'via Blog this'

Perl one-liners - bl.ocks.org

Perl one-liners - bl.ocks.org: "Perl one-liners"



Insert line numbers in a file:

perl -i -ne 'printf "%04d %s", $., $_'

'via Blog this'