linux 中 批量提取指定的列

 

001、

(py38) root@DESKTOP-IDT9S0E:/home/test# ls
a.txt  index.txt  record.sh
(py38) root@DESKTOP-IDT9S0E:/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
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
(py38) root@DESKTOP-IDT9S0E:/home/test# cat index.txt    ## 列号
2
4
7

 

(py38) root@DESKTOP-IDT9S0E:/home/test# ls
a.txt  index.txt  record.sh
(py38) root@DESKTOP-IDT9S0E:/home/test# cat record.sh   ## 程序
#!/bin/bash

rm -f temp.txt result.txt

for i in $(cat index.txt)
do
        cut -d " " -f $i a.txt | paste -s -d " " >> temp.txt
done

head -n 1 temp.txt | awk '{print NF}' | xargs seq | while read i
do
        cut -d " " -f $i temp.txt | paste -s -d " " >> result.txt
done
rm -f temp.txt

 

(py38) root@DESKTOP-IDT9S0E:/home/test# ls
a.txt  index.txt  record.sh
(py38) root@DESKTOP-IDT9S0E:/home/test# bash record.sh    ## 执行程序
(py38) root@DESKTOP-IDT9S0E:/home/test# ls
a.txt  index.txt  record.sh  result.txt
(py38) root@DESKTOP-IDT9S0E:/home/test# cat result.txt    ## 提取结果
02 04 07
12 14 17
22 24 27
32 34 37
42 44 47
52 54 57

 

posted @ 2023-04-26 15:05  小鲨鱼2018  阅读(82)  评论(0编辑  收藏  举报