zoukankan      html  css  js  c++  java
  • linux系统如何提取奇数列

    1、遇到一个数据需要提取奇数列  

    创建测试数据

    [root@linuxprobe test]# seq -f %03g 60 | xargs -n 30 > test.txt
    [root@linuxprobe test]# seq -f c%02g 30 | xargs |cat - test.txt > a && mv a test.txt
    [root@linuxprobe test]# cat test.txt
    c01 c02 c03 c04 c05 c06 c07 c08 c09 c10 c11 c12 c13 c14 c15 c16 c17 c18 c19 c20 c21 c22 c23 c24 c25 c26 c27 c28 c29 c30
    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

     

    2、测试程序 ,生成过度文件tmp.txt (奇数列的转置文件)

    [root@linuxprobe test]# ls
    test.txt
    [root@linuxprobe test]# awk '{print NF}' test.txt | head -n 2  ##查看列数  
    30
    30
    [root@linuxprobe test]# for i in `seq 1 2 30`;do awk -v a=$i '{print $a }' test.txt|tr "\n" " "|sed 's/$/\n/' >> tmp.txt; done ###生成临时文件tmp.txt
    [root@linuxprobe test]# ls
    test.txt  tmp.txt
    [root@linuxprobe test]# cat tmp.txt  ##奇数列文件的转置文件
    c01 001 031
    c03 003 033
    c05 005 035
    c07 007 037
    c09 009 039
    c11 011 041
    c13 013 043
    c15 015 045
    c17 017 047
    c19 019 049
    c21 021 051
    c23 023 053
    c25 025 055
    c27 027 057
    c29 029 059

    3、对临时文件转置生成结果文件

    [root@linuxprobe test]# awk '{print NF}' tmp.txt | head -n 2 ##查看列数 
    3
    3
    [root@linuxprobe test]# for i in `seq 3`;do cut -d " " -f $i tmp.txt | tr "\n" " " | sed 's/$/\n/'  >> result.txt;done ##对临时文件转置,生成结果文件
    [root@linuxprobe test]# cat result.txt
    c01 c03 c05 c07 c09 c11 c13 c15 c17 c19 c21 c23 c25 c27 c29
    001 003 005 007 009 011 013 015 017 019 021 023 025 027 029
    031 033 035 037 039 041 043 045 047 049 051 053 055 057 059

     已经提取奇数列。 

    2、

    [root@centos7 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
    61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    [root@centos7 test]# awk '{for (i = 1; i <= NF; i+=2) printf("%s ", $i); printf("\n")}' a.txt
    01 03 05 07 09 11 13 15 17 19
    21 23 25 27 29 31 33 35 37 39
    41 43 45 47 49 51 53 55 57 59
    61 63 65 67 69 71 73 75 77 79
    [root@centos7 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
    61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    [root@centos7 test]# awk '{for (i = 1; i <= NF; i++) if(i % 2 != 0) printf("%s ", $i); printf("\n")}' a.txt
    01 03 05 07 09 11 13 15 17 19
    21 23 25 27 29 31 33 35 37 39
    41 43 45 47 49 51 53 55 57 59
    61 63 65 67 69 71 73 75 77 79
  • 相关阅读:
    [LeetCode] 75. Sort Colors 颜色排序
    [LeetCode] 76. Minimum Window Substring 最小窗口子串
    OpenCV 2.4.10 Linux Qt Conifguration
    OpenCV2.4.10 Mac Qt Configuration
    [LeetCode] Combinations 组合项
    [LeetCode] 79. Word Search 词语搜索
    OpenCV count the number of connected camera 检测连接的摄像头的数量
    OpenCV 2.4.11 VS2010 Configuration
    Qt 程序和窗口添加图标
    [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/13759446.html
Copyright © 2011-2022 走看看