我想提取從以下字符串個別詞: -如何在unix中多行提取單詞?
Exported Layer : missing_hello
Comment :
Total Polygons : 20000 (reported 100).
我想從上面的字符串中提取單詞「missing_hello」和「2000」,並希望將其顯示爲
missing_hello : 20000
如何在unix中做到這一點?
我想提取從以下字符串個別詞: -如何在unix中多行提取單詞?
Exported Layer : missing_hello
Comment :
Total Polygons : 20000 (reported 100).
我想從上面的字符串中提取單詞「missing_hello」和「2000」,並希望將其顯示爲
missing_hello : 20000
如何在unix中做到這一點?
假設比missing_hello是每次一個字 - 你可以:
perl -lane '$el=$F[3] if(/Exported Layer/); print "$el: $F[3]" if(/Total Polygons/);'
在awk
:
awk -F: '/Exported Layer/ { export_layer = $2 }
/Total Polygons/ { printf("%s : %s\n", export_layer, $2); }' "[email protected]"
如果輸入是垃圾,輸出將是太(GIGO)。如果這些字段可以包含冒號,那麼生活會變得更加混亂。
在sed
:
sed -n -e '/Exported Layer : *\(.*\)/{s//\1 : /;h;}' \
-e '/Total Polygons : *\(.*\)/{s//\1/;x;G;s/\n//;p;}' "[email protected]"
冒號領域不與本sed
版中的問題。
現在在MacOS X 10.6.7上測試。這兩個腳本都包含「總多邊形」行中的數字後面的註釋。這兩種腳本都可以很容易地修改爲僅打印數字並忽略評論。這將有助於對所有格式的可能性進行精確定義。
我可能實際上使用Perl(或Python)來完成這項工作;現場拆分只是混亂得足以從這些語言中更好的設施中受益。
看看這個指南指http://www.grymoire.com/Unix/Sed.html
桑達無疑是值得學習的工具。我會專門研究題爲「使用\ 1保留部分模式」和「使用多行」的章節。
如果你有Perl,你可以這樣做:
use strict;
use warnings;
my $layer;
my $polys;
while (<>) {
if ($_ =~ m{^Exported \s Layer \s : \s (\S+)}xms) {
$layer = $1;
next;
}
if ($_ =~ m{^Total \s Polygons \s : \s (\d+)}xms) {
$polys = $1;
}
if (defined $layer && defined $polys) {
print "$layer : $polys\n";
$layer = $polys = undef;
}
}
我瘋玩。我沒有讀過'undef'這行...我會刪除我的主要評論。 – 2011-06-14 22:09:07