linux系统中批量提取指定列的数据
linux系统中批量提取指定列的数据。
1、测试数据
[root@centos79 test]# cat a.txt 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 03 0f 0t 0s 0g 0y 0a 0d 0e 0n 07 03 0a 0g 0w 0z 0n 0k 04 0s 03 0d 0i 0e 0w 0g 0w 0u 08 0s 0d 0e 0a 0v 0m 0p
2、提取 3,5,7,8,9,15,17列
[root@centos79 test]# ls a.txt cols result [root@centos79 test]# cat a.txt 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 03 0f 0t 0s 0g 0y 0a 0d 0e 0n 07 03 0a 0g 0w 0z 0n 0k 04 0s 03 0d 0i 0e 0w 0g 0w 0u 08 0s 0d 0e 0a 0v 0m 0p [root@centos79 test]# cat cols 3 5 7 8 9 15 17 [root@centos79 test]# cat result [root@centos79 test]# for i in `cat cols`; do paste result <(cut -d " " -f $i a.txt ) > tmp && mv tmp result; done [root@centos79 test]# cat result 03 05 07 08 09 15 17 0t 0g 0a 0d 0e 0w 0n 03 0i 0w 0g 0w 0a 0m
3、awk提取
[root@centos79 test]# ls a.txt cols result [root@centos79 test]# cat a.txt 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 03 0f 0t 0s 0g 0y 0a 0d 0e 0n 07 03 0a 0g 0w 0z 0n 0k 04 0s 03 0d 0i 0e 0w 0g 0w 0u 08 0s 0d 0e 0a 0v 0m 0p [root@centos79 test]# cat cols 3 5 7 8 9 15 17 [root@centos79 test]# cat result [root@centos79 test]# for i in $(cat cols ); do paste result <(awk -v a=$i '{print $a}' a.txt ) > tmp && mv tmp result; done [root@centos79 test]# cat result 03 05 07 08 09 15 17 0t 0g 0a 0d 0e 0w 0n 03 0i 0w 0g 0w 0a 0m
4、while语句
[root@centos79 test]# ls a.txt cols result [root@centos79 test]# cat a.txt 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 03 0f 0t 0s 0g 0y 0a 0d 0e 0n 07 03 0a 0g 0w 0z 0n 0k 04 0s 03 0d 0i 0e 0w 0g 0w 0u 08 0s 0d 0e 0a 0v 0m 0p [root@centos79 test]# cat cols 3 5 7 8 9 15 17 [root@centos79 test]# cat result [root@centos79 test]# cat cols | while read i; do paste result <(cut -d " " -f $i a.txt ) > tmp && mv tmp result ; done [root@centos79 test]# cat result 03 05 07 08 09 15 17 0t 0g 0a 0d 0e 0w 0n 03 0i 0w 0g 0w 0a 0m
5、awk实现
root@DESKTOP-1N42TVH:/home/test# ls idx.txt test.txt root@DESKTOP-1N42TVH:/home/test# cat test.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 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 root@DESKTOP-1N42TVH:/home/test# cat idx.txt 2 6 7 17 root@DESKTOP-1N42TVH:/home/test# for i in `cat idx.txt`; do cut -d " " -f $i test.txt | paste -s -d " " >> temp; done ## 提取并转置 root@DESKTOP-1N42TVH:/home/test# ls idx.txt temp test.txt root@DESKTOP-1N42TVH:/home/test# cat temp 002 022 042 062 082 006 026 046 066 086 007 027 047 067 087 017 037 057 077 097 root@DESKTOP-1N42TVH:/home/test# for i in `head -n 1 temp | awk '{print NF}' | xargs seq`; do cut -d " " -f $i temp | paste -s -d " " >> result; done root@DESKTOP-1N42TVH:/home/test# ls idx.txt result temp test.txt root@DESKTOP-1N42TVH:/home/test# cat result ## 结果文件 002 006 007 017 022 026 027 037 042 046 047 057 062 066 067 077 082 086 087 097
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律