Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:
CSS
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:
CSS
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 ?
Posted
Updated 24-Oct-15 4:26am
v4
Comments
Member 12083305 24-Oct-15 19:41pm    
thx for the solution.
How do i select only the 2nd line and the last line ?
And from the last line only the value right of the ;

finaly the file must look like this mnupowein;value;mnupower;value;mnucurrent;value; etc etc

1 solution

$A = get-content -path $filePath | select -first 3 -last 1

The problem is that you read the entire file and then transmit it to select via a pipe(|).
Should certainly be better to do somothing like
$A = get-content -path $filePath -head 3;get-content -path $filePath -tail 1

This way, get-content can optimize the file reading.

[Update]
Have look at Example 4 on this page https://technet.microsoft.com/en-us/library/hh849787.aspx[^]
 
Share this answer
 
v2
Comments
Member 12083305 25-Oct-15 7:41am    
Hi I tried your solution.
it did not work i got errors":

Get-Content : A parameter cannot be found that matches parameter name 'head'.
At E:\c\test.ps1:13 char:33
+ $A = get-content $filePath -head <<<< 3;get-content $filePath -tail 1
+ CategoryInfo : InvalidArgument: (:) [Get-Content], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetContentCommand

Get-Content : A parameter cannot be found that matches parameter name 'tail'.
At E:\c\test.ps1:13 char:63
+ $A = get-content $filePath -head 3;get-content $filePath -tail <<<< 1
+ CategoryInfo : InvalidArgument: (:) [Get-Content], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetContentCommand

the -head and -tail parameters are not recognized
Patrice T 25-Oct-15 11:30am    
update answer
Member 12083305 25-Oct-15 11:53am    
i tried that solution this is the result:

e:\a\*.tmp | ForEach {Get-Content $_ -Head 1; Get-Content $_ -Tail 1}
The term 'e:\a\*.tmp' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spel
ling of the name, or if a path was included, verify that the path is correct and try again.
At line:2 char:11
+ e:\a\*.tmp <<<< | ForEach {Get-Content $_ -Head 1; Get-Content $_ -Tail 1}
+ CategoryInfo : ObjectNotFound: (e:\a\*.tmp:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

stil the same head and tail not recognized
Patrice T 25-Oct-15 13:28pm    
Are you using an old version of powershell ?
Member 12083305 25-Oct-15 15:13pm    
I had version 2.0 now i have v3.0 do i have to upgrade to v4 ? or wil 3 do the trick ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900