-1

I have sample data file (csv) as below and required to generate large data from sample file. for that i need to change date (DD) in timestamp and run for loop to generate more data.

14/03/2017 00:05:58,General2,General3,General4,General5,General6,General7

closed as unclear what you're asking by RomanPerekhrest, αғsнιη, Jeff Schaller, Stephen Rauch, Kusalananda Aug 16 '17 at 20:43

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    What do you want to change the date to? A pistachio? – DopeGhoti Aug 16 '17 at 16:48
  • 1
    required to generate monthly data. Hence required to change date with another DD. like 15 – Rakesh C Aug 16 '17 at 16:49
0

Here is one of the possibilities in bash (tried on Ubuntu 16.04.3 LTS)

Put your csv in a file called yourfile.csv (or change this code), then run this:

#!/bin/bash
DATE=$1
for i in `seq 1 30`; do
        NEWDATE=`date -d"${DATE}+${i}day" +'%d/%m/%Y'`
        while read line; do
                echo $line | sed -u -r "s#^.{10}#${NEWDATE}#"
        done <yourfile.csv
done

It will generate +30 days for EACH input line, so you only need to have file with 1 line. Oh and it accepts date as parameter and will generate +30 days of data starting from that date.

Update for slowness: Try this, maybe it works faster for you:

#!/bin/bash
DATE=$1
let i=0
while read line; do
        let i=(i++ % 30)
        NEWDATE=`date -d"${DATE}+${i}day" +'%d/%m/%Y'`
        echo $line | sed -u -r "s#^.{10}#${NEWDATE}#"
done <yourfile.csv

It will be 1M row will make 1M rows, not 1M makes 30M.

  • Your bash script is working perfectly fine. but its taking too much time. Could you please help me with perl or python script. – Rakesh C Aug 17 '17 at 12:29
  • How many lines did you have in your csv file? It works practically instantly for me, and I had just 4 lines. Remember, it makes 30 lines of csv for each 1 line in the file, so if you kept a large csv, it would create a huge one. – Gnudiff Aug 17 '17 at 13:12
  • 1M row we have in sample CSV file – Rakesh C Aug 17 '17 at 14:08
  • Well then use just the first 10 lines of it maybe? – Gnudiff Aug 17 '17 at 15:22
  • Added another solution, which does not generate new lines, but changes all lines in existing file. – Gnudiff Aug 18 '17 at 7:32
0

To simply change the days in the date to some fixed value like 15, you can do

sed 's/[0-9]*/15/' your.csv

If you want to change it in that file, add the -i option if your sed supports it.

If you want to automate this for all days in many month, better use another tool like python or perl.

Update: Or have some sed fun:

sed -n 'p;x;s/^/0123456789_/;H;g
  :a
  s#\(.\)\(/.*\1\)\(.\)#\3\2\3#
  s/^0_/10/;s/^1_/20/;s/^2_/30/
  /^31.0[46]/s/1/2/;s#^32/0\(.\)\(.*\1\)\(.\)#01/0\3\2\3#
  P;\#^01/08#!ba' sample.csv

This multiplies each line with each day starting from the given 14/03 to the 01/08. Sorry for drinking too much.

  • Could you please share the example please – Rakesh C Aug 16 '17 at 16:58
  • Share what example? For the automatization? Then you need to define what you need exactly. – Philippos Aug 16 '17 at 17:01
  • yes, for automation – Rakesh C Aug 16 '17 at 17:01
  • 15/03/2017 00:05:58,General2,General3,General4,General5,General6,General7 Just required to change DD rest values will be same. – Rakesh C Aug 16 '17 at 17:02
  • Only for a couple of days or for each day from March 14th to August 1st or what? Please edit the question to clarify. – Philippos Aug 16 '17 at 17:05

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