0

I'm having trouble creating a batch file, which will correct some text files for me. So I need your advice. I need to get the data in field |No=xxx| in numerical format without any alphabetic characters , special character or spaces

I'm looking for a Batch code that can do this work, because I have a lot of files with this problem.

The text files (input.txt) are made this way:

|Job=1|Typ=S|Action=Order|No=531.04220440.00|
|Job=1|Typ=P|Action=Information|M=1|
|Job=2|Typ=S|Action=Order|No=ALR-2005605 |
|Job=2|Typ=P|Action=Information|M=3|
….
|Job=99|Typ=S|Action=Order|No=AF502033H|
|Job=99|Typ=P|Action=Information|M=1|

I was not able to understand JREPL by the way. I have to create files correting the input between |No=and | The File Example should look like that ,after the batch (output.txt)

|Job=1|Typ=S|Action=Order|No=5310422044000|
|Job=1|Typ=P|Action=Information|M=1|
|Job=2|Typ=S|Action=Order|No=2005605|
|Job=2|Typ=P|Action=Information|M=3|
….
|Job=99|Typ=S|Action=Order|No=502033|
|Job=99|Typ=P|Action=Information|M=1|

Thanks for your help.

Frank L
  • 3
  • 1
  • Please post some snippets of the approaches you have tried, so people can guide you to a solution. – betseyb Apr 08 '16 at 13:37
  • You should search some key word firstly. [link1](http://stackoverflow.com/questions/19855925/removing-non-alphanumeric-characters-in-a-batch-variable) [link2](http://stackoverflow.com/questions/22465485/how-to-remove-alphabets-and-special-characters-from-a-string-using-regex-in-wind) – enjoying Apr 08 '16 at 14:06

2 Answers2

0
@ECHO OFF
SETLOCAL
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "filename1=%sourcedir%\q36500938.txt"
SET "outfile=%destdir%\outfile.txt"
(
FOR /f "usebackqdelims=" %%a IN ("%filename1%") DO (
 ECHO "%%a"|FIND "|No=" >nul
 IF ERRORLEVEL 1 (ECHO(%%a
 ) ELSE (
  SET "line=%%a"
  CALL :process
 )
)
)>"%outfile%"

GOTO :EOF

:process
SET "tail="
:procloop
SET "line=%line:~0,-1%"
SET "char=%line:~-1%"
IF "%char%"=="=" (
 FOR %%x IN ("%line%%tail%|") DO ECHO(%%~x
 GOTO :EOF 
)
IF "%char%" geq "0" IF "%char%" leq "9" SET "tail=%char%%tail%"
GOTO procloop

You would need to change the settings of sourcedir and destdir to suit your circumstances.

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

Produces the file defined as %outfile%

It would appear, though it is not explicitly stated, that you wish to eliminate all non-numerics between |No= and | at the end of each line.

The above simply detects whether the line contains |No= and assumes that if it appears, it is in the last field. If it does not appear, simply regurgitate the line. If it does appear, process the line in line.

The processing simply lops off the last character and then determines whether the new last-character is = or not. If it is, assume that it is the = in No=, so add back the tail and closing | and output that. By putting the required string in quotes, you can use for to assign it to %%x and then use %%~x to remove the enclosing quotes- otherwise you'll get awkward syntax error to resolve.

If the last character is not = but is 0..9 then accumulate it else ignore it and continue processing until = is detected.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • It worked perfectly well on my first file, thank you @Magoo. Incredible , Thank you for your fast answer! – Frank L Apr 08 '16 at 15:52
0

Here is a simple JREPL.BAT solution

call jrepl "\D" "" /p "(\|No=)([\c|]+)(\|)$" /prepl "$1+{$2}+$3" /f input.txt /o output.txt
dbenham
  • 127,446
  • 28
  • 251
  • 390