怎么办我想从log.txt文件中获取此字符串

时间:2012-03-07 03:19:15

标签: python c awk sed

log.txt格式:

;Index,Level,StripSize,SizeinMB,Status,Bootable,Array,Name
0,0,32kB,8192,"Normal","Yes",0,"FFS"
1,0,128kB,19046,"Normall","No",0,"Dev_Cache"
2,0,64kB,305244,"Normalll","Yes",1,"0W24SWKT"

或其他格式:

;Index,Level,StripSize,SizeinMB,Status,Bootable,Array,Name
0,0,32kB,8192,"Normal","Yes",0,"FFS"
1,0,128kB,19046,"Available","No",0,"Dev_Cache"
2,0,64kB,305244,"Normal","Yes",1,"0W24SWKT"
下面是我的步骤:

iffs.sed
/Bootable/{
n;
s/^.*[0-9],\"([a-zA-Z]+)\",.*$/set abc=\1/p;
}
1. sed -nrf iffs.sed log.txt
result: set abc=Normal
2. sed "2d" log.txt |sed -nrf iffs.sed
result: set abc=Normal or Available
3. sed "2,3d" log.txt |sed -nrf iffs.sed
result: set abc=Normal
你可以用简单的方法帮忙吗? windows os和winpe 3.0环境。 我想得到这个结果“set abc2 = Normal; set abc3 = Normal or Available; set abc4 = Normal”

3 个答案:

答案 0 :(得分:1)

在Python中:

for line in open('log.txt'):
    print 'abc=' + line.split(',')[4]

答案 1 :(得分:1)

[ghoti@pc ~/tmp]$ cat data 
;Index,Level,StripSize,SizeinMB,Status,Bootable,Array,Name
0,0,32kB,8192,"Normal","Yes",0,"FFS"
1,0,128kB,19046,"Available","No",0,"Dev_Cache"
2,0,64kB,305244,"Normal","Yes",1,"0W24SWKT"
[ghoti@pc ~/tmp]$ cat doit.awk 
#!/usr/bin/awk -f

BEGIN {
  FS=",";
  ind=ARGV[1];
  ARGV[1]="";
}

$1 == ind {
  gsub(/"/, "", $5);
  print $5;
}

[ghoti@pc ~/tmp]$ ./doit.awk 2 data
Normal
[ghoti@pc ~/tmp]$ ./doit.awk 1 data
Available
[ghoti@pc ~/tmp]$ 

如果您的索引保证不会在文件中重复,那么您可以在nextfile后加print $5来加快速度。

答案 2 :(得分:0)

这可能对您有用:

sed '1d;=;s/[^"]*"\([^"]*\).*/\1/' log.txt | sed 'N;s/\n/=/;s/^/set abc/'
set abc2=Normal
set abc3=Available
set abc4=Normal