• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Remove first 35 chars in text File Window OS

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

trekky

Member
Joined
Oct 3, 2011
I have a text file that I filtered to get a number and have only one line left in the text file
"test.txt:3: text2.txt:258:Raw Value : 39,716"

I want to take the last number which In this case is "39,716" and need to use is as a variable such as if larger then 20,000 go to X
but I am getting a false reading due to the first 35 chars,
so in windows through a command line or power shell (or something that can be scripted) I want to remove "test.txt:3: text2.txt:258:Raw Value : " and only leave "39,716" (or have cmd or PowerShell read the last number and create a new text file with the number inside
note 39,716 is a variable based on output of a larger output of a text file

thx
 

ebug122

Member
Joined
Nov 1, 2009
Location
Ontario, Canada
This thread may not be required anymore, but I thought I'd try it out in Powershell:

Contents of test.txt:
Code:
test.txt:3: text2.txt:258:Raw Value : 39,716

script.ps1
Code:
[COLOR=#008000]#Assign the contents of test.txt to the variable $file[/COLOR]
$file = Get-Content C:\Temp\test.txt

[COLOR=#008000]#Splits the line from test.txt into an array using a colon as the delimiter[/COLOR]
$result = $file.split(":")

[COLOR=#008000]#Select the last value in the array (39,716), remove the leading space character and output it to result.txt[/COLOR]
$result[-1].TrimStart() | Out-File C:\Temp\result.txt

Contents of result.txt:
Code:
39,716
 

pcgamer4life

Member
Joined
Dec 30, 2004
This thread may not be required anymore, but I thought I'd try it out in Powershell:

Contents of test.txt:
Code:
test.txt:3: text2.txt:258:Raw Value : 39,716

script.ps1
Code:
[COLOR=#008000]#Assign the contents of test.txt to the variable $file[/COLOR]
$file = Get-Content C:\Temp\test.txt

[COLOR=#008000]#Splits the line from test.txt into an array using a colon as the delimiter[/COLOR]
$result = $file.split(":")

[COLOR=#008000]#Select the last value in the array (39,716), remove the leading space character and output it to result.txt[/COLOR]
$result[-1].TrimStart() | Out-File C:\Temp\result.txt

Contents of result.txt:
Code:
39,716

This is probably as good a method as any. Simply removing the first 35 chars isn't going to cut it since there's (presumably) the potential for the first few values to have fewer/more digits which would throw you off.

You could also do it something like this:
Code:
PS C:\tmp\> $data = "test.txt:3: text2.txt:258:Raw Value : 39,716"
PS C:\tmp\> $value = $data.Substring($v.IndexOf("Raw Value : ")+12)
PS C:\tmp\> $value
[b]39,716[/b]

or
Code:
PS C:\tmp\> $data = "test.txt:3: text2.txt:258:Raw Value : 39,716"
PS C:\tmp\> $value = $data.Substring($v.LastIndexOf(':')+2)
PS C:\tmp\> $value
[b]39,716[/b]