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

 

posted @ 2021-07-21 19:58  小鲨鱼2018  阅读(1960)  评论(0编辑  收藏  举报