Unix命令到一列文件转换成按行数指定多列文件 [英] Unix command to convert one column file into multiple column file specified by number of rows

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

问题描述

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

我不希望推倒重来。我想确认是否有unix命令/或编写自定义脚本之前这样做的标准方式。

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

  $猫input.txt中
汤姆
插口

巴特

酒吧

我希望把它变成3排文件

  $猫input.txt中| my_script --every = 3 =对齐和美化--delimiter标签
汤姆·巴特
插孔富
金棒

或不同delimter 2排文件:

  $猫input.txt中| my_script --every = 2 =对齐和美化--delimiter,
汤姆·金,富
千斤顶,巴特,酒吧


解决方案

通过 AWK

 的awk -v行= 2'{A [(NR-1)%排] = A [(NR-1)%排] $ 0;接下来} END {为(我在一个)打印[I]}'文件

输出:

 汤姆巴特
插孔富
金棒

在这里指定原材料变量没有你想要的行。例如:行= 3 为三行

尝试这样的,如果你想打破只在特定的列行

猫文件| xargs的-N2

下面2的每一行包含2栏,你可以用你想要的。

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

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

I want to turn this into 3 row file

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

or 2 row file with the different delimter:

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

解决方案

With awk

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

output:

tom  bart 
jack foo 
kim  bar 

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

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

这篇关于Unix命令到一列文件转换成按行数指定多列文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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