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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。