0

My problem is can't split the tilde I/p file: sample data: only has 5 cols where 2nd col value is blank but delims using tilde operator is showing 2nd field value is 3.

Input file:

1~~3~4~5

my actual output is

field 1: 1
field 2: 3
field 3: 4
field 4: 5
field 5:

my expected output should be

field 1: 1
field 2: 
field 3: 3
field 4: 4
field 5: 5

my expected output should be field 2 output should be blank. Below code demilits comma. I want similar to do for tilde symbol(~)

Here is my code:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "usebackq delims=" %%# in (F:\batch\input.txt) do (
pause
    set "LINE=%%#"
    echo Line is:%%#
    setlocal EnableDelayedExpansion
    for /F "tokens=1-5 delims=," %%A in (^""!LINE:,="^,"!"^") do (
        endlocal
    echo Field 1: %%~A
    echo Field 2: %%~B
    echo Field 3: %%~C
    echo Field 4: %%~D
    echo Field 5: %%~E
        setlocal EnableDelayedExpansion
    )
    endlocal
)
pause
endlocal

After reading that post in the comment still I am not able to get the exact output. Can you please advise ?

Kingsters
  • 47
  • 1
  • 3
  • 8
  • 1
    Please post genuine representative sample data – Magoo Sep 27 '17 at 05:43
  • @Magoo Input file is same as i shown in body of the text and also explained. pls check. Thanks for looking.It is just 1~~3 ~4~5 – Kingsters Sep 27 '17 at 05:59
  • 1
    Possible duplicate of [How to escape "~" in set/replace command?](https://stackoverflow.com/questions/35245802/how-to-escape-in-set-replace-command) – JosefZ Sep 27 '17 at 07:30
  • can you please explain with my input file and code ? I am not getting the exact output. – Kingsters Sep 27 '17 at 08:56
  • Yes. The For command will treat two consecutive delimiters as one. I like to use a helper batch file that Dbenham wrote to solve this issue. http://www.dostips.com/forum/viewtopic.php?t=5702 – Squashman Sep 27 '17 at 15:38

1 Answers1

1
@ECHO Off
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q46440111.txt"

FOR /f "usebackqdelims=" %%A IN ("%filename1%") DO (
 SET "line=%%A"
 CALL :replace~ line ":"
 FOR /f "tokens=1-5delims=:" %%a IN ('call echo "%%line%%"' ) DO (
  echo Field 1: %%~a
  echo Field 2: %%~b
  echo Field 3: %%~c
  echo Field 4: %%~d
  echo Field 5: %%~e
 )
)


GOTO :EOF

:replace~
CALL SET "$=%%%1%%"
SET "%1="
:replace~l
IF NOT DEFINED $ GOTO :EOF 
IF "%$:~0,1%"=="~" (CALL SET "%1=%%%1%%%2") ELSE (CALL SET "%1=%%%1%%%$:~0,1%")
SET "$=%$:~1%"
GOTO replace~l

You would need to change the setting of sourcedir to suit your circumstances.

I used a file named q46440111.txt containing your data for my testing.

Since you appear to want to operate with delayedexpansion disabled, I've not invoked it.

The line read from the file is assigned to line and this variable is processed by the routine replace~ to mechanically replace each ~ with the second argument supplied to the routine.

The result is then parsed by the second for and the individual elements reported.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Thanks a lot Magoo.. It worked perfectly. The only thing is it is taking 20 seconds to process 5 records. I have to process 1 million records. Is there anyway to reduce the execution time? – Kingsters Sep 28 '17 at 03:25
  • Batch is notoriously slow and was never designed to tackle a problem of such dimension. I would use 3rd-party utilities like `sed` or `(g)awk` or write a program in a high-level language. – Magoo Sep 28 '17 at 04:05
  • If i have the input file like this 1~2~test) ~4 ~5 it got exited due to close paranthesis. Any idea? – Kingsters Sep 28 '17 at 06:53
  • There are many symbols that have special meaning for `cmd`, but no universal method to smoothly process all of them. This is why I requested a representative data sample. Each "poison" character has its own work-around method, and using workarounds will add significantly to the processing time - which you've already said is excessive. Batch is not a suitable language for general string-processing and I believe you should investigate `sed` or `(g)awk` to perform your task. – Magoo Sep 28 '17 at 08:13