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
原文地址:https://www.cnblogs.com/liujiaxin2018/p/15779410.html