zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    【SQL】在含有GROUP BY的SELECT语句中如何显示COUNT()为0的结果
    【SQL】SQL分页查询总结
    开篇
    Android Native Crash 排查思路
    jmeter+ant+jenkins接口自动化测试框架
    为何推荐使用线程池而不是显式创建线程原因之一—至少让线程有范围和限制
    quartz 中的线程池
    select in 查询结果无顺序及解决办法
    Druid 数据库连接池如何根据url加载Driver
    java 线程池参数
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/15041164.html
Copyright © 2011-2022 走看看