2012-03-07 36 views
0

log.txt的格式:怎麼做我想從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環境。 和我想要得到這樣的結果 「設置ABC2 =正常;設置ABC3 =正常或可用;設置abc4 =正常」

+1

什麼字符串是你想獲得? – gfortune 2012-03-07 03:20:56

+0

這個問題被標記爲Python,C,sed和AWK ?! – chrisaycock 2012-03-07 03:30:22

+1

數據格式大喊「使用awk」是一件好事,否則我會問同樣的問題。現在我只能得到awk -F,'... {print'abc =「$ 5}; ...'log.txt – technosaurus 2012-03-07 03:31:36

回答

1

在Python:

for line in open('log.txt'): 
    print 'abc=' + line.split(',')[4] 
1
[[email protected] ~/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" 
[[email protected] ~/tmp]$ cat doit.awk 
#!/usr/bin/awk -f 

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

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

[[email protected] ~/tmp]$ ./doit.awk 2 data 
Normal 
[[email protected] ~/tmp]$ ./doit.awk 1 data 
Available 
[[email protected] ~/tmp]$ 

如果你的索引保證不在文件中重複,你可以在print $5之後加上nextfile來加快速度。

+0

好的,謝謝你的支持 – Sam 2012-03-07 05:23:22

+1

@getjoefree - +1是一種很好的方式來表達謝意。 :) – Graham 2012-03-09 04:22:31

0

這可能會爲你工作:

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