Cut text after nth character in a string using batch script

April 8, 2019 at 08:28:48
Specs: DOS
I have file like below

ai,one,qew
bet,two,erfv
cars,three,eie

and I need output like below

ai,one
bet,two
cars,three

I need windows script to do this. Here the should be deleted based on 2nd comma


See More: Cut text after nth character in a string using batch script

Reply ↓  Report •

#1
April 8, 2019 at 08:36:46
input.txt contains:
ai,one,qew
bet,two,erfv
cars,three,eie

this is test.bat, run it
@echo off
echo off>output.txt
for /f "tokens=1,2 delims=," %%a in (input.txt) do echo %%a,%%b>>output.txt

output.txt contains:
ai,one
bet,two
cars,three

i5-6600K[delid]@4.8GHz/4.4GHz cache/@1.440v | 2x4GB Crucial-DDR4-2400CL17@CL12-15-15-28 1T 3000MHz@1.45v | Sapphire Nitro+ SE RX 590 8GB@1525Mhz core@1.0v/2250MHz@1500MHz timings BiosModded


Reply ↓  Report •

#2
April 29, 2019 at 03:00:54
I've a file with not fixed length.. but I need to create a file for a fixed length columns.

sample data:

a,b,c
d,e,,f
g,,,h
i,,j

I want the output for 3 columns fixed length delimited file

a,b,c
d,e,
g,,
i,,j

FOR /f "tokens=1-3 delims=," %%a IN (input) DO ECHO %%a,%%b,%%c>> output

For this script I'm getting the wrong output
a,b,c
d,e,f
g,h,
i,j

Please help in getting the desired output. Thank you!


Reply ↓  Report •
Related Solutions


Ask Question