从多个文件中获取最后一行文本并写入新文件 [英] get last line text from multiple files and write in a new file

查看:79
本文介绍了从多个文件中获取最后一行文本并写入新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



i有以下情况:

有15个包含数据的日志文件(文本)



每个文件都以某种标题开头,我只对3行感兴趣。

该文件包含值(tekst)从上到下书写,我对文件的最后一行感兴趣,更具体的是在...之后的最后一个值(文本);



这是日志文件的外观喜欢

 W 
-10496
mnuPowerIn


63581203668; 210,34
63581203728; 203,46
63581203788; 204,65
63581203848; 206,15
63581203908; 204,35
63581203968; 198,97



另一个日志文件

 W 
-10496
mnuPower


63581203668 ; 166,81
63581203728; 165,45
63581203788; 164,64
63581203848; 165,18
63581203908; 163,28
63581203968; 159,21
63581204029; 154,86



输出文件shouls l喜欢这样:

 mnuPowerIn:198,97 
mnuPower:154,86



i am全新的Powershell发现了一些命令,但结合命令是一项艰巨的工作。



最后但并非最不重要我想让这个脚本每分钟运行一次,所以输出文件是总是包含实际值,如果我可以使用.cmd或.bat文件运行它会很棒



感谢先进





我做了一个几乎可以做到这一点的代码



 $ input_path = 'e:\A\ * .tmp'
$ fileDirectory =e:\ a
$ output_file ='e:\A\output.txt'

Clear-Content $ output_file

foreach(Get-ChildItem $ fileDirectory中的$ file)

{
$ filePath = $ fileDirectory +\ + $文件;
$ A = get-content -path $ filePath | select -first 3 -last 1
Add-Content $ output_file $ A
}





它无效完美无瑕。

输出文件中的输出与控制台中的输出不同

控制台输出:

  A  
- 1146130
逆变器 2 - mnuCurrent
63581293796; 0, 83
Wh
- 16728065
逆变器 2 - mnuTodayEnergy
63581293796; 447, 00
W
- 10496
mnuPower
63581293796; 153, 78
W
- 10496
逆变器 2 - mnuPower
63581293796; 153, 78
W
- 10496
mnuPowerIn
63581293796; 193, 58







文件输出:

  A  
- 1146130
逆变器 2 - mnuCurrent
63581293796; 0 , 83
Wh
- 16728065
逆变器 2 - mnuTodayEnergy
63581293796; 447, 00
A
- 1146130
逆变器 2 - mnuCurrent
63581293796; 447, 00
W
- 10496
mnuPower
63581293796; 153, 78
W
- 10496
逆变器 2 - mnuPower
63581293796; 153, 78







i看到一个447,00的mnu当前奇怪的不在控制台上



任何提示?



i现在运行它用于测试目的,但我注意到脚本使用了大量的处理器时间,任何想法让这个脚本运行得更快?

解决方案

input_path ='e:\A\ * .tmp'


fileDirectory =e:\ a


output_file ='e :\一个\\ output.txt的

清除-内容

Hi Guys,

i have the following situation:
there aree 15 log files that contain data (text)

Each file is starting with some kind of header, i am only interested in the 3line.
The file contains value's (tekst) is written top to bottom, i am interested in the last line of the file, to be more specific the last value (text) that is after the ;

this is how a log file looks like

W
-10496
mnuPowerIn


63581203668;210,34
63581203728;203,46
63581203788;204,65
63581203848;206,15
63581203908;204,35
63581203968;198,97


another log file

W
-10496
mnuPower


63581203668;166,81
63581203728;165,45
63581203788;164,64
63581203848;165,18
63581203908;163,28
63581203968;159,21
63581204029;154,86


the output file shouls look like this:

mnuPowerIn:198,97
mnuPower:154,86


i am completely new with Powershell found some commands with but combining the command is a tough job.

last but not least i want this script to run each minute so the output file is always containing the actual values, it would be great if i can run it with a .cmd or .bat file

thanks in advanced


I made a code that almost do the trick

$input_path = ‘e:\A\*.tmp’
$fileDirectory = "e:\a"
$output_file = ‘e:\A\output.txt’

Clear-Content $output_file

foreach($file in Get-ChildItem $fileDirectory )

{
$filePath = $fileDirectory + "\" + $file;
$A = get-content -path $filePath | select -first 3 -last 1 
Add-Content $output_file $A
}



it is not working flawless.
The output in the output file is different than the output in the console
console output:

A
-1146130
Inverter #2 - mnuCurrent
63581293796;0,83
Wh
-16728065
Inverter #2 - mnuTodayEnergy
63581293796;447,00
W
-10496
mnuPower
63581293796;153,78
W
-10496
Inverter #2 - mnuPower
63581293796;153,78
W
-10496
mnuPowerIn
63581293796;193,58




file output:

A
-1146130
Inverter #2 - mnuCurrent
63581293796;0,83
Wh
-16728065
Inverter #2 - mnuTodayEnergy
63581293796;447,00
A
-1146130
Inverter #2 - mnuCurrent
63581293796;447,00
W
-10496
mnuPower
63581293796;153,78
W
-10496
Inverter #2 - mnuPower
63581293796;153,78




i see a mnu current of 447,00 that is strange that is not on the console

any tips ?

i have it running now for test purposes, but i noticed that the script uses a lot of processor time, any idea's to make this script run faster ?

解决方案

input_path = ‘e:\A\*.tmp’


fileDirectory = "e:\a"


output_file = ‘e:\A\output.txt’ Clear-Content


这篇关于从多个文件中获取最后一行文本并写入新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆