Search a Powerhsell script and add a character at teh end of hte line RRS feed

  • Question

  • What command is used to search through a file, for a 5 digit number, and if it exists then to add let's say a 100 preceded by a coma at the end of the line this number appears in?
    • Moved by Bill_Stewart Tuesday, December 18, 2018 12:28 AM This is not "scripts on demand"
    Wednesday, October 10, 2018 6:23 PM

All replies

  • I will post a script shortly then.

    Wednesday, October 10, 2018 6:54 PM
  • This is the .csv file the script is looking into:

    5,A,01234,67891,Y,1253/2,1775478,,20181003,20181107,20181114,,1234567890000,OTHERCOMP ACCOUNTS PAYABLE,123 ROAD WAY,,FOREST,TX,01234-5678,,,,1234564870004,Another Warehouse, 300 Apt View Court NW,Specific Place,TX,12345-5678,,,018-441-5522,41245,DEFORPBC,,USD,N,,,,,,,,,,N,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,00987894564560,00456,152322,CANVAS,,,,123.00,US,0.00,US,0.00,US,10.5200,0.0000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
    5,A,01234,55555,Y,1254/2,1771234,,20181003,20181107,20181114,,1234567890000,OTHEROTHERCOMP ACCOUNTS PAYABLE,123 ROAD WAY,,JUNGLE,GA,01234-9999,,,,1234564870004,BAnother Warehouse, 400 Sea View Court NW,AnotherSpecific Place,GA,12345-9999,,,018-441-5522,41245,GHIORPBC,,USD,N,,,,,,,,,,N,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,00987894564560,00456,152322,CANVAS,,,,123.00,US,0.00,US,0.00,US,10.5200,0.0000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

    Modify the PS Script below, sot that depending on what it sees in position 4, it will add a field.
    For example:
    If it sees 12345, it will add a ",100"
    If it sees 67891, it will add a ".500"
    (without the quotations).

    $inFolder = '\\saved\data'
    $outSuffix = '_rev'
    $replace_67891 = @{
    	'SPECIFIC PLACE' = '01-US'
    $referenceCount = 0
    $row = 0
    Get-ChildItem -Path $inFolder -Filter *.csv | Where-Object {$_.BaseName -notmatch "$($outSuffix)\Z"} | ForEach-Object {
    	$outFile = Join-Path -Path $_.DirectoryName -ChildPath "$($_.BaseName)$($outSuffix)$($_.Extension)"
    	$saveFile = $false
    	Write-Host "Processing '$($_.Name)' ..."
    	$content = Get-Content -Path $_.FullName | ForEach-Object {
    		$fieldChanged = $false
    		$fields = $_.Split(',')
    		If ($referenceCount -eq 0) {$referenceCount = $fields.Count}
    		If ($fields.Count -ne $referenceCount) {
    			Throw "Field count in line $($row) is incorrect (is: $($fields.Count), expected: $($referenceCount))!"
    		$old3 = $fields[3]
    		If ($fields[3] -eq '67891') {
    			If ($replace_67891.ContainsKey($fields[26])) {
    				$fields[3] = $replace_67891[$fields[26]]
    				$fieldChanged = $true
    		If ($fieldChanged) {
    			Write-Host "    line $($row): $($old3) [$($fields[26])]) --> $($fields[3])"
    			$saveFile = $true
    		$fields -join ','
    	If ($saveFile) {
    		$content | Set-Content -Path $outFile
    		Write-Host "Changes written to $($outFile)."
    	} Else {
    		Write-Host "No changes found."

    Wednesday, October 10, 2018 6:57 PM
  • We do not fix or modify scripts on request.  Please contact a consultant to fix you script for you.


    Wednesday, October 10, 2018 7:18 PM
  • As jrv says, however I will offer some pointers.

    I am assuming from your code that you have a series of csv (, delimited). Also I think I can reasonably assume that the columns of the csv files are ordered. If you have headers then you just use import-csv, otherwise you can use 

    $Header = @"

    "Firstcolumn","secondcolumn","theColumnYouCareAbout","lots more columns"


    $Data = ($Header + (Get-content -path "Your path") ) | ConvertFrom-Csv

    Which will get you everything in a nice object orientated structure.

    As to your actual question I don't know what you mean by "possition 4" but in general I think you are looking for a switch.

    If you are using PowerShell_ISE then try hitting ctrl+j and selecting the switch command.

    Friday, October 12, 2018 5:48 AM