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 @   小鲨鱼2018  阅读(98)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2022-04-26 linux 中调用shell脚本时指定工作目录为shell脚本所在的目录
2022-04-26 修改 WSL2 可用内存大小和交换分区大小
2022-04-26 linux 中 awk命令统计A、C、T、G碱基数目
2022-04-26 linux 中shell脚本统计参考基因组中碱基的总数目
2022-04-26 linux中如何将一行数据转换为一列数据
2022-04-26 linux中如何给数据添加行号
2021-04-26 linux系统中如何将一行数据转换为一列数据
点击右上角即可分享
微信分享提示