我有一个文本文件。内容:
game1
game2
game3
game4
/usr/local/games
/usr/local/games
/usr/local/games
/usr/local/games
10
20
30
40
我想要的输出如下:
game1 /usr/local/games 10
game2 /usr/local/games 20
game3 /usr/local/games 30
game4 /usr/local/games 40
最佳答案
awk '
BEGIN { numRows = 4 }
{
rowNr = (NR-1)%numRows+1
colNr = ++colCnt[rowNr]
numCols = (colCnt[rowNr] > numCols ? colCnt[rowNr] : numCols)
val[rowNr,colNr] = $0
}
END {
for (rowNr=1; rowNr<=numRows; rowNr++)
for (colNr=1; colNr<=numCols; colNr++)
printf "%s%s", val[rowNr,colNr], (colNr < numCols ? OFS : ORS)
}
' file
game1 /usr/local/games 10
game2 /usr/local/games 20
game3 /usr/local/games 30
game4 /usr/local/games 40