I need some pointers / example of a powershell script that will extract a string from a file based on the starting position of that string.
Appreciate the feedback.
Powershell
Last Comment
Roberto Madro R.
8/22/2022 - Mon
SubSun
You can use Select-String, for example following command will retrieve the lines strting with subsun from file test.txt gc c:\test.txt | Select-String -Pattern "^subsun"
More for more examples you can refer the following articles. If you have any specific requirement then you can ask, so we can provide you the exact command to retrieve the required string..
Following "Pattern"s is fairly straightforward, my problem is, I don't have a pattern in the file, I however know the starting and endining position of the string I'm after and extracting it that way ensures accuracy.
If you want to extract based on the string position, then you can try to retrieve the line based on index and again use substring to get exact string value....
Following command will return Testing from line 2 (gc test.txt | Select-Object -Index 1).substring(7,7)
Following command will return EE from line 3 (gc test.txt | Select-Object -Index 2).substring(7,2)
Not sure if this is what you are looking for..
Roberto Madro R.
ASKER
We're getting close and I'll try your suggestion, the file I have in mind has only one line and it's around 400 characters, and I'm only interested in extracting any value between position 29 and position 40, sometimes the value between these two positions could be 5 digits (that's for older customers), and sometimes it could be 8,9, 10 digits (for newer customers), so as long as I can get the value (whatever it is) between position 29 and 40 I'm happy.
Thx
SubSun
For single line file, you don't have to add index.. (gc test.txt).substring(28,11) should retrieve the characters from 29 to 40..
Thank you Subsun, I think your suggestion will work, but I just examined one of the files I'm dealing with and found that the spaces between delimiters collapses in based on the number of characters, so if I was to use the solution you recommended, the optimal way would be to begin at position 29 but end at the delimiter, in my case it is a comma "," this way if my customer ID is 5 digits and it begins at positon 29, I'd only need to grab whatever between position 29 and 34 and since the customer ID varies the only way to ensure that I'm only getting the cusomter ID is to grab whatever value between position 29 and the next delimiter. sorry I didn't explain it this way earlier.
Dale Harris
Here's a solution I made a while back that does exactly what you're asking provided the line is always the same except for the customer ID
Using Subsun's previous information:
#We will first need to set the length of the whole string without the customer ID
$NormalLength = 142
#Then we get the length of the actual string
$Length = (gc test.txt).length
#Then we are going to do some simple math to figure out how large the customer ID is
#It's expressed like this: CustomerID Length = Length of string - Normal Length
$CustomerIDLength = $Length - $NormalLength
or if you wanted to get the position 29, then split by "," (comma), then grab from the 29th position to the first comma, you could do something like this:
gc c:\test.txt | Select-String -Pattern "^subsun"
More for more examples you can refer the following articles. If you have any specific requirement then you can ask, so we can provide you the exact command to retrieve the required string..
References :
http://technet.microsoft.com/en-us/library/hh849903.aspx
http://www.computerperformance.co.uk/powershell/powershell_select_string.htm