Search string Copy numbers at end

by vanhoja at 2013-03-07 14:01:54

Hello,

I am trying to create a script that will read a particular file that starts with SO and contains text lets say something like SO784314.eif. I am wanting to search the file for a particular line HD_LOAD_ID_NUMBER=123456 where the number length can very. I am needing to take the number and add to another line HD_LOAD_ID_NUMBER= and add a -2 to the end of the numbers so something like this HD_LOAD_ID_NUMBER=123456-2. Is there a way I can do this.
by vanhoja at 2013-03-07 14:03:06
This is what I have so far:
$Content = Get-Content c:\test\SO*.eif | Select-String "LMR" -Quiet

if ($Content -eq ‘True’){
echo test
$ConfigFiles=Get-ChildItem c:\test\SO*.eif -Recurse
foreach ($file in $ConfigFiles)
{
(Get-Content $file.PSPath) |

ForEach-Object {
$_ -replace "HD_LOAD_ID_NUMBER=", "HD_LOAD_ID_NUMBER=Test"} |
Set-Content $file.PSPath
}

}
by vanhoja at 2013-03-07 20:37:12
Ok code update I have it searching, finding the numbers but when it goes to add them to the other line it adds the numbers for each file such as 1234 4859 12312-3 code below:
$Location = "c:\test\SO*.eif"
$SearchStr = "LMR"
$LoadID = "PRFX_DOC_NBR="
$DocID = "HD_LOAD_ID_NUMBER="


$Sel = Select-String -Pattern $SearchStr -Path $Location
if($Sel -eq $null)
{
write-host "$Location does not contain $SearchStr"
}
Else
{

$Num = Select-String -Pattern ‘(?<=^PRFX_DOC_NBR=)\d+’ -Path $Location |
ForEach-Object {$.Matches[0].Value}
write-host ($LoadID+$Num)

$NumIn = Select-String -Pattern ‘(?<=^HD_LOAD_ID_NUMBER=)\d+’ -Path $Location |
ForEach-Object {$
.Matches[0].Value}

$FileEdt = ("$DocID"+$Num +"-2")

$ConfigFiles=Get-ChildItem $Location -Recurse
foreach ($Sel in $ConfigFiles)
{
(Get-Content $Sel.PSPath) |

ForEach-Object {
$_ -replace "$DocID", "$FileEdt"} |
Set-Content $Sel.PSPath
}
}
by mjolinor at 2013-03-08 06:26:56
Your code is a little hard to follow not knowing all the details of your file system, but based on your original description of the objective I came up with this using some test data:

$file = 'c:\testfiles\SO784314.eif'
(get-content $file) |
foreach {
[regex]::Replace($,'(HD_LOAD_ID_NUMBER=\d+)',{"$argsn$args-2&quot;}&#41;<br> }</code></blockquote>by DonJ at 2013-03-08 06:30:29<blockquote>Vanhoja, please consider using the CODE button to format your code - that'll help make it easier for folks to read. There are additional tips at the top of this screen.</blockquote>by vanhoja at 2013-03-08 06:53:36<blockquote>im sorry, will try and do better. for a test file the data in it will be(actual file will have more):<br>LMR<br>PRFX_DOC_NBR=63190<br>HD_LOAD_ID_NUMBER=<br><br>What the code I am trying to write will check the files for any containing LMR and then get the PRFX_Doc number, in this case 63190. Then it will add the number along with -2 (63190-2) to the end of the HD_Load line. Right now the code I wrote reads all the files and gets all the doc numbers place the numbers together with a -2 at the end (HD_LOAD_ID_NUMBER= 9123 45698 63190-2) and does this to all 3 test files even though one does not have LMR. Reposting code using code button:<br><br><code>$Location = &quot;c:\test\SO*.eif&quot;<br>$SearchStr = &quot;LMR&quot;<br>$LoadID = &quot;PRFX_DOC_NBR=&quot;<br>$DocID = &quot;HD_LOAD_ID_NUMBER=&quot;<br><br><br>$Sel = Select-String -Pattern $SearchStr -Path $Location<br>if&#40;$Sel -eq $null&#41;<br>{<br>write-host &quot;$Location does not contain $SearchStr&quot;<br>}<br>Else<br>{<br><br>$Num = Select-String -Pattern &#39;&#40;?&lt;=^PRFX_DOC_NBR=&#41;\d+&#39; -Path $Location |<br>ForEach-Object {$_.Matches[0].Value}<br>write-host &#40;$LoadID+$Num&#41;<br><br>$NumIn = Select-String -Pattern &#39;&#40;?&lt;=^HD_LOAD_ID_NUMBER=&#41;\d+&#39; -Path $Location |<br>ForEach-Object {$_.Matches[0].Value}<br><br>$FileEdt = &#40;&quot;$DocID&quot;+$Num +&quot;-2&quot;&#41;<br><br>$ConfigFiles=Get-ChildItem $Location -Recurse<br>foreach &#40;$Sel in $ConfigFiles&#41;<br>{<br>&#40;Get-Content $Sel.PSPath&#41; |<br><br>ForEach-Object {<br>$_ -replace &quot;$DocID&quot;, &quot;$FileEdt&quot;} |<br>Set-Content $Sel.PSPath<br>}<br>}</code></blockquote>by mjolinor at 2013-03-08 06:58:37<blockquote>Okay, that's a different scenario than I first undertood the problem to be. Let me work on that for a bit.</blockquote>by mjolinor at 2013-03-08 08:09:54<blockquote>Using this:<br><code>$SearchStr = &quot;LMR&quot;<br>$LoadID = &quot;PRFX_DOC_NBR=&quot;<br>$DocID = &quot;HD_LOAD_ID_NUMBER=&quot;<br><br><br>$text = @&#39;<br>some stuff<br>LMR<br>PRFX_DOC_NBR=63190<br>HD_LOAD_ID_NUMBER=<br>some other stuff<br>LMR<br>PRFX_DOC_NBR=63191<br>HD_LOAD_ID_NUMBER=<br>still more stuff<br>&#39;@<br><br>$file = &#39;c:\testfiles\SO784314.eif&#39;<br><br>$text | set-content $file<br><br><br>$regex = @&quot;<br>&#40;?ms&#41;$SearchStr<br>$LoadID&#40;\d+&#41;<br>$DocID<br>&quot;@<br><br><br>&#40;[IO.File]::ReadAllText&#40;$file&#41;&#41; -replace $regex,&#39;$0$1-2&#39;</code><br><br>I get this:<br><br>some stuff<br>LMR<br>PRFX_DOC_NBR=63190<br>HD_LOAD_ID_NUMBER=63190-2<br>some other stuff<br>LMR<br>PRFX_DOC_NBR=63191<br>HD_LOAD_ID_NUMBER=63191-2<br>still more stuff</blockquote>by vanhoja at 2013-03-08 08:58:08<blockquote>If I am reading it right you are hard coding in the code below and then it changes in the file. The problem is the document number will change each time so it can not be hard coded and there can be multiple .eif files in folder location.<br><code>$text = @&#39;<br>some stuff<br>LMR<br>PRFX_DOC_NBR=63190<br>HD_LOAD_ID_NUMBER=<br>some other stuff<br>LMR<br>PRFX_DOC_NBR=63191<br>HD_LOAD_ID_NUMBER=<br>still more stuff<br>&#39;@</code></blockquote>by mjolinor at 2013-03-08 09:25:50<blockquote>All that's doing is creating a sample data file to test the script. Replace that sample data file with one of your production files and see if you get the expected results.<br><br>Use just this much, and replace the path in $file with the path to one of your files:<br><br><code>$file = &#39;c:\testfiles\SO784314.eif&#39;<br><br>$SearchStr = &quot;LMR&quot;<br>$LoadID = &quot;PRFX_DOC_NBR=&quot;<br>$DocID = &quot;HD_LOAD_ID_NUMBER=&quot;<br><br>$regex = @&quot;<br>&#40;?ms&#41;$SearchStr<br>$LoadID&#40;\d+&#41;<br>$DocID<br>&quot;@<br><br>&#40;[IO.File]::ReadAllText&#40;$file&#41;&#41; -replace $regex,&#39;$0$1-2&#39;</code></blockquote>by vanhoja at 2013-03-08 10:09:40<blockquote>the file displays the data correctly in console if it only has those 3 lines but does not update actual file. Now when I have it point to an actual production file it does not work.</blockquote>by mjolinor at 2013-03-08 10:33:01<blockquote>It wasn't supposed to update the file. I'm just trying to produce a method that produces the correct data. We can worry about where it gets written to after we get it doing that much right.<br><br>If it works with the sample data, but does not work with a production file, then then the sample data is not representative of the production data. Not having the prodcution files, I cannot say what's different. Only you can do that.</blockquote>by vanhoja at 2013-03-08 11:00:43<blockquote>ok, was just thrown off since it did edit the file in the earlier edition. The doc number and load id is the same format in file but can show up in different locations. LMR is only at end of string and the begging can be different.</blockquote>by mjolinor at 2013-03-08 11:06:24<blockquote>Can you post sample data from the producion file that could be used for testing?</blockquote>by vanhoja at 2013-03-08 11:12:35<blockquote>here is part of the file I just copied the part that had all 3 variables the file goes longer and it does not always appear in same line:<br><br>RECORD_TYPE=PRFX<br>PRFX_CO_ID=NS<br>PRFX_EXT_PARTNER=NS<br>PRFX_DOC_TYPE=Shipment<br>PRFX_DOC_NBR=53190<br>PRFX_TRANS_DIR=I<br>PRFX_DATE=2013-03-06<br>PRFX_TIME=08:37:28<br>PRFX_CONTROL_NBR=NSP-63190-G0RF<br>RECORD_TYPE=SO_ID<br>HD_USER_ID=username<br>HD_SO_SHPCMP_ID=NS<br>HD_SO_SHPWHS_ID=LMR<br>HD_LOAD_ID_NUMBER=</blockquote>by mjolinor at 2013-03-09 20:19:37<blockquote>What do you want the result from the script to look like, given that input?</blockquote>by vanhoja at 2013-03-09 20:29:54<blockquote>here is how the output should look like after updating the file, it needs to only do it to the files with LMR in it, basically it needs to take PRFX_DOC_NBR number append it to HD_LOAD_ID_NUMBER with a -2 and to add 01 to PRFX_CONTROL_NBR=NSP-63190-G0RF:<br>RECORD_TYPE=PRFX<br>PRFX_CO_ID=NS<br>PRFX_EXT_PARTNER=NS<br>PRFX_DOC_TYPE=Shipment<br>PRFX_DOC_NBR=53190<br>PRFX_TRANS_DIR=I<br>PRFX_DATE=2013-03-06<br>PRFX_TIME=08:37:28<br>PRFX_CONTROL_NBR=01NSP-63190-G0RF<br>RECORD_TYPE=SO_ID<br>HD_USER_ID=username<br>HD_SO_SHPCMP_ID=NS<br>HD_SO_SHPWHS_ID=LMR<br>HD_LOAD_ID_NUMBER=53190-2</blockquote>by mjolinor at 2013-03-09 21:58:45<blockquote>Okay, is this any closer?<br><br><code>foreach &#40;$file in &#40;Get-ChildItem c:\test\SO*.eif | select -ExpandProperty fullname&#41;&#41;<br> {<br> if &#40;Select-String -Path $file -Pattern &quot;LMR&quot; -Quiet&#41;<br> {<br> &#40; Get-Content $file&#41; |<br> foreach {<br><br> switch -Regex &#40;$_&#41;<br> { <br> &#39;PRFX_DOC_NBR=\d+&#39; {$PrfDocNbr = $_ -replace &#39;^\D+&#40;\d+&#41;&#39;,&#39;$1&#39;;$_}<br><br> &#39;HD_LOAD_ID_NUMBER=&#39; {&quot;$_$PrfDocNbr-2&quot;}<br><br> Default {$_}<br> }<br> <br><br> } | Set-Content $file<br> } <br> }</code><br><br>Edit - removed an extraneous pipe.</blockquote>by vanhoja at 2013-03-10 20:38:55<blockquote>Well when I try and run it I get the below errors, I have reboot just to make sure nothing had them open on computer.<br><br>Set-Content : The process cannot access the file 'C:\test\SO64201.eif' because it is being used by another process.<br>At C:\scripts\PSOtest.ps1:18 char:24<br>+ } | Set-Content &lt;&lt;&lt;&lt; $file<br> + CategoryInfo : NotSpecified: (:) [Set-Content], IOException<br> + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.SetContentCommand<br> <br>Set-Content : The process cannot access the file 'C:\test\SOtestNO.eif' because it is being used by another process.<br>At C:\scripts\PSOtest.ps1:18 char:24<br>+ } | Set-Content &lt;&lt;&lt;&lt; $file<br> + CategoryInfo : NotSpecified: (:) [Set-Content], IOException<br> + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.SetContentCommand</blockquote>by mjolinor at 2013-03-10 20:44:41<blockquote>I made and edit to the script. See if that stops the errors.</blockquote>by vanhoja at 2013-03-10 20:46:46<blockquote>lol I had made that change myself after a quick search, it does work now. Just got to figure out/add updating the line from PRFX_CONTROL_NBR=NSP-63190-G0RF to PRFX_CONTROL_NBR=01NSP-63190-G0RF</blockquote>by MasterOfTheHat at 2013-03-11 13:21:25<blockquote>I took mjolinar's script and modified it to add your &quot;01&quot; to the PRFX_CONTROL_NBR, protect against adding a newline after the last line of the file (<a href="http&#58;//blogs&#46;msdn&#46;com/b/jmanning/archive/2007/05/23/powershell-gotcha-of-the-day-set-content-adds-newlines-by-default&#46;aspx">Set-Content does this by design</a>), and get rid of the regex stuff (just because I don't like it and the .NET substring function is easier for me to understand). Also added break statements in the switch and tweaked the syntax to use ForEach-Object instead of the .NET foreach. <br>[code2=powershell]Get-ChildItem c:\temp\SO*.eif | Select-Object -ExpandProperty fullname | ForEach-Object { <br> if &#40;Select-String -Path $_ -Pattern &quot;LMR&quot; -Quiet&#41; <br> { <br> $str = &quot;&quot;<br> &#40; Get-Content $_ &#41; |<br> ForEach-Object {<br> if &#40;$str -ne &quot;&quot;&#41;<br> {<br> $str += &quot;n"
}
switch -Regex ($
)
{
'PRFX_DOC_NBR=\d+' {
$PrfDocNbr = $.substring($.IndexOf("=")+1)
$str += $
break;
}
'PRFX_CONTROL_NBR=*' {
$str += "PRFX_CONTROL_NBR=01$($
.substring($.IndexOf("=")+1))"
}
'HD_LOAD_ID_NUMBER=' {
$str += "HD_LOAD_ID_NUMBER=$PrfDocNbr-2"
break;
}
Default {
$str += $

break;
}
}
}
[io.file]::WriteAllText($, $str)
}
}[/code2]

It turns this file:
RECORD_TYPE=PRFX
PRFX_CO_ID=NS
PRFX_EXT_PARTNER=NS
PRFX_DOC_TYPE=Shipment
PRFX_DOC_NBR=53190
PRFX_TRANS_DIR=I
PRFX_DATE=2013-03-06
PRFX_TIME=08:37:28
PRFX_CONTROL_NBR=NSP-63190-G0RF
RECORD_TYPE=SO_ID
HD_USER_ID=username
HD_SO_SHPCMP_ID=NS
HD_SO_SHPWHS_ID=LMR
HD_LOAD_ID_NUMBER=


into this one:
RECORD_TYPE=PRFX
PRFX_CO_ID=NS
PRFX_EXT_PARTNER=NS
PRFX_DOC_TYPE=Shipment
PRFX_DOC_NBR=53190
PRFX_TRANS_DIR=I
PRFX_DATE=2013-03-06
PRFX_TIME=08:37:28
PRFX_CONTROL_NBR=01NSP-63190-G0RF
RECORD_TYPE=SO_ID
HD_USER_ID=username
HD_SO_SHPCMP_ID=NS
HD_SO_SHPWHS_ID=LMR
HD_LOAD_ID_NUMBER=53190-2
by mjolinor at 2013-03-11 13:47:10
If you’e going to "get rid of the regex" you should probably change the switch type to -Wildcard, and use an * instead of (/d+) in the first test.

As it stands, you’re using a Regex switch with a mixture of wildcard and regex test patterns.
by vanhoja at 2013-03-11 13:48:46
That worked perfectly thank you both, being so new to powershell it was driving me crazy.
by mjolinor at 2013-03-11 13:50:31
Do you understand it well enough to reproduce it for different data next time?
by vanhoja at 2013-03-11 14:01:41
I can read through it and see what it does for the most part but since I don’t know it all yet parts I don’t understand but know what the output does.
by mjolinor at 2013-03-11 14:13:26
I’d advise going through that and finding / reading the full help or about on anyting you don’t understand.

Come back and start a new thread with questions about anything you can’t find the help on, or don’t understand after reading the help.
by vanhoja at 2013-03-11 14:17:44
ok, thanks, I have all ready printed off the cheat shits provided in the forum but will run the help command to get some better understanding.
by MasterOfTheHat at 2013-03-11 14:20:11
[quote="mjolinor"]If you’e going to "get rid of the regex" you should probably change the switch type to -Wildcard, and use an * instead of (/d+) in the first test.

As it stands, you’re using a Regex switch with a mixture of wildcard and regex test patterns.[/quote]
Gah! Nice catch, mjolinor. That’s what I get for copying and pasting.

And he’s absolutely right. Read the help texts and ask questions!