# Get data from string and output

**URL:** <https://forums.powershell.org/t/get-data-from-string-and-output/15356>\
**Category:** PowerShell Help\
**Created:** [December 8, 2020, 2:50am UTC](https://forums.powershell.org/t/get-data-from-string-and-output/15356 "2020-12-08T02:50:31Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![andyh](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@andyh](https://forums.powershell.org/u/andyh)\
**Post date:** [December 8, 2020, 2:50am UTC](https://forums.powershell.org/t/get-data-from-string-and-output/15356/1 "2020-12-08T02:50:31Z")

</div>

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,$X **0000001240** ,$U0000

I want to get the **0000001240** value in a variable.

Thanks.

&nbsp;

---

<div class="post-metadata">

**Author:** ![adminofthings](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@adminofthings](https://forums.powershell.org/u/adminofthings)\
**Post date:** [December 8, 2020, 6:44am UTC](https://forums.powershell.org/t/get-data-from-string-and-output/15356/2 "2020-12-08T06:44:27Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![andyh](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@andyh](https://forums.powershell.org/u/andyh)\
**Post date:** [December 8, 2020, 7:12am UTC](https://forums.powershell.org/t/get-data-from-string-and-output/15356/3 "2020-12-08T07:12:14Z")

</div>

Could it be checked by line?

foreach ($linelvl in $lines)  
…

&nbsp;

Thx.

&nbsp;

---

<div class="post-metadata">

**Author:** ![adminofthings](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@adminofthings](https://forums.powershell.org/u/adminofthings)\
**Post date:** [December 8, 2020, 7:41am UTC](https://forums.powershell.org/t/get-data-from-string-and-output/15356/4 "2020-12-08T07:41:16Z")

</div>

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
}
```

---

<div class="post-metadata">

**Author:** ![andyh](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@andyh](https://forums.powershell.org/u/andyh)\
**Post date:** [December 8, 2020, 7:54am UTC](https://forums.powershell.org/t/get-data-from-string-and-output/15356/5 "2020-12-08T07:54:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![andyh](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@andyh](https://forums.powershell.org/u/andyh)\
**Post date:** [December 8, 2020, 8:13am UTC](https://forums.powershell.org/t/get-data-from-string-and-output/15356/6 "2020-12-08T08:13:58Z")

</div>

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
	}
}
```

&nbsp;

---

<div class="post-metadata">

**Author:** ![andyh](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@andyh](https://forums.powershell.org/u/andyh)\
**Post date:** [December 8, 2020, 8:17am UTC](https://forums.powershell.org/t/get-data-from-string-and-output/15356/7 "2020-12-08T08:17:57Z")

</div>

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,$X **0000001240** ,$U0000

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [December 8, 2020, 9:07am UTC](https://forums.powershell.org/t/get-data-from-string-and-output/15356/8 "2020-12-08T09:07:29Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![andyh](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@andyh](https://forums.powershell.org/u/andyh)\
**Post date:** [December 8, 2020, 10:07am UTC](https://forums.powershell.org/t/get-data-from-string-and-output/15356/9 "2020-12-08T10:07:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:28pm UTC](https://forums.powershell.org/t/get-data-from-string-and-output/15356/10 "2024-05-16T20:28:13Z")

</div>


