linux中实现将宽列数据按照指定列数堆叠输出
001、方法1, cut + 循环
[root@pc1 test01]# ls a.txt [root@pc1 test01]# cat a.txt ## 测试数据 001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 [root@pc1 test01]# for i in $(seq 1 2 $(head -n 1 a.txt | awk '{print NF}')); do let j=$i+1; cut -d " " -f $i-$j a.txt >> result.txt; done [root@pc1 test01]# ls a.txt result.txt [root@pc1 test01]# cat result.txt 001 002 011 012 021 022 031 032 041 042 003 004 013 014 023 024 033 034 043 044 005 006 015 016 025 026 035 036 045 046 007 008 017 018 027 028 037 038 047 048 009 010 019 020 029 030 039 040 049 050
002、 xargs 实现
(base) [root@pc1 test2]# ls a.txt (base) [root@pc1 test2]# cat a.txt ## 测试数据 001 002 003 004 005 006 007 008 011 012 013 014 015 016 017 018 021 022 023 024 025 026 027 028 031 032 033 034 035 036 037 038 (base) [root@pc1 test2]# cat a.txt | xargs -n 2 ## 每两行转换为一列 001 002 003 004 005 006 007 008 011 012 013 014 015 016 017 018 021 022 023 024 025 026 027 028 031 032 033 034 035 036 037 038
003、 sed + awk实现
(base) [root@pc1 test2]# ls a.txt (base) [root@pc1 test2]# cat a.txt ## 测试数据 001 002 003 004 005 006 007 008 011 012 013 014 015 016 017 018 021 022 023 024 025 026 027 028 031 032 033 034 035 036 037 038 (base) [root@pc1 test2]# cat a.txt | sed 's/\t/\n/g' | awk '{if(NR % 2 == 0) {print $0} else {printf("%s\t", $0)}}' 001 002 003 004 005 006 007 008 011 012 013 014 015 016 017 018 021 022 023 024 025 026 027 028 031 032 033 034 035 036 037 038
004、awk
(base) [root@pc1 test2]# ls a.txt (base) [root@pc1 test2]# cat a.txt ## 测试数据 001 002 003 004 005 006 007 008 011 012 013 014 015 016 017 018 021 022 023 024 025 026 027 028 031 032 033 034 035 036 037 038 (base) [root@pc1 test2]# awk '{for(i = 1; i <= NF; i++) {if(i % 2 == 0 && i != NF){printf("%s_", $i)} else if(i % 2 != 0) {printf("%s ", $i)} else {print $i}}}' a.txt 001 002_003 004_005 006_007 008 011 012_013 014_015 016_017 018 021 022_023 024_025 026_027 028 031 032_033 034_035 036_037 038 (base) [root@pc1 test2]# awk '{for(i = 1; i <= NF; i++) {if(i % 2 == 0 && i != NF){printf("%s_", $i)} else if(i % 2 != 0) {printf("%s ", $i)} else {print $i}}}' a.txt | sed 's/_/\n/g' 001 002 003 004 005 006 007 008 011 012 013 014 015 016 017 018 021 022 023 024 025 026 027 028 031 032 033 034 035 036 037 038
005、 python实现
(base) [root@pc1 test2]# ls a.txt test.py (base) [root@pc1 test2]# cat a.txt ## 测试数据 001 002 003 004 005 006 007 008 011 012 013 014 015 016 017 018 021 022 023 024 025 026 027 028 031 032 033 034 035 036 037 038 (base) [root@pc1 test2]# cat test.py ## 转换程序 #!/usr/bin/env python3 # -*- coding: utf-8 -*- in_file = open("a.txt", "r") out_file = open("result.txt", "w") length = len(in_file.readlines()[0].strip().split("\t")) in_file.close() in_file = open("a.txt", "r") dict1 = dict() for i in range(0,length,2): dict1[i] = [] for i in in_file: i = i.strip().split("\t") for j in range(len(i)): for k in dict1: if k == j or k + 1 == j: dict1[k].append(i[j]) in_file.close() for i,j in dict1.items(): for k in range(len(j)): if k % 2 == 0: out_file.write(j[k] + " ") else: out_file.write(j[k] + "\n") out_file.close() (base) [root@pc1 test2]# python3 test.py ## 执行程序 (base) [root@pc1 test2]# ls a.txt result.txt test.py (base) [root@pc1 test2]# cat result.txt ## 运行结果 001 002 011 012 021 022 031 032 003 004 013 014 023 024 033 034 005 006 015 016 025 026 035 036 007 008 017 018 027 028 037 038 (base) [root@pc1 test2]#
。
分类:
linux shell
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2022-09-26 linux中sed命令同时传入两个参数
2022-09-26 linux 中删除目录中所有以数字开头的文件
2022-09-26 linux 中如何在文件的行首或者行尾添加一行
2020-09-26 sratoolkits软件的安装 cdb-config:command not found 解决方法
2020-09-26 R语言中order函数的用法,对数据框进行排序
2020-09-26 linux系统 comm命令
2020-09-26 《Linux就该这么学》学习笔记 04