Removing header and trailer row, removing field in csv file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
vsmeruga
Posts: 35
Joined: 04 Jun 2014 06:42

Removing header and trailer row, removing field in csv file

#1 Post by vsmeruga » 06 Oct 2014 04:50

Hello

I am an Architect and not a windows batch scripter and I need help to write a small script to remove header and footer of a csv file located in a specific directory and also I should remove an unwanted field from the list of fields.

Please help me on this .

Many Thanks
sheku

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Removing header and trailer row, removing field in csv f

#2 Post by foxidrive » 06 Oct 2014 07:04

Please explain the task with examples.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Removing header and trailer row, removing field in csv f

#3 Post by Squashman » 06 Oct 2014 07:05

Would need to see an example of the data before any of us could help you.

vsmeruga
Posts: 35
Joined: 04 Jun 2014 06:42

Re: Removing header and trailer row, removing field in csv f

#4 Post by vsmeruga » 06 Oct 2014 08:14

Hi Sorry for the delayed reply .
File looks like below, it is a .csv file, I should remove header, trailer and "ID" field with value from the file.


0HEADER20140930-100111,,,,,,,
Date,Exch,Product,ID,Price,Multiplier,Account,Tax
20140930,20Y ,ABC ,1.1112E+11,1000,10,12347890,30
9TRAILER00000004,,,,,,,


vsmeruga
Posts: 35
Joined: 04 Jun 2014 06:42

Re: Removing header and trailer row, removing field in csv f

#5 Post by vsmeruga » 06 Oct 2014 08:27

ID - 1.1112E+11 - is a 11 digit number

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Removing header and trailer row, removing field in csv f

#6 Post by Squashman » 06 Oct 2014 09:01

Will any of the fields ever have empty data?

vsmeruga
Posts: 35
Joined: 04 Jun 2014 06:42

Re: Removing header and trailer row, removing field in csv f

#7 Post by vsmeruga » 06 Oct 2014 09:02

Yes. fields can be empty.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Removing header and trailer row, removing field in csv f

#8 Post by foxidrive » 06 Oct 2014 09:20

Here is a robust solution for the data you have shown:

Code: Select all

type "file.csv"|findrepl /o:2:-2 |findrepl "(.*?,.*?,.*?,).*?,(.*)" "$1$2" >"newfile.csv"



This uses a helper batch file called `findrepl.bat` (by aacini) - download from: https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.bat

Place `findrepl.bat` in the same folder as the batch file or on the path.

vsmeruga
Posts: 35
Joined: 04 Jun 2014 06:42

Re: Removing header and trailer row, removing field in csv f

#9 Post by vsmeruga » 06 Oct 2014 09:40

Thank you very much. I will try it and respond back.

vsmeruga
Posts: 35
Joined: 04 Jun 2014 06:42

Re: Removing header and trailer row, removing field in csv f

#10 Post by vsmeruga » 06 Oct 2014 09:43

Hi foxidrive

This is the example data I have provided. I get more rows every day in a single file with header and footer. I believe the solution provided to me will work for more rows

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Removing header and trailer row, removing field in csv f

#11 Post by foxidrive » 06 Oct 2014 10:04

It will also work for more rows and more columns, as long as the format of the data is the same.

vsmeruga
Posts: 35
Joined: 04 Jun 2014 06:42

Re: Removing header and trailer row, removing field in csv f

#12 Post by vsmeruga » 07 Oct 2014 04:25

Hi

I do not have access to dropbox and I could not get the file( repl.bat)
instead I found another link in the net as below and I tried creating a batch file(repl.bat) based on it but not succeded. Error came as shown as below
Please correct me what I have done wrong.

Thanks
Sheku




F:\>type "file.csv"|findrepl /o:2:-2 |findrepl "(.*?,.*?,
.*?,).*?,(.*)" "$1$2" >"newfile.csv"
'findrepl' is not recognized as an internal or external command,
operable program or batch file.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Removing header and trailer row, removing field in csv f

#13 Post by foxidrive » 07 Oct 2014 04:46

Search this forum for FINDREPL.BAT

You got the wrong helper batch file.

vsmeruga
Posts: 35
Joined: 04 Jun 2014 06:42

Re: Removing header and trailer row, removing field in csv f

#14 Post by vsmeruga » 07 Oct 2014 05:15

Hi I copied the code from the below link and created findrepl.bat and got the below error. My file.csv looks like below


F:\>type "file.csv"|findrepl /o:2:-2 |findrepl "(.*?,.*?,
.*?,).*?,(.*)" "$1$2" >"newfile.csv"
ERROR: Insufficient arguments. Use REPL /? to get help.


0HEADER20140930-100111,,,,,,,
Date,Exch,Product,ID,Price,Multiplier,Account,Tax
20140930,20Y ,ABC ,1.1112E+11,1000,10,12347890,30
9TRAILER00000004,,,,,,,

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Removing header and trailer row, removing field in csv f

#15 Post by foxidrive » 07 Oct 2014 05:42

vsmeruga wrote:
Hi I copied the code from the below link and created findrepl.bat and got the below error. My file.csv looks like below


You are still using the wrong batch file.

ERROR: Insufficient arguments. Use REPL /? to get help.

Post Reply