0

I want to split a file into multiple files and save the file in date format.

Why doesn't this command work?

awk -v DATE= date '+%d%m%Y'-F"," 'NR>1 { print > "Test_" DATE ".CSV_"$1".csv"}' Testing.CSV
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
FARAH
  • 33
  • 1
  • 7
  • This is a follow-up of http://stackoverflow.com/questions/42292805/splitting-content-by-id-1st-column-and-generate-new-data-file-based-on-format – J. Chomel Feb 20 '17 at 08:35
  • Why not just use awk's own date functions. I see no need to use the shell version instead. – grail Feb 20 '17 at 10:23

2 Answers2

2

this should work

awk -F, -v date=$(date +%d%m%Y) 'NR>1{print > "Test_"date".CSV_"$1".csv"}' file

if not, try first

awk -v date=$(date +%d%m%Y) 'BEGIN{print date}'

to check whether date is correctly set as an awk variable.

karakfa
  • 66,216
  • 7
  • 41
  • 56
  • @RavinderSingh : This is my successfull command: awk -v DATE="$(date +"%Y%m%d")" -F"," 'NR>1 { print > "Assignment_"$1""DATE".csv"}' Text_01012020.CSV but it come out with this: Assignment_"A"_01012017 I want to remove "__", can you help me? – FARAH Feb 20 '17 at 03:49
  • if you don't want the underscore char, remove it from the command. The empty string "" before DATE is not necessary. Also, all caps variable naming is not recommended since it can override `awk` defined variables unintentionally. – karakfa Feb 20 '17 at 03:53
  • I just want to remove the double quoatation, as it "$1" then it will appear "A", I just want it appear only A without double quotation – FARAH Feb 20 '17 at 03:56
  • I just want to remove the double quoatation, as it "$1" then it will appear "A", I just want it appear only A without double quotation – FARAH Feb 20 '17 at 04:00
  • 1
    I think your field $1 has quote marks in the original input file, you can remove them with `gsub(/"/,"",$1)` – karakfa Feb 20 '17 at 04:05
  • I cannot find it..I put the above command but it give me sub not A. I want **A** only without "____" – FARAH Feb 20 '17 at 04:31
1

@FARAH: Try:

awk -v DATE=$(date +%d%m%Y) -F"," 'NR>1 { print > "Test_" DATE ".CSV_"$1".csv"}'   Input_file

As there is lack of information about samples and expected output so seeing that your command needs to some fine tuned, try above and let us know if this helps.

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • 1
    Don't you mean `$(date +%d%m%Y)`? Currently, `DATE` is set to the literal string `date +%d%m%Y`. – Benjamin W. Feb 20 '17 at 02:25
  • awk -v DATE="$(date +"%Y%m%d")" -F"," 'NR>1 { print > "Test_" DATE ".CSV_"$1".csv"}' input_file I try this..cannot also\ – FARAH Feb 20 '17 at 02:38
  • Thanks you Benjamin, I corrected it now, thank you for guiding here, I haven't tested the previous one actually. – RavinderSingh13 Feb 20 '17 at 03:34
  • @karakfa ----- This is my successfull command: awk -v DATE="$(date +"%Y%m%d")" -F"," 'NR>1 { print > "Assignment_"$1"_"DATE".csv"}' Text_01012020.CSV but it come out with this: Assignment_"A"_01012017 I want to remove "___", can you help me? – FARAH Feb 20 '17 at 03:40