在perl或awk中将每4列打印到一行 [英] print every 4 columns to one row in perl or awk
本文介绍了在perl或awk中将每4列打印到一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请帮助我如何将每4个连续的行转换为一个制表符分隔的列?
would you please help me how to convert every 4-sequantial rows into one tab-separated column?
转换:
A
1
2
3
3
3
4
1
至:
A 1 2 3
3 3 4 1
推荐答案
一个简单的方法是使用xargs
:
A simple way to do this is to use xargs
:
$ xargs -n4 < file
A 1 2 3
3 3 4 1
使用awk
您将执行以下操作:
With awk
you would do:
$ awk '{printf "%s%s",$0,(NR%4?FS:RS)}' file
A 1 2 3
3 3 4 1
另一种灵活的方法是使用pr
:
Another flexible approach is to use pr
:
$ pr -tas' ' --columns 4 file
A 1 2 3
3 3 4 1
awk
和pr
解决方案都可以轻松修改,以将输出分隔符更改为TAB
:
Both the awk
and pr
solution can be easily modified to change the output separator to a TAB
:
$ pr -at --columns 4 file
A 1 2 3
3 3 4 1
$ awk '{printf "%s%s",$0,(NR%4?OFS:RS)}' OFS='\t' file
A 1 2 3
3 3 4 1
这篇关于在perl或awk中将每4列打印到一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文