早教吧 育儿知识 作业答案 考试题库 百科 知识分享

Ultraedit数字求和有上万行两列数据,制表符分开,第1列为英语字母组合,第2列为冒号分开的2,3,4或5组数字(如下),需要计算出第2列中所有数字的合.以第一行“abc111:222:333”为例,需要得出“abc66

题目详情
Ultraedit 数字 求和
有上万行两列数据,制表符分开,第1列为英语字母组合,第2列为冒号分开的2,3,4或5组数字(如下),需要计算出第2列中所有数字的合.以第一行“abc 111:222:333”为例,需要得出“abc 666”这种结果,每行如此.如果不能用ultraedit实现,有没有更好的办法,因为行数太多,有可能超出10万行.
.
abc 111:222:333
aa 22875:185
aaaaa 1935:740:4435
a 783:494:480:121
aaaaaaa 277:25:238
bb 254:580:121
bbbbb 243:23:211
b 480:125:185
bbbbbbb 131:1027:740:121
cc 3598:264
ccccc 203:55
c 71:217
ccccccc 2054:2308
ccc 274:1776::121
aaa 868:1261:185
bbb 4435:50:00
.
▼优质解答
答案和解析
Ultraedit 有运算功能,但主要是整列求和 菜单“列”下的“列/选区求和”
直接用 正则表达式 似乎不行.有两种途径:
1.把":"替换成制表符,然后整体拷贝到Excel中,在某列(如第6列)中写公式=b1+c1+d1+e1+f1,下拉适用所有,得出一样的结果.
2.用ultraedit的script也可以实现.
if (UltraEdit.document.length > 0) { // Is any file opened?
// Define environment for this script.
UltraEdit.insertMode();
UltraEdit.columnModeOff();
UltraEdit.activeDocument.hexOff();
UltraEdit.ueReOn();
/* Verify the line termination of the last line of the file
and insert one if the last line has no line termination.*/
UltraEdit.activeDocument.bottom();
if (UltraEdit.activeDocument.isColNumGt(1)) {
UltraEdit.activeDocument.insertLine();
if (UltraEdit.activeDocument.isColNumGt(1)) {
UltraEdit.activeDocument.deleteToStartOfLine();
}
}
UltraEdit.activeDocument.top();
UltraEdit.activeDocument.trimTrailingSpaces();
// Define line terminator type by default for DOS.If the version of
// UE/UES has support for the line terminator property,use it to set
// the line terminator string according to the value of this property.
var sLineTerm = "\r\n";
if (typeof(UltraEdit.activeDocument.lineTerminator) == "number") {
if (UltraEdit.activeDocument.lineTerminator == 1) {
sLineTerm = "\n"; // UNIX file not converted to DOS.
} // Ignore older versions of UltraEdit and MAC files for this
} // script and use in all other cases also DOS line terminators.
UltraEdit.selectClipboard(9);
// Process entire file in blocks of 10000 lines to end of file.
var nLineNum = 1;
do {
nLineNum += 10000;
UltraEdit.activeDocument.gotoLineSelect(nLineNum,1);
if (!UltraEdit.activeDocument.isSel()) break;
var asLines = UltraEdit.activeDocument.selection.split(sLineTerm);
// Process each line of the selected block.
for (var nIndex = 0; nIndex < asLines.length; nIndex++ ) {
// Find first character of a sequence of colon delimited numbers at end of the line.
var nNumberPos = asLines[nIndex].search(/(?:\d+:*)+$/);
// If no number found,for example a blank line,skip the line.
if (nNumberPos < 0) continue;
// Split the line into fixed text and the numbers as string.
var sText = asLines[nIndex].substr(0,nNumberPos);
var sNumbers = asLines[nIndex].substr(nNumberPos);
// Split the numbers string up into an array of strings each with a number.
var asNumbers = sNumbers.split(":");
// If the line contains only 1 number,nothing to do for this line.
if (asNumbers == null) continue;
// Convert the number strings into integers and sum them.
var nSum = 0;
for (var nNumber = 0; nNumber < asNumbers.length; nNumber++ ) {
// Skip empty strings caused by ::between the numbers.
if (asNumbers[nNumber].length == 0) continue;
nSum += parseInt(asNumbers[nNumber],10);
}
// Build new line with fixed text and the sum converted to a decimal number string.
asLines[nIndex] = sText + nSum.toString(10);
}
// Rebuild the block with the modified lines in user clipboard 9.
UltraEdit.clipboardContent = asLines.join(sLineTerm);
// Paste the block over the still selected lines.
UltraEdit.activeDocument.paste();
} while (UltraEdit.activeDocument.isEof() == false);
// Clear user clipboard 9 and select Windows clipboard.
UltraEdit.clearClipboard();
UltraEdit.selectClipboard(0);
}