0

I have a script that monitors a specific process and I have a working script that outputs it as such;

0   0   0

I am using this script to try to cut everything after the first "0" using this script

Get-Content –path c:\batchjobs\location\test.txt | Trim(Char[8,60]) > c:\batchjobs\location\test1.txt

the powershell ise keeps erroring out, here's the output, from that:

PS C:\batchjobs\location> C:\batchjobs\location\TestTrim.ps1
The term 'Trim' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, 
or if a path was included, verify that the path is correct and try again.
At C:\batchjobs\location\TestTrim.ps1:2 char:58
+ Get-Content –path c:\batchjobs\location\test.txt | Trim <<<< (Char[8,60]) > c:\batchjobs\location\test1.txt
    + CategoryInfo          : ObjectNotFound: (Trim:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

any help would be greatly appreciated

2 Answers 2

3

Methods such as Trim and SubString require an object to operate on, you are not telling the script which object you mean. In the context you will want to indicate $_ for the result object. Also, Get-Content will return all the lines of the file, iterate over them using For-Each a.k.a %.

Get-Content -path c:\batchjobs\location\test.txt | % { $_.Substring(0,7).Trim(); } > c:\batchjobs\location\test1.txt
Sign up to request clarification or add additional context in comments.

3 Comments

I wasn't sure what you meant by Char[8,60] so I just put Substring(8,1) which will only output the 8th character of the line. If you have other intention perhaps you can modify this to your liking or let me know what you intended.
Oh sorry the Char[8,60] was my attempt to delete everything from the 8th character of the line on, all I need is the first 7 characters, does this help
Sure, just use Substring(0,7) - I'll update my example. Hope it helps.
0

Assuming your files are of a size such that you won't start getting out of memory errors using get-content, then this approach is fine. If you want the first 8 chars of a huge file (maybe 1GB in size?), then the following approach might be a little more flexible:

$stream = [System.IO.StreamReader]"C:\Users\Ryan\Desktop\test.csv"
$i=0

#read characters until we get to 8 or end of the stream. won't load the while file into memory
$output = while(($i -lt 8) -and (!$stream.EndOfStream)){ [char]$stream.Read(); $i++ }
$stream.close()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.