Get data from string and output

I’m reading a text file looking for a string that start with $X. When I find that string I want to get the next 7 positions from
that string. The values in file are separated by a comma.

example:

$M00000000,$F000000,$J08,$V1000,$f000000,$m0001274,$n0001274
$Z,$I RW6 ,$N 1 ,$G201120,$H141340,$M00000000,$F000000,$X0000001240,$U0000

I want to get the 0000001240 value in a variable.

Thanks.

 

I don’t understand the “7 positions” requirement as your desired result doesn’t seem to show that.

# Finds text between $X and the next comma
(Select-String -Path file.txt -Pattern '(?<=\$X).*?(?=,)').Matches.Value

# Returns the next 7 characters after $X
(Select-String -Path file.txt -Pattern '(?<=\$X).{7}').Matches.Value

Could it be checked by line?

foreach ($linelvl in $lines)

 

Thx.

 

Sure, but Select-String -Path without -Raw switch does read line by line already.

foreach ($linelvl in $lines) {
    ($linelvl | Select-String -Pattern '(?<=\$X).*?(?=,)').Matches.Value
}

Sorry, I was trying to get just that value in a variable I could use in an ouput.

something like:

$wo = ($linelvl | Select-String -Pattern ‘(?<=$X).*?(?=,)’).Matches.Value
Thx again.

This is my full script, and I’m trying to add the Value from the $X string to my last field of my output.

THanks… hope this helps explain better what I’m trying to achive.

param
(
  [string]$path
)
$myFiles = Import-Csv -Delimiter '~' -Path $path -Header Path, ShortName
# Then you can call it like so
$myFiles.Path #Returns just the path
$myFiles.ShortName # Returns the shortname
$path='G:\AreaSistemi1\'
$count = 0
foreach ($myFile in $myFiles)
{
	get-childitem -path $myFile.Path *.spx | % {
       " Starting File Process....."  >> $myFile.Pathsistem.log
	   $PDate = get-date
       $date = get-date -uformat "_%d_%m_%Y_%H%M%S.bak"
       #"Set backup date to $date" >> $path\sistem.log
       $newname=($_.fullname -replace ".SPX",$date)
	   $file = [System.IO.Path]::GetFileNameWithoutExtension($_.fullname)
       "File backup name is $newname" >> $myFile.Pathsistem.log 
       Rename-Item $_.fullname $newname
	   "Renamed file to $newname" >> $myFile.Pathsistem.log
	   $lines = get-content $newname
	   
	  
	   $count = 0
	   
	   foreach ($line in $lines[0..($lines.length -2)]) 
	   
	   
	    {
			$line + " " + $myFile.ShortName  | Out-File "$path\sistem.csv"   -append
			$count ++
			
	    }		
       Write-Host $_.fullname 
      "Files Processed on $PDate are $newname and Lines Processed $count " >> $path\sistem.log
	}
}

 

The last line of the files contains the $X, but I strip that line before writing the output. I need to somehow capture that
value before I remove that line during my output write.

sample data from a file.
$M00000000,$F000000,$J08,$V1000,$f000000,$m0000000,$n0000000
$M00000000,$F000000,$J08,$V1000,$f000000,$m0000000,$n0000000
$M00000000,$F000000,$J08,$V1000,$f000000,$m0000000,$n0000000
$M00000000,$F000000,$J08,$V1000,$f000000,$m0001274,$n0001274
$Z,$I RW6 ,$N 1 ,$G201120,$H141340,$M00000000,$F000000,$X0000001240,$U0000

Here are some methods:

#emulate get-content
$lines = @'
$M00000000,$F000000,$J08,$V1000,$f000000,$m0000000,$n0000000
$M00000000,$F000000,$J08,$V1000,$f000000,$m0000000,$n0000000
$M00000000,$F000000,$J08,$V1000,$f000000,$m0000000,$n0000000
$M00000000,$F000000,$J08,$V1000,$f000000,$m0001274,$n0001274
$Z,$I RW6  ,$N   1  ,$G201120,$H141340,$M00000000,$F000000,$X0000001240,$U0000
'@ -split [environment]::NewLine

$wo = ($lines[-1] | Select-String -Pattern '(?<=\$X).*?(?=,)').Matches.Value
#or
$wo = ($lines | Select-Object -Last 1 | Select-String -Pattern '(?<=\$X).*?(?=,)').Matches.Value

#Convert into PSObject
$restOfData = $lines | Select-Object -First ($lines.Count -1) | ConvertFrom-Csv -Header 'Hdr1','Hdr2','Hdr3','Hdr4','Hdr5','Hdr6','Hdr7'

OutPut:

PS C:\Users\rasim> $wo

0000001240
PS C:\Users\rasim> $restOfData | Format-Table -AutoSize


Hdr1       Hdr2     Hdr3 Hdr4   Hdr5     Hdr6      Hdr7
----       ----     ---- ----   ----     ----      ----
$M00000000 $F000000 $J08 $V1000 $f000000 $m0000000 $n0000000
$M00000000 $F000000 $J08 $V1000 $f000000 $m0000000 $n0000000
$M00000000 $F000000 $J08 $V1000 $f000000 $m0000000 $n0000000
$M00000000 $F000000 $J08 $V1000 $f000000 $m0001274 $n0001274

Thanks so much I believe with your help it’s working as expected.