[英]Pick up particular lines from a text file and calculate them together
这可能是一个漫长的过程,但是我为自己编写了一个AppleScript,可以随时记录我在任何项目中工作了多长时间。 我想创建另一个脚本,该脚本根据日志文件中的信息计算总花费时间。
通常,我的日志文件如下所示:
140304 1353 - Start
140304 1459 - End
work time : 0106
break time : 0000
140307 1248 - Start
140307 1353 - End
work time : 0105
break time : 0000
140321 1101 - Start
140321 1306 - Have a break now
140321 1342 - Back to work
140321 1423 - Have a break now - Go eat
140321 1522 - Back to work
140321 1522 - End
work time : 0246
break time : 0135
因此,我需要获取每个“工作时间”值并一起计算。
我曾尝试使用Google搜索,但不确定如何开始使用。
Applescript的文本项定界符(TID)非常适合此功能。 我们通过将TID设置为您要获取的号码之前的文本(在这种情况下为“工作时间”),将文本分成一个列表。 然后我只抓住TID之后的下一个单词,因为那是数字。 这很容易。 您的示例的结果为457。我的脚本返回总和,并显示用于计算总和的所有值:{457,{“ 0106”,“ 0105”,“ 0246”}}
set workLog to "140304 1353 - Start
140304 1459 - End
work time : 0106
break time : 0000
140307 1248 - Start
140307 1353 - End
work time : 0105
break time : 0000
140321 1101 - Start
140321 1306 - Have a break now
140321 1342 - Back to work
140321 1423 - Have a break now - Go eat
140321 1522 - Back to work
140321 1522 - End
work time : 0246
break time : 0135"
set beforeText to "work time"
set AppleScript's text item delimiters to beforeText
set textItems to text items of workLog
set AppleScript's text item delimiters to ""
set theSum to 0
set sumItems to {}
repeat with i from 2 to count of textItems
set workTime to word 1 of (item i of textItems)
set end of sumItems to workTime
set theSum to theSum + (workTime as number)
end repeat
return {theSum, sumItems}
现在,只需将我的代码中的第一行替换为此,它将在您的日志文件中为您工作。 祝好运。
set workLog to read (choose file)
这是@ regulus6633的纯AppleScript答案的基于do shell script
的替代方法:
注意:我不确定您的工作时间数字(例如0106
代表什么-在这里我只是假设它们是要求和的十进制整数。
set logFile to "/Users/jdoe/hours.log" # Replace with POSIX path to your log file
set total to do shell script ¬
"awk '/^work time :/ { total+=$4 } END { print total }' " ¬
& quoted form of logFile
shell命令使用awk
,这大大简化了解析过程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.