Delete rows from csv file when column value is not equal to

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Delete rows from csv file when column value is not equal

#16 Post by aGerman » 02 Aug 2010 13:04

You also talk about csv files. I can't see any commas, so it isn't a "real" csv file! What else is the data separator? In case it is a tab then note that the forum software doesn't display it. Again: We need to know the data separator!

Regards
aGerman

santhosh
Posts: 41
Joined: 02 Aug 2010 05:10

Re: Delete rows from csv file when column value is not equal

#17 Post by santhosh » 02 Aug 2010 14:45

Thanks for quick respond german.

Yes it is comma separator.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Delete rows from csv file when column value is not equal

#18 Post by aGerman » 02 Aug 2010 15:17

OK. Means, if you open you csv file using a text editor it looks like that

Code: Select all

ap,ch,rum,sun,ka,jan,jim,bin,sin
kl,ka,kim,rim,ka,jan,jim,ap,bun
tn,kk,lun,sun,kl,nem,jim,bin,bun


In this case you could use that

Code: Select all

@echo off &setlocal
>"new.csv" type nul
for /f "usebackq tokens=1-8* delims=," %%a in ("sat.csv") do (
  if "%%h"=="ap" (
    >>"new.csv" echo %%a,%%b,%%c,%%d,%%e,%%f,%%g,%%h,%%i
  )
)


Regards
aGerman

santhosh
Posts: 41
Joined: 02 Aug 2010 05:10

Re: Delete rows from csv file when column value is not equal

#19 Post by santhosh » 02 Aug 2010 16:10

Thanks agreman

thank you very much , i will try it and confirm you.

santhosh
Posts: 41
Joined: 02 Aug 2010 05:10

Re: Delete rows from csv file when column value is not equal

#20 Post by santhosh » 02 Aug 2010 16:31

Thanks agerman

Its working fine........i got another doubt but i will post it in related forum.......thanks for your quick support.

santhosh
Posts: 41
Joined: 02 Aug 2010 05:10

Re: Delete rows from csv file when column value is not equal

#21 Post by santhosh » 09 Aug 2010 15:13

hi agerman,

now i am facing another problem...


FINDSTR /C:"TN", channelTransaction*.csv | FINDSTR /C:"O2C" | FINDSTR /C:"TRANSFER" > O2CTESTTN.CSV
> TN\TN_south_%gg%\TN\O2C\O2C.csv type nul
for /f "usebackq tokens=1-8* delims=," %%a in ("O2CTESTTN.csv") do (
if "%%f"=="TN" (
>> TN\TN_south_%gg%\TN\O2C\O2C.csv echo %%a,%%b,%%c,%%d,%%e,%%f,%%g,%%h,%%i
)
)



if all the column contains data then the ouput is coming..........if a column is blank then the output not coming.

example:

sat ran fun gun gim TN asm
sat blank fav cou rum TN ss

output should be:
it should print both the line .

but its printing only first line.
and if any of the column is empty its not printing that row.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Delete rows from csv file when column value is not equal

#22 Post by aGerman » 09 Aug 2010 15:55

I know this problem. We split the line on each found comma. The problem is that the cmd interpretes ",," like a single comma. I have no clean solution for this problem. The only thing we could do is to replace ",," by ", ," and reverse this after process the line.

Untested:

Code: Select all

setlocal EnableDelayedExpansion
for /f "usebackq delims=" %%a in ("O2CTESTTN.csv") do (
  set "line=%%a"
  set "line=!line:,,=, ,!"
  set "line=!line:,,=, ,!"
  for /f "tokens=1-8* delims=," %%b in ("!line!") do (
    if "%%g"=="TN" (
      set "newLine=%%b,%%c,%%d,%%e,%%f,%%g,%%h,%%i,%%j"
      set "newLine=!newLine:, ,=,,!"
      set "newLine=!newLine:, ,=,,!"
      >> TN\TN_south_%gg%\TN\O2C\O2C.csv echo\!newLine!
    )
  )
)
endlocal


Regards
aGerman

santhosh
Posts: 41
Joined: 02 Aug 2010 05:10

Re: Delete rows from csv file when column value is not equal

#23 Post by santhosh » 09 Aug 2010 16:25

Thanks agerman

its working

now i going to change all my coding to this one :(

Post Reply