0

Hi I have a pipe | delimited file. I need to reverse all the numbers in there

The file looks like this

Entity|Division|Channel|200|300|800

I need to read the file an create a new one with reversed numbers

Entity|Division|Channel|-200|-300|-800

trying to make this work but, not entirely sure how to amend the text I'm getting. I need help on the :processToken procedure. How to output the tokens into a new file and add "-" and add back the delimiter |

for /f "tokens=* delims= " %%f in (M:\GPAR\Dev\EQ\Upload_Files\eq_test.txt) do (
set line=%%f
call :processToken
)
goto :eof

:processToken
for /f "tokens=* delims=|" %%a in ("%line%") do (
)
if not "%line%" == "" goto :processToken
goto :eof

Thanks

gemmo
  • 1,286
  • 2
  • 16
  • 33

2 Answers2

0
@echo off
set "ent_file=c:\entity.txt"
break>"%tmp%\rev.txt"
for /f "usebackq tokens=1,2,3,4,5,6 delims=|" %%a in ("%ent_file%") do (

    echo %%a^|%%b^|%%c^|-%%d^|-%%e^|-%%f

)>>"%tmp%\rev.txt"

move /y "%tmp%\rev.txt" "%ent_file%" 
del /q /f "%tmp%\rev.txt"

Should work if your file contains only lines like the one you've posted.

EDIT output the part after the 6th token:

@echo off
set "ent_file=c:\entity.txt"
break>"%tmp%\rev.txt"
for /f "usebackq tokens=1,2,3,4,5,6,* delims=|" %%a in ("%ent_file%") do (

    echo %%a^|%%b^|%%c^|-%%d^|-%%e^|-%%f|%%g

)>>"%tmp%\rev.txt"

move /y "%tmp%\rev.txt" "%ent_file%" 
del /q /f "%tmp%\rev.txt"
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • Thanks that works. I thought I was being smart and see if I can apply it to greater than 6 columns. What happens after 26/Z? I have 55 columns in total. – gemmo Sep 18 '14 at 23:34
  • @gemmo you can use 61 tokens ->http://ss64.com/nt/for_f.html - but you need to know the their sequence.Here are all of them `? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ `` a b c d e f g h i j k l m n o p q r s t u v w x y z {` Note the caret character ^ must be escaped with a second caret: ^^ – npocmaka Sep 18 '14 at 23:45
  • @gemmo - check this http://stackoverflow.com/questions/8520313/dos-batch-script-to-parse-csv-file-and-output-a-text-file/8520993#8520993 it turns out you cannot get more than 31 tokens. – npocmaka Sep 19 '14 at 20:57
  • @gemmo , if you want to process the string up-to the 6th token you can use `*` and assign the rest to `%%g` – npocmaka Sep 20 '14 at 14:53
0
@echo off
setlocal EnableDelayedExpansion

set digits=0123456789

(for /F "delims=" %%f in (eq_test.txt) do (
   set "input=%%f"
   set "output="
   call :processToken
   set /P "=!output:~1!" < NUL
   echo/
)) > new_file.txt
goto :EOF

:processToken
   for /F "tokens=1* delims=|" %%a in ("%input%") do (
      set "token=%%a"
      set "input=%%b"
   )
   if "!digits:%token:~0,1%=!" neq "%digits%" set "token=-%token%"
   set "output=%output%|%token%"
   if defined input goto processToken
exit /B
Aacini
  • 65,180
  • 12
  • 72
  • 108