bytes packets errs drop fifo colls carrier compressed lo: 20114 136 0 0 0 0 0 0 20114 136 0 0 0 0 0 0
我想用正则表达式匹配 lo 这个部分。。。
完整可用代码如下:
exports.info = function() {
// read /proc/net/dev
var fc = fs.readFileSync('/proc/net/dev','utf8'),
result = {};
// format file content
fc = fc.replace(/\s{2,}/g,' ').replace(': ',':').split("\n");
var columnLine = fc[0].split('|');
fc[0] = columnLine[columnLine.length - 1];
columnLine[columnLine.length - 1] = fc[0].substring(0, fc[0].search(/(\b\S+\:.*)/g));
fc[0] = fc[0].substring(fc[0].search(/(\b\S+\:.*)/g));
for(var i=0;i<fc.length;i++) {
if(fc[i]=='') { continue; }
var receiveCols = columnLine[columnLine.length - 2].split(' '),
transmitCols = columnLine[columnLine.length - 1].split(' '),
tKey = fc[i].substring(0,fc[i].indexOf(':')),
tValue = fc[i].substring(fc[i].indexOf(':') + 1).split(' '),
tArr = {}, item;
while(item = tValue.shift()) {
var title = (receiveCols.length>0 ? 'recv' : 'trans') + '_' + (receiveCols.shift() || transmitCols.shift())
tArr[title] = item;
}
result[tKey] = tArr;
}
return result;
}