Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How to filter CSV files using batch as below:

I have a file with 7 columns separated by '~'

I need to check if column number 4 is in (456,987,654,123,456), If yes than append the whole row to existing csv file

this link may help, but i dont know how to modify it to use it

share|improve this question
    
First, ~ isn't a comma, so I don't think this is technically a Comma Separated Values (CSV) case. It is, however, a fixed delimiter issue. Are you sure this isn't a homework exercise? –  Slartibartfast Jun 14 '11 at 4:44
    
solved it , thanks: –  user1111111 Jun 14 '11 at 4:52
    
Installed LogParser and run below: logparser -i:CSV "SELECT * into C:\Battest\Final.csv FROM C:\Battest\test.csv where mbrno = 123" –  user1111111 Jun 14 '11 at 4:52

1 Answer 1

up vote 1 down vote accepted

This is a shot in the dark. I haven't tested it:

:: ------------------ BAT start here -----------
@echo off
del c:\Total.csv
Del c:\Final.csv

Type c:\BATTEST\*.CSV > c:\Total.csv

FOR /F "eol=- delims=~ tokens=1-5,6,7*" %%1 IN ( c:\Total.CSV ) DO IF %%4 EQ 456 ECHO %%1 %%2 %%3 %%4 %%5 %%6 %%7 %%8 >> c:\Final.csv
FOR /F "eol=- delims=~ tokens=1-5,6,7*" %%1 IN ( c:\Total.CSV ) DO IF %%4 EQ 987 ECHO %%1 %%2 %%3 %%4 %%5 %%6 %%7 %%8 >> c:\Final.csv
FOR /F "eol=- delims=~ tokens=1-5,6,7*" %%1 IN ( c:\Total.CSV ) DO IF %%4 EQ 654 ECHO %%1 %%2 %%3 %%4 %%5 %%6 %%7 %%8 >> c:\Final.csv
FOR /F "eol=- delims=~ tokens=1-5,6,7*" %%1 IN ( c:\Total.CSV ) DO IF %%4 EQ 123 ECHO %%1 %%2 %%3 %%4 %%5 %%6 %%7 %%8 >> c:\Final.csv

:: ------------------ Bat end here --------------------

A few assumptions this makes:

  • your file has 8 fields in it, never more.
  • your source files are located in C:\BATTEST

If it works, your output file will be in C:\Final.csv. If your file has fewer fields in it, then there are a few things that would need to be changed.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.