Blog Archive

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'

No comments:

Post a Comment