NAME 10/01 09/02 09/07 09/08 09/09 09/10 09/11 09/12 09/13 09/14 09/15 09/16 09/17 09/18 09/19 10/01
CNMPMT_DATA 99.22 99.22 99.22 99.22 99.22 99.22 99.22 99.22 99.22 99.22 99.22 99.22 99.22 99.22 99.22 99.22
DATA01 9.79 10.75 13.67 7.97 15.22 15.1 14.63 15.71 15.56 15.37 16.69 17.33 17.06 16.5 18.01 99.22
DATATBLS4PEAS 9.21 7.45 51.77 8.26 54.56 54.43 50.68 42.2 38.23 53.47 51.02 49.53 48.01 46.58 46.34 99.22
EDD_DATA 57.79 57.71 58.46 57.58 57.93 59.09 58.88 58.88 58.83 58.79 58.77 58.73 58.7 59.69 59.34 99.22
EDD_INDX 23.34 23.44 22.96 23.27 23.07 22.94 22.79 22.94 23.06 23.03 23.13 23.25 23.08 22.81 23.05 99.22
INDX01 8.59 8.74 8.68 8.72 8.94 8.71 8.46 8.78 8.87 8.82 9.19 9.41 9.21 8.86 9.38 99.22
SYSAUX 96.65 96.67 96.62 96.92 96.55 96.57 96.56 96.59 96.57 96.63 96.65 96.7 96.72 96.73 96.65 99.22
SYSTEM 93.37 93.37 93.36 93.38 93.36 93.36 93.36 93.36 93.36 93.36 93.36 93.36 93.36 93.36 93.36 99.22
TEMP 97.43 98.50 98.54 88.77 99.54 95.68 99.54 99.52 99.25 99.35 96.44 98.87 98.53 98.54 98.13 99.22
TEMP_EDDDATA 96.58 96.58 96.58 96.58 96.58 96.58 96.58 96.55 96.55 96.55 96.55 96.54 96.37 96.37 96.38 99.22
UNDOTBS1 22.35 14.31 34.18 30.17 5.37 8.94 17.08 17.43 14.7 22.96 11.99 7.83 .02 8.07 3.67 99.22
UNDOTBS2 42.45 47.22 53.84 43.96 25.77 36.5 40.05 28.89 27.75 26.86 23.4 22.86 .02 .03 6.56 99.22
UNDOTBS3 68.65 72.3 39.89 28.95 44.52 69.05 71.89 52.23 48.23 41.67 31.52 30.09 16.98 23.44 18.54 99.22
USERS 22.09 22.09 22.09 22.09 22.09 22.09 22.09 22.09 22.09 22.09 22.09 22.09 22.09 22.09 22.09 99.22
此文件我只想要9月份的列。文件是制表符分隔的。 尝试低于其工作但它改变了列位置
awk 'NR==1{for(i=1; i<=NF; i++) if ($i ~"09") {a[i]++;} } { for (i in a) printf "%s\t", $i; printf "\n"}' Allen_Free.txt
09/07 09/08 09/09 09/10 09/11 09/12 09/13 09/14 09/15 09/16 09/17 09/18 09/19 09/02
99.22 99.22 99.22 99.22 99.22 99.22 99.22 99.22 99.22 99.22 99.22 99.22 99.22 99.22
13.67 7.97 15.22 15.1 14.63 15.71 15.56 15.37 16.69 17.33 17.06 16.5 18.01 10.75
51.77 8.26 54.56 54.43 50.68 42.2 38.23 53.47 51.02 49.53 48.01 46.58 46.34 7.45
58.46 57.58 57.93 59.09 58.88 58.88 58.83 58.79 58.77 58.73 58.7 59.69 59.34 57.71
22.96 23.27 23.07 22.94 22.79 22.94 23.06 23.03 23.13 23.25 23.08 22.81 23.05 23.44
8.68 8.72 8.94 8.71 8.46 8.78 8.87 8.82 9.19 9.41 9.21 8.86 9.38 8.74
96.62 96.92 96.55 96.57 96.56 96.59 96.57 96.63 96.65 96.7 96.72 96.73 96.65 96.67
93.36 93.38 93.36 93.36 93.36 93.36 93.36 93.36 93.36 93.36 93.36 93.36 93.36 93.37
98.54 88.77 99.54 95.68 99.54 99.52 99.25 99.35 96.44 98.87 98.53 98.54 98.13 98.50
96.58 96.58 96.58 96.58 96.58 96.55 96.55 96.55 96.55 96.54 96.37 96.37 96.38 96.58
34.18 30.17 5.37 8.94 17.08 17.43 14.7 22.96 11.99 7.83 .02 8.07 3.67 14.31
53.84 43.96 25.77 36.5 40.05 28.89 27.75 26.86 23.4 22.86 .02 .03 6.56 47.22
39.89 28.95 44.52 69.05 71.89 52.23 48.23 41.67 31.52 30.09 16.98 23.44 18.54 72.3
22.09 22.09 22.09 22.09 22.09 22.09 22.09 22.09 22.09 22.09 22.09 22.09 22.09 22.09
<09>应该是09/02,然后09/07不确定是什么问题。
请帮忙。
答案 0 :(得分:1)
如果perl
解决方案没问题,
$ perl -MList::MoreUtils=indexes -nale '@i = indexes { /^09/ } @F if $. == 1; print join "\t", @F[@i]' ip.txt
09/02 09/07 09/08 09/09 09/10 09/11 09/12 09/13 09/14 09/15 09/16 09/17 09/18 09/19
99.22 99.22 99.22 99.22 99.22 99.22 99.22 99.22 99.22 99.22 99.22 99.22 99.22 99.22
10.75 13.67 7.97 15.22 15.1 14.63 15.71 15.56 15.37 16.69 17.33 17.06 16.5 18.01
7.45 51.77 8.26 54.56 54.43 50.68 42.2 38.23 53.47 51.02 49.53 48.01 46.58 46.34
57.71 58.46 57.58 57.93 59.09 58.88 58.88 58.83 58.79 58.77 58.73 58.7 59.69 59.34
23.44 22.96 23.27 23.07 22.94 22.79 22.94 23.06 23.03 23.13 23.25 23.08 22.81 23.05
8.74 8.68 8.72 8.94 8.71 8.46 8.78 8.87 8.82 9.19 9.41 9.21 8.86 9.38
96.67 96.62 96.92 96.55 96.57 96.56 96.59 96.57 96.63 96.65 96.7 96.72 96.73 96.65
93.37 93.36 93.38 93.36 93.36 93.36 93.36 93.36 93.36 93.36 93.36 93.36 93.36 93.36
98.50 98.54 88.77 99.54 95.68 99.54 99.52 99.25 99.35 96.44 98.87 98.53 98.54 98.13
96.58 96.58 96.58 96.58 96.58 96.58 96.55 96.55 96.55 96.55 96.54 96.37 96.37 96.38
14.31 34.18 30.17 5.37 8.94 17.08 17.43 14.7 22.96 11.99 7.83 .02 8.07 3.67
47.22 53.84 43.96 25.77 36.5 40.05 28.89 27.75 26.86 23.4 22.86 .02 .03 6.56
72.3 39.89 28.95 44.52 69.05 71.89 52.23 48.23 41.67 31.52 30.09 16.98 23.44 18.54
22.09 22.09 22.09 22.09 22.09 22.09 22.09 22.09 22.09 22.09 22.09 22.09 22.09 22.09
-MList::MoreUtils=indexes
使用List::MoreUtils模块indexes
函数
-ale
在空格上拆分输入行并保存到@F
数组,输入行中的换行符将被删除并在打印时添加@i = indexes { /^09/ } @F if $. == 1
如果是第一行,请获取@F
数组中与正则表达式匹配的所有元素的索引/^09/
print join "\t", @F[@i]
打印保存在@F
数组中的@i
匹配索引的元素,以制表符分隔如果List::MoreUtils
模块不可用,
perl -nale '@i = grep { $F[$_] =~ /^09/ } 0..$#F if $. == 1; print join "\t", @F[@i]' ip.txt
答案 1 :(得分:0)
awk对于按行处理数据非常有用,因此您可以先将数据转置。
我就是这样做的:
来自An efficient way to transpose a file in Bash
function transpose() {
awk '{
for (i=1; i<=NF; i++) {
a[NR,i] = $i
}
}
NF>p { p = NF }
END {
for(j=1; j<=p; j++) {
str=a[1,j]
for(i=2; i<=NR; i++){
str=str" "a[i,j];
}
print str
}
}'
}
然后
cat file.txt | transpose | awk 'NR==1 { print $0 } /[0-9]+\/09/ { print $0 }' | transpose