使用awk反复将n个行数转换为列 [英] Convert n number of rows to columns repeatedly using awk
问题描述
我的数据是一个大型文本文件,由12行重复组成.看起来像这样:
My data is a large text file that consists of 12 rows repeating. It looks something like this:
{
1
2
3
4
5
6
7
8
9
10
}
一遍又一遍.我想将每12行变成一列.因此数据看起来像这样:
repeating over and over. I want to turn every 12 rows into columns. so the data would look like this:
{ 1 2 3 4 5 6 7 8 9 10 }
{ 1 2 3 4 5 6 7 8 9 10 }
{ 1 2 3 4 5 6 7 8 9 10 }
我发现了一些使用awk将所有行转换为列的示例:awk '{printf("%s ", $0)}'
,但是没有找到如何将每12行转换为列然后重复该过程的示例.
I have found some examples of how to convert all the rows to columns using awk: awk '{printf("%s ", $0)}'
, but no examples of how to convert every 12 rows into columns and then repeat the process.
推荐答案
您可以使用以下内容:
awk '{printf "%s%s", $0, (NR%12?OFS:RS)}' file
NR%12
的计算结果为true,除非记录号可以被0整除.为true时,将使用输出字段分隔符(默认为空格).如果为false,则使用记录分隔符(默认情况下为换行符).
NR%12
evaluates to true except when the record number is exactly divisible by 0. When it is true, the output field separator is used (which defaults to a space). When it is false, the record separator is used (by default, a newline).
对其进行测试:
$ awk '{printf "%s%s", $0, (NR%12?OFS:RS)}' file
{ 1 2 3 4 5 6 7 8 9 10 }
这篇关于使用awk反复将n个行数转换为列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!