Home > Back-end >  Batch script to delete rown where column value is "IN" and "BR"
Batch script to delete rown where column value is "IN" and "BR"

Time:07-09

I Have a csv file which has 35 columns and in 19th column i get these country values like IN, BR, US etc. I need to remove the rows where the country value in 19th column is IN and BR.

I tried the following but has many issues as i can use tokens upto 26 only and also not being able to use both IN and BR.

@echo off &setlocal
set /p "header="<"old.csv"
>"your new.csv" echo.%header%
for /f "usebackq skip=1 delims=, tokens=1-26*" %%a in ("old.csv") do (
  if not "%%t" == "BR" 
  (
    >>"your new.csv" echo.%%a,%%b,%%c,%%d.....,%%Z
  )
)

CodePudding user response:

If no (quoted) field values contain , on their own, the following code could work:

findstr /V "IN,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*$ BR,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*$" "old.csv"

The search expressions are anchored to the end of lines, because findstr does not support more than 16 character classes, but 19 would be required from the beginning.

The header is not specifically handled, as I assumed it does neither contain IN nor BR in the 17th field from behind.

CodePudding user response:

This should work:

@echo off
setlocal EnableDelayedExpansion

set "remove=IN|BR"

set /P "header=" < "old.csv"

> "your new.csv" (
   echo %header%
   for /F "usebackq skip=1 delims=, tokens=1-19*" %%a in ("old.csv") do (
      if "!remove:%%~s=!" equ "%remove%" (
         echo %%a,%%b,%%c,%%d,...,%%s,%%t
      )
   )
)

From the FOR /? help screen:

If the last character in the tokens= string is an asterisk, an additional variable is assigned that receive the rest of the text in the line after the last analyzed token.

In other words, in previous code the %%t token receive all the columns after the 19th one...

  • Related