Reputation: 487
In my opac_one-hit*.log I have 3 columns with numbers which are separated with ; I want to find and rename the duplicated numbers in the third column and make the repeated numbers look like number_1... number_2
@ECHO OFF
SETLOCAL enabledelayedexpansion
for /r %%# in ("opac_one-hit*.log") do (
FOR /F "usebackq skip=3 tokens=1,3 delims=;" %%G IN ( "%%~f#" ) DO ( echo %%H >> liste.txt
)
)
Sample from opac_one-hit*.log:
"F96B1606";"216618711"; "BV499630491";
"F96B1607";"216618878"; "BV499630823";
"F96B1661";"216653304"; "BV49843883X";
"F96B1690";"216796148"; "BV49843883X";
Result:
the duplicated number in the third column should look like this "BV49843883X_1"
If there are two duplicates then BV49843883X_1
and BV49843883X_2
Upvotes: 1
Views: 162
Reputation: 67206
You did not specified the desired output. This Batch file is a starting point:
@echo off
setlocal EnableDelayedExpansion
for %%# in ("opac_one-hit*.log") do (
for /F "usebackq tokens=3 delims=; " %%G in ("%%~f#") do (
echo "%%~G!N3[%%~G]!"
set /A "C3[%%~G]+=1"
set "N3[%%~G]=_!C3[%%~G]!"
)
)
This is the output:
"BV499630491"
"BV499630823"
"BV49843883X"
"BV49843883X_1"
Upvotes: 1
Reputation: 79982
Sadly you haven't shown exactly what your output should be, so I'll have to leave it to you to complete the task.
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
:: remove variables starting @
FOR /F "delims==" %%a In ('set @ 2^>Nul') DO SET "%%a="
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "filename1=%sourcedir%\q49821253.txt"
SET "outfile=%destdir%\outfile.txt"
REM for /r %%a in ("%filename1%") do (
for %%a in ("%filename1%") do (
FOR /F "usebackq tokens=1,3 delims=;" %%G IN ( "%%~fa" ) DO (
CALL :bumpcount %%H
)
)
GOTO :EOF
:bumpcount
SET "col3=%~1"
(
IF DEFINED @%col3% (
SET /a @%col3%+=1
ECHO "%col3%_!@%col3%!"
) ELSE (
echo %1
SET /a @%col3%=0
)
)>> "%outfile%"
GOTO :eof
Note that I've changed the filenames to suit my system, removed the skip
from the file-analyser and replaced the for/r
with a plain vanilla for
for testing.
The for /f
to remove variables starting @
needs to be moved to the line before the for...%%G...
if you want to restart the numbering at the start of each new file. Don't use ::-comments
within a loop, use REM
comments instead.
First, clear all @
variables. then, on each line, pass column3's contents to a subprocedure called :bumpcount
:bumpcount
sets the variable col3
to the value of column3, removing the enclosing quotes. If the variable @
+column3 is defined, then we have encountered this value in the past, so increment it and append the new count-of-repeats to the contents of column3; otherwise, just output the value as suplied and set %
+column3 to 0
, indicating that it's been seen before and starting the count for that particular value's suffix.
Note the (...)
surrounding the if defined
statement. This gathers all output to the redirected destination.
Since you didn't explicitly show the desired output, I simply reproduced column3 without the terminal ;
as I assume it appeared from your original code.
Upvotes: 0