Chinaunix
标题:
需要按照自己的格式输出,应该如何做(有答案的同时有讲解更好),awk?
[打印本页]
作者:
lzolder
时间:
2010-05-26 11:39
标题:
需要按照自己的格式输出,应该如何做(有答案的同时有讲解更好),awk?
原始文件如:
物质1:
属性1:aaaa
属性2:bbb
属性3:ccc
...
物质2:
属性1:ddd
属性2:eee
属性3:fff
...
...
复制代码
想得到如下输出
属性1 属性2 属性3
aaa bbb ccc
ddd eee fff
复制代码
请高手帮忙,如果能提供讲解,那就更感谢了。
作者:
where27
时间:
2010-05-26 12:20
回复
1#
lzolder
awk -F: 'BEGIN{print "属性1\t属性2\t属性3"}/属性[12]/{printf $2"\t"}/属性3/{print $2}' file
复制代码
BEGIN{...}打印表头,后面打印相应的内容,不过文本里属性1属性2属性3得是按顺序排下来的,不然就不灵了
作者:
bbgg1983
时间:
2010-05-26 12:35
回复
2#
where27
有省略号啊,表头的属性不止有三个吧?直接打估计够呛
作者:
renxiao2003
时间:
2010-05-26 12:52
是啊。这个数据有点难度。
作者:
lzolder
时间:
2010-05-26 12:58
本帖最后由 lzolder 于 2010-05-26 13:04 编辑
首先谢谢老表的回复。bbgg1983说对了,我按照老表的思路没实现
我的原始文件如下
Enclosure Device ID: 8
Slot Number: 22
Device Id: 31
Sequence Number: 2
Media Error Count: 0
Other Error Count: 0
Predictive Failure Count: 0
Last Predictive Failure Event Seq Number: 0
PD Type: SATA
Raw Size: 931.512 GB [0x74706db0 Sectors]
Non Coerced Size: 931.012 GB [0x74606db0 Sectors]
Coerced Size: 930.390 GB [0x744c8000 Sectors]
Firmware state: Online
SAS Address(0): 0x500e004aaaaaaa16
Connected Port Number: 4(path0)
Inquiry Data: 9QJ3LCXTST31000340NS SN06
FDE Capable: Not Capable
FDE Enable: Disable
Secured: Unsecured
Locked: Unlocked
Foreign State: None
Device Speed: 3.0Gb/s
Link Speed: 3.0Gb/s
Media Type: Hard Disk Device
Enclosure Device ID: 8
Slot Number: 23
Device Id: 32
Sequence Number: 4
Media Error Count: 0
Other Error Count: 0
Predictive Failure Count: 0
Last Predictive Failure Event Seq Number: 0
PD Type: SATA
Raw Size: 931.512 GB [0x74706db0 Sectors]
Non Coerced Size: 931.012 GB [0x74606db0 Sectors]
Coerced Size: 930.390 GB [0x744c8000 Sectors]
Firmware state: Online
SAS Address(0): 0x500e004aaaaaaa17
Connected Port Number: 4(path0)
Inquiry Data: 9QJ3EBPVST31000340NS SN06
FDE Capable: Not Capable
FDE Enable: Disable
Secured: Unsecured
Locked: Unlocked
Foreign State: None
Device Speed: 3.0Gb/s
Link Speed: 3.0Gb/s
Media Type: Hard Disk Device
复制代码
我想要得到的结果是:
Enclosure ID Slot Number Device Id Raw Size Inquiry Data
8 22 31 931.512 GB 9QJ3LCXTST31000340NS
8 23 32 931.512 GB 9QJ3EBPVST31000340NS
复制代码
我的原始文件不只2个磁盘信息,还有很多个
而我想得到的结果,我不仅限于Enclosure Device ID、Slot Number、Device Id、Raw Size、Inquiry Data,比如我可能需要去掉Device id,然后增加Link Speed属性
不知我说清楚我的需求了没?
作者:
ywlscpl
时间:
2010-05-26 13:08
awk -F ':' '{if (/:$/) {m=0;n++}else if ($2) {a[n","$1]=$2;m++;b[n","m]=$1}}END{for (i=1;i<=m;i++) printf b[1","i]"\t";print "";for (i=1;i<=n;i++) {for (j=1;j<=m;j++) printf a[i","b[i","j]]"\t";print ""}}' file
复制代码
作者:
ywlscpl
时间:
2010-05-26 13:10
写好了,发现数据已经变了
作者:
ywlscpl
时间:
2010-05-26 13:32
全部属性
awk -F '\n' -v RS= '{for (i=1;i<=NF;i++) {split($i,m,":");a[NR","i]=m[1];b[NR","i]=m[2]}}END{for (i=1;i<=NF;i++) printf a[1","i]"\t";print "";for (i=1;i<=NR;i++) {for (j=1;j<=NF;j++) printf b[i","j]"\t";print ""}}' file
复制代码
作者:
lzolder
时间:
2010-05-26 14:11
本帖最后由 lzolder 于 2010-05-26 14:14 编辑
按照老表的思路我如果这样写
awk -F: 'BEGIN {print "Enclosure Device ID\tSlot Number\tDevice Id"}/Enclosure Device ID/{print $2}' file
复制代码
可以输出:
Enclosure Device ID Slot Number Device Id
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
复制代码
但是我写
awk -F: 'BEGIN {print "Enclosure Device ID\tSlot Number\tDevice Id"}/Enclosure Device ID/{print $2}/Slot Number/{print $2}' file
复制代码
结果就变成了:
Enclosure Device ID Slot Number Device Id
8
0
8
1
8
2
8
3
8
4
8
5
8
6
8
7
8
8
8
9
8
10
8
11
8
12
8
13
8
14
8
15
8
16
8
17
8
18
8
19
8
20
8
21
8
22
8
23
复制代码
我应该如何实现0 1 2 3 4 ... 23出现在第二列呢?
作者:
bbgg1983
时间:
2010-05-26 14:35
回复
8#
ywlscpl
awk在你手里真是无所不能丫
作者:
Shell_HAT
时间:
2010-05-26 14:41
回复
9#
lzolder
awk -F: 'BEGIN {print "Enclosure Device ID\tSlot Number\tDevice Id"}/Enclosure Device ID|Slot Number/{printf $2"\t"}/Device Id/{print $2}' file
复制代码
作者:
lzolder
时间:
2010-05-26 14:42
全部属性
ywlscpl 发表于 2010-05-26 13:32
如果是部分属性呢?好好研究下你的脚本,头晕中
作者:
ywlscpl
时间:
2010-05-26 14:45
想输出什么字段,在BEGIN里加
awk -F '\n' -v RS= 'BEGIN{d["Enclosure ID"];d["Slot Number"];d["Device Id"];d["Raw Size"];d["Inquiry Data"]}{for (i=1;i<=NF;i++) {split($i,m,":");a[NR","i]=m[1];b[NR","i]=m[2]}}END{for (i=1;i<=NF;i++) if (a[1","i] in d) printf a[1","i]"\t";print "";for (i=1;i<=NR;i++) {for (j=1;j<=NF;j++) if (a[1","j] in d) printf b[i","j]"\t";print ""}}' file
复制代码
作者:
where27
时间:
2010-05-26 14:45
回复
13#
ywlscpl
高手高手高高手
作者:
lzolder
时间:
2010-05-26 15:00
awk -F: 'BEGIN {print "Enclosure Device ID\tSlot Number\tDevice Id"}/Enclosure Device ID|Slot Number/{printf $2"\t"}/Device Id/{print $2}' file
复制代码
如果我增加一个属性,PD Type
是否应该写成
awk -F: 'BEGIN {print "Enclosure Device ID\tSlot Number\tDevice Id"}/Enclosure Device ID|Slot Number|Device Id/{printf $2"\t"}/PD Type/{print $2}' file
复制代码
作者:
Shell_HAT
时间:
2010-05-26 15:01
换个思路:
awk -F: '/Enclosure Device ID|Slot Number|Device Id/{a[$1]=a[$1]":"$2}END{for(i in a)print i,a[i]}' urfile | awk -F: '{for(i=1;i<=NF;i++)a[NR,i]=$i}END{for(i=1;i<=NF;i++){for(j=1;j<=NR;j++)printf ("%-20s",a[j,i]);print ""}}'
复制代码
想输出什么字段,在pattern里加
作者:
lzolder
时间:
2010-05-26 15:35
awk -F: 'BEGIN {print "Enclosure ID\tSlot\tDevice Id\t\tPD Type\t\t\tRaw Size\t\t\t\tstate"}/Enclosure Device ID|Slot Number|Device Id|PD Type|Raw Size/{printf $2"\t\t"}/Firmware state/{print $2}' file
复制代码
我用以上代码基本实现了我想要得到的输出
下来看看shell_hat 和ywlscpl两位高手的代码,实在是晕得厉害
作者:
expert1
时间:
2010-05-26 22:23
本帖最后由 expert1 于 2010-05-27 12:42 编辑
很简单,行列互换可以说无非是换ORS,FS,RS,OFS这几个,看你玩的是否熟练罢了
awk 'BEGIN{FS="[:\[]";print"Enclosure Device ID\tSlot Number\tDevce Id\tPD Type\tRaw Size"}
{ if(/Enclosure Device ID|Slot Number|Device Id|PD Type|Raw Size/) {ORS="\t"; print $2} else if(/^$/) {ORS="\n"; print " "} }'
复制代码
结果为:
Enclosure Device ID Slot Number Devce Id PD Type Raw Size
8 22 31 SATA 931.512 GB
8 23 32 SATA 931.512 GB
复制代码
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2