-3

I managed to get this command as I want but why my column name excluded?

this is my command

awk \
  -v DATE="$(date +"%d%m%Y")" \
  -F"," \
  'BEGIN{OFS=","} NR>1{ gsub(/"/,"",$1); print > "Assignment_"$1"_"DATE".csv"}' \
  Test_01012020.CSV

In my original files, test_01012020.csv contain column: name, class, age and etc but after I do splitting in files Assignment_"$1"_"DATE".csv" I just get the value for example : FARAH, CLASS A, 24 and etc but in the new file not included column name. I need column name as original file not header in my splitting files. can anyone help me?

codeforester
  • 39,467
  • 16
  • 112
  • 140
FARAH
  • 33
  • 1
  • 7

1 Answers1

0

@FARAH: Try:

awk \
  -v DATE="$(date +"%d%m%Y")" \
  -F"," \
  'BEGIN{OFS=","} NR==1{print > "Assignment_"$1"_"DATE".csv"}} NR>1{ gsub(/"/,"",$1); print > "Assignment_"$1"_"DATE".csv"}' \
  Test_01012020.CSV

Obviously it will not print headings as NR>1 means leave the very first line, try above and you could change the headings as per your need too.

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • I want column name example name, class,age and etc. I have a lot of column name. how can i added manually? @RavinderSingh – FARAH Feb 21 '17 at 01:20
  • FARAH: just try to put the headings in print which is present in BEGIN section after OFS, try to put there and it should fly then. – RavinderSingh13 Feb 21 '17 at 01:24
  • but it quit difficult if i have a lot of column name. i need to key in manually? @RavinderSingh. is there more easy way to get it ? – FARAH Feb 21 '17 at 01:27
  • ok, I thought you need to enter few fields out of Input_file, I just edited code now, it should insert the first lien as heading now. – RavinderSingh13 Feb 21 '17 at 01:30
  • I still cannot include my column name. How? Please help me :'( @RavinderSingh – FARAH Feb 21 '17 at 01:45