如何将具有单列的文本文件转换为矩阵? [英] How do I turn a text file with a single column into a matrix?

查看:52
本文介绍了如何将具有单列的文本文件转换为矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个只有一列数字的文本文件,如下所示:

I have a text file that has a single column of numbers, like this:

1
2
3
4
5
6

我想按从左到右的顺序将其转换为两列:

I want to convert it into two columns, in the left to right order this way:

1 2
3 4
5 6

我可以做到:

awk '{print>"line-"NR%2}' file
paste line-0  line-1 >newfile

但是我认为对两个中间文件的依赖会使它在脚本中变得脆弱.

But I think the reliance on two intermediate files will make it fragile in a script.

我想使用 cat文件|mystery-zip-command> newfile

推荐答案

您可以使用 粘贴 以执行此操作:

You can use paste to do this:

paste -d " " - - < file > newfile


您还可以使用 pr :

pr -ats" " -2 file > newfile

  • -a -使用循环顺序
  • -t -取消标题和结尾
  • -s" -使用单个空格作为分隔符
  • -2 -两列输出
    • -a - use round robin order
    • -t - suppress header and trailer
    • -s " " - use single space as the delimiter
    • -2 - two column output
    • 另请参阅:

      这篇关于如何将具有单列的文本文件转换为矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆