An efficient way to transpose a file in Bash

前端 未结 29 2462
时光说笑
时光说笑 2020-11-22 03:30

I have a huge tab-separated file formatted like this

X column1 column2 column3
row1 0 1 2
row2 3 4 5
row3 6 7 8
row4 9 10 11

I would like t

29条回答
  •  离开以前
    2020-11-22 04:04

    Here's a Haskell solution. When compiled with -O2, it runs slightly faster than ghostdog's awk and slightly slower than Stephan's thinly wrapped c python on my machine for repeated "Hello world" input lines. Unfortunately GHC's support for passing command line code is non-existent as far as I can tell, so you will have to write it to a file yourself. It will truncate the rows to the length of the shortest row.

    transpose :: [[a]] -> [[a]]
    transpose = foldr (zipWith (:)) (repeat [])
    
    main :: IO ()
    main = interact $ unlines . map unwords . transpose . map words . lines
    

提交回复
热议问题