New CSV column with todays date

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
adriles
Posts: 4
Joined: 03 Jan 2018 05:09

New CSV column with todays date

#1 Post by adriles » 03 Jan 2018 05:21

Hi, I only discovered how to create .bat files last week and its starting to change my world (in work). Excitedly I've written a few files to move, copy and rename documents but what I'm trying to now I cant find help with through searching google/forums...

Please could a charitable sole help me with the code to add a column to the end of a pipeline delimited csv and have every row in the new column contain todays date?

Further background- the data extract I need to update always has the same number of columns, but is ever increasing in rows. The reason I am adding a date is so when I import the data to my system I have an accurate source date for weeding expired data.

With help from another forum i'm currently playing around with:

Code: Select all

@echo off
(for /f "delims=" %%a in (file.csv) do (
  echo(%%a|%time%
))>"fileout.csv" 
Last edited by adriles on 03 Jan 2018 07:24, edited 1 time in total.

adriles
Posts: 4
Joined: 03 Jan 2018 05:09

Re: New CSV column with todays date

#2 Post by adriles » 03 Jan 2018 06:49

I've tried these so far but cant get them to work:

Code: Select all

$ awk -F, 'NF>1{$0 = "\"DD/MM/YYYY"\" FS $0}1' file
[/code]sed 'N;s/","/","DD/MM/YYYY","/5' file[/code]

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

Re: New CSV column with todays date

#3 Post by Squashman » 03 Jan 2018 08:44

You were close with your batch file. You have to remember that a PIPE is a special character just like it is in a BASH script as well. In a batch file you need to escape it with the circumflex if you want to output the PIPE to the output file.

Code: Select all

echo %%a^|%date% %time%
You probably are not going to get a lot of help with AWK and SED on this forum.

adriles
Posts: 4
Joined: 03 Jan 2018 05:09

Re: New CSV column with todays date

#4 Post by adriles » 04 Jan 2018 09:11

Thank you for your help Squashman.

All looked good in 'test' but now I've implemented the code in 'prod' with a 16000+row csv I have 334 rows that didnt get a date added to the end. I re-ran the bat file and the same 334 were missing again. All of the rows have data in column 1 and data upto the same column number as rows that did get a date.

Any suggestions or tweaks?

Thanks, Adrian

adriles
Posts: 4
Joined: 03 Jan 2018 05:09

Re: New CSV column with todays date

#5 Post by adriles » 05 Jan 2018 11:12

I am currently receiving help on stackoverflow.com and will update here if a solution is possible. Apologies to anyone frustrated by it being open on two forums.

Post Reply