Solved Every two - - substitute ; for space

June 2, 2017 at 01:23:47
Specs: Windows 10
Hello,

I have been given this txt file ej:
-state;of;product-;-201705-;-ID1-;-SOLD-;-4-;-PROD;1-;;
and i need to change it to:
-state of product-;-201705-;-ID1-;-SOLD-;-4-;-PROD 1-;;

As you can see every two - i need to change ; with an space.

┬┐Can you help me?


See More: Every two - - substitute ; for space

Reply ↓  Report •

#1
June 2, 2017 at 02:52:22
Something like this?

I didn't quite understand what you meant, so I assumed you meant between every set of -'s

@echo off
setlocal enabledelayedexpansion

:: Change this string to whatever you want to process
set input=-state;of;product-;-201705-;-ID1-;-SOLD-;-4-;-PROD;1-;

set _input=!input!
set substitute=0
set output=

:process
if not defined _input (
	set output=!output:--=-;-!;;
	echo !output!
	pause > nul
	exit
)
set "char=!_input:~0,1!"
set "_input=!_input:~1!"
if "!char!"=="-" (
	if !substitute!==0 (
		set substitute=1
	) else (
		set substitute=0
	)
	set output=!output!!char!
	set "char=!_input:~0,1!
	set "_input=!_input:~1!"
)
if "!char!"==";" (
	if !substitute!==1 (
		set "output=!output! "
	)
) else (
	set output=!output!!char!
)
goto :process

Don't worry if plan A fails, there are 25 more letters in the alphabet ;)

message edited by RainBawZ


Reply ↓  Report •

#2
June 2, 2017 at 06:39:32
✔ Best Answer
If you're using Win10, you should probably be looking for a PowerShell script, since batch's depreciation is a concern.
gc someFile.txt | 
 ForEach-Object {
  $_.ToCharArray() | 
  ForEach-Object -Begin { $out = New-Object Text.StringBuilder; $inField = $false } {
    $inField = $inField -xor $_ -eq '-'
    if ($inField -and $_ -eq ';') { $_ = ' ' }
    $out.Append($_) | Out-Null
  } -End { $out.ToString() }
} > newFile.txt

How To Ask Questions The Smart Way

message edited by Razor2.3


Reply ↓  Report •
Related Solutions


Ask Question