Question

Ok guys. I am new to stack. searched for an answer but couldn't find one. My txt file

CUMBS02778607   DVD/SLS34142072,Z5
CUMBS02778844   SHIP/25/02/14/HR/15,Z5
CUMBS02780058   DVD/SLS34142200,ISSUE 9,Z5
CUMBS02779867   SHIP/27/02/14/CAM/49,Z5
CUMBS02779883   SHIP/27/02/14/CAM/48,Z5
CUMBS02779816   SHIP/27/02/14/CAM/36,Z7
CUMBS02779948   SHIP/27/02/14/CAM/46,Z5
CUMBS02780309   DVD/SLS34142262,Z5
CUMBS02780218   DVD/SLS34142231,Z5
CUMBS02780406   DVD/SLS34142266-267,Z5
CUMBS02779956   SHIP/27/02/14/CAM/50,Z5
CUMBS02779743   DVD/SLS34142181-182,Z5
CUMBS02779611   SHIP/27/02/14/PRO/02,Z4
CUMBS02780155   DVD/SLS34142191-193,Z5
CUMBS02777848   ORD.7737623,REF.62664,SLS34141962,Z7

contains the following line:

CUMBS02778607   DVD/SLS34142072,Z5

My Script Running in batch is as follows. Search for .....SLS and replace it with just SLS

@echo off &setlocal
set "search=*SLS"
set "replace=SLS"
set "textfile=new.txt"
set "newfile=output.txt"

(for /f "delims=" %%i in ('findstr /n "^" "%textfile%"') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:%search%=%replace%!"
    echo(!line!
    endlocal
))>"%newfile%"
type "%newfile%"

The problem is file returns and throws away the CUMBS******** Number. I need to keep that is a variable and put it back in the start of the string. This removes any junk before the characters SLS. Lines Guys any help would be appreciated.

Example of text returned. lines afterwards starting like 2: , 5: etc and not starting with CUMB****** I will delete.

SLS34142072,Z5
2:CUMBS02778844 SHIP/25/02/14/HR/15,Z5
SLS34142200,ISSUE 9,Z5
4:CUMBS02779867 SHIP/27/02/14/CAM/49,Z5
5:CUMBS02779883 SHIP/27/02/14/CAM/48,Z5
6:CUMBS02779816 SHIP/27/02/14/CAM/36,Z7
7:CUMBS02779948 SHIP/27/02/14/CAM/46,Z5
SLS34142262,Z5
SLS34142231,Z5
SLS34142266-267,Z5
11:CUMBS02779956    SHIP/27/02/14/CAM/50,Z5
SLS34142181-182,Z5
13:CUMBS02779611    SHIP/27/02/14/PRO/02,Z4
SLS34142191-193,Z5
SLS34141962,Z7
Was it helpful?

Solution

@echo off &setlocal
set "search=*SLS"
set "replace=SLS"
set "textfile=new.txt"
set "newfile=output.txt"

(for /f "usebackq tokens=1,*" %%a in ("%textfile%") do (
    set "line=%%b"
    setlocal enabledelayedexpansion
    set "line=!line:%search%=%replace%!"
    echo(%%a !line!
    endlocal
))>"%newfile%"
type "%newfile%"

You have it almost done.

Using *SLS is asking to delete from start of line up to SLS, so it deletes the CUMBS* field. Instead, split the input record on the tab, keeping the first field and doing the replacemente only on the second one.

The numbering comes from findstr /n As it is not needed, removed

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top