linux中将指定行数据合并为一行数据

1、测试数据

复制代码
root@DESKTOP-1N42TVH:/home/test# ls
a.txt
root@DESKTOP-1N42TVH:/home/test# cat a.txt
01 11
02 12
03 13
04 14
05 15
06 16
07 17
08 18
09 19
10 20
复制代码

 

2、将每两行数据合并为一行数据

a、sed实现

复制代码
root@DESKTOP-1N42TVH:/home/test# ls
a.txt
root@DESKTOP-1N42TVH:/home/test# cat a.txt
01 11
02 12
03 13
04 14
05 15
06 16
07 17
08 18
09 19
10 20
root@DESKTOP-1N42TVH:/home/test# sed 'N; s/\n/ /' a.txt   ## 将每两行中间的换行符替换为空格
01 11 02 12
03 13 04 14
05 15 06 16
07 17 08 18
09 19 10 20
复制代码

 

b、awk实现

复制代码
root@DESKTOP-1N42TVH:/home/test# ls
a.txt
root@DESKTOP-1N42TVH:/home/test# cat a.txt
01 11
02 12
03 13
04 14
05 15
06 16
07 17
08 18
09 19
10 20
root@DESKTOP-1N42TVH:/home/test# awk '{if(NR % 2 == 0) {print $0} else {printf("%s ", $0)}}' a.txt
01 11 02 12
03 13 04 14
05 15 06 16
07 17 08 18
09 19 10 20
复制代码

 

c、paste实现

复制代码
root@DESKTOP-1N42TVH:/home/test# ls
a.txt
root@DESKTOP-1N42TVH:/home/test# cat a.txt
01 02
03 04
05 06
07 08
09 10
11 12
13 14
15 16
17 18
19 20
root@DESKTOP-1N42TVH:/home/test# cat a.txt | paste - -
01 02   03 04
05 06   07 08
09 10   11 12
13 14   15 16
17 18   19 20
root@DESKTOP-1N42TVH:/home/test# cat a.txt | paste - - -d " "
01 02 03 04
05 06 07 08
09 10 11 12
13 14 15 16
17 18 19 20
复制代码

 

 

3、将每三行数据合并为一行数据

a、awk实现

复制代码
root@DESKTOP-1N42TVH:/home/test# ls
a.txt
root@DESKTOP-1N42TVH:/home/test# cat a.txt
01 11
02 12
03 13
04 14
05 15
06 16
07 17
08 18
09 19
10 20
root@DESKTOP-1N42TVH:/home/test# awk '{if(NR % 3 == 0) {print $0} else {printf("%s ", $0)}}' a.txt  ## 不完整行最后缺少空格
01 11 02 12 03 13
04 14 05 15 06 16
07 17 08 18 09 19
10 20 root@DESKTOP-1N42TVH:/home/test# awk '{if(NR % 3 == 0) {print $0} else {printf("%s ", $0)}}' a.txt | sed '$ s/$/\n/'  ## sed添加空格
01 11 02 12 03 13
04 14 05 15 06 16
07 17 08 18 09 19
10 20
root@DESKTOP-1N42TVH:/home/test# awk '{if(NR % 3 == 0) {print $0} else {printf("%s ", $0)}}' a.txt | sed '$ s/$/\n/'
01 11 02 12 03 13
04 14 05 15 06 16
07 17 08 18 09 19
10 20
复制代码

 

b、paste实现

复制代码
root@DESKTOP-1N42TVH:/home/test# ls
a.txt
root@DESKTOP-1N42TVH:/home/test# cat a.txt
01 02
03 04
05 06
07 08
09 10
11 12
13 14
15 16
17 18
19 20
root@DESKTOP-1N42TVH:/home/test# cat a.txt | paste - - -    ## 每三行合并为一行
01 02   03 04   05 06
07 08   09 10   11 12
13 14   15 16   17 18
19 20
root@DESKTOP-1N42TVH:/home/test# cat a.txt | paste - - - -d " "   ## 指定输出分割符
01 02 03 04 05 06
07 08 09 10 11 12
13 14 15 16 17 18
19 20
复制代码

 

 

 

4、每四行数据合并为一行数据

a、awk实现

复制代码
root@DESKTOP-1N42TVH:/home/test# ls
a.txt
root@DESKTOP-1N42TVH:/home/test# cat a.txt
01 11
02 12
03 13
04 14
05 15
06 16
07 17
08 18
09 19
10 20
root@DESKTOP-1N42TVH:/home/test# awk '{if(NR % 4 == 0) {print $0} else {printf("%s ", $0)}}' a.txt
01 11 02 12 03 13 04 14
05 15 06 16 07 17 08 18
09 19 10 20 root@DESKTOP-1N42TVH:/home/test# awk '{if(NR % 4 == 0) {print $0} else {printf("%s ", $0)}}' a.txt | sed '$ s/$/\n/'
01 11 02 12 03 13 04 14
05 15 06 16 07 17 08 18
09 19 10 20
root@DESKTOP-1N42TVH:/home/test# awk '{if(NR % 4 == 0) {print $0} else {printf("%s ", $0)}}' a.txt | sed '$ s/$/\n/' 
01 11 02 12 03 13 04 14
05 15 06 16 07 17 08 18
09 19 10 20
复制代码

 

b、paste 实现

复制代码
root@DESKTOP-1N42TVH:/home/test# ls
a.txt
root@DESKTOP-1N42TVH:/home/test# cat a.txt
01 02
03 04
05 06
07 08
09 10
11 12
13 14
15 16
17 18
19 20
root@DESKTOP-1N42TVH:/home/test# cat a.txt | paste - - - -
01 02   03 04   05 06   07 08
09 10   11 12   13 14   15 16
17 18   19 20
root@DESKTOP-1N42TVH:/home/test# cat a.txt | paste - - - - -d " "   ## 将四行合并为一行
01 02 03 04 05 06 07 08
09 10 11 12 13 14 15 16
17 18 19 20
复制代码

 

posted @   小鲨鱼2018  阅读(1919)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2021-01-08 c语言中对数组元素进行倒序排列
2021-01-08 c语言中利用键盘输入数组元素的值
2021-01-08 c语言中数组的复制
2021-01-08 C语言中生成数组
2021-01-08 c语言中循环语句汇总
2021-01-08 c语言中continue语句
2021-01-08 c语言绘制倒立金字塔
点击右上角即可分享
微信分享提示