将单列文件转换成多列 [英] Convert single column file into multiple columns

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

问题描述

基本上,我想将一个列文件转换为按行数指定的多列文件.

Basically, I want to convert one column file into multiple column file specified by number of rows.

我不想重新发明轮子.我想确保在编写自定义脚本之前是否有unix命令或执行此操作的标准方法.

I do not want to reinvent the wheel. I want to make sure if there is a unix command / or standard way of doing this before writing a custom script.

例如,假设我有以下文件:

For example, let's say I have the following file:

$cat input.txt
tom
jack
kim
bart
foo
bar

我想把它变成3行文件

$ cat input.txt | my_script --every=3 --delimiter=tab
tom bart
jack foo
kim bar

或带有不同除斜线符的2行文件:

or 2 row file with the different delimter:

$ cat input.txt | my_script --every=2 --delimiter=,
tom,kim,foo
jack,bart,bar

推荐答案

使用awk

 awk -v row=2 '{A[(NR-1)%row]=A[(NR-1)%row]$0" ";next}END{for(i in A)print A[i]}' file

输出:

tom  bart 
jack foo 
kim  bar 

此处在raw变量中指定所需的行数.例如:row=3代表三行.

here specify no of row you want in raw variable. eg: row=3 for three rows.

如果您只想中断特定行中的列,请尝试这样

Try like this for if you want only break column in specific rows

cat file | xargs -n2

这里每2行包含2列,您可以使用所需的内容.

Here 2 for each row contain 2 column, You can use what you want.

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

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