Link to home
Start Free TrialLog in
Avatar of jt30605
jt30605

asked on

PowerShell to pull value between two characters in a text file.

I need to pull the value that is between two characters in a text file.  For example, the contents of my text file are:

test parse.
I want to pull the value
between the pounds. The value is #456#.

How can I grab the "456" value?  As close as I've gotten is using the select-string statemnt I can get the entire line that includes the value, but I cannot get just that value.

My select-string statement is $str = Select-String -Pattern "#(.*)#" c:\testparse.text

Any help would be greatly appreciated.
ASKER CERTIFIED SOLUTION
Avatar of Shift-3
Shift-3
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RMDOps
RMDOps

Hi,

I have used these 5 steps for my script, a problem I am getting is it is stopping on the first result, and not going through the entire file. Seems it's not looping

$messagesubjects=import-csv C:\email.csv | Out-String
foreach($messagesubject in $messagesubjects) {
$start= $messagesubject.indexof("#") +1
$end= $messagesubject.indexof("#", $start)
$length =$end - $start
$messagesubjects.substring($start, $length) | out-file C:\usertest.txt -Append
}