0

This is my 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?

I find out this:

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

but after I run this command, my file can be assignment_A_01012017 but then inside my file..the column not seperated into column. How?

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
FARAH
  • 33
  • 1
  • 7
  • I don't see any `"___"` in your output. – pfnuesel Feb 20 '17 at 07:25
  • @pfnuesel yes. But actually I want to remove "____" if I do this command **awk -v DATE="$(date +"%d%m%Y")" -F"," 'NR>1 { print > "Assignment_"$1"_"DATE".csv"}' Test_01012020.CSV** my file naming will be like **Assignment_"A"_01012017 but actually I want **Assignment_A_01012017** without "__" – FARAH Feb 20 '17 at 07:32
  • Then remove the `"`. – pfnuesel Feb 20 '17 at 07:35
  • @pfnuesel I remove it but it come out with **Assigment_$1_01012017** – FARAH Feb 20 '17 at 07:37
  • show some sample of your input and expected output so we can see where is your problem. – NeronLeVelu Feb 20 '17 at 07:49
  • I already get it by using this command **awk -v DATE="$(date +"%d%m%Y")" -F"," 'BEGIN{OFS=","}NR>1 { gsub(/"/,"",$1); print > "Assignment_"$1"_"DATE".csv"}' Text_01012020.CSV** but I want column name also inside my file. how? @NeronLeVelu – FARAH Feb 20 '17 at 08:07

3 Answers3

1

Use below awk

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

Explanation

awk -F, -v DATE="$(date +'%Y%m%d')" '    # Start awk, where field sep being  comma
                                         # and variable DATE with current date
     NR>1{                               # If no records greater than 1 then 
            s=$1;                        # save field1 data to variable s
            gsub(/"/,"",s);              # substitute double quote with null in variable s, so here we remove quote

            # print current record/line to file Assigment_{field1}_{current_date}.csv
            # {field1} = value of variable s after removing double quote
            # {current_date} = value of variable DATE

            print > "Assignment_"s"_"DATE".csv"
         }
     ' Text_01012020.CSV
Akshay Hegde
  • 16,536
  • 2
  • 22
  • 36
  • thank you. I already get it. but then how can I put column name together? This is my command **awk -v DATE="$(date +"%d%m%Y")" -F"," 'BEGIN{OFS=","}NR>1 { gsub(/"/,"",$1); print > "Assignment_"$1"_"DATE".csv"}' Text_01012020.CSV** – FARAH Feb 20 '17 at 08:18
  • @FARAH what do you mean by column name together ? – Akshay Hegde Feb 20 '17 at 08:31
  • @FARAH : if you remove `NR>1`, if your file `Text_01012020.CSV` contains header then you will get column name – Akshay Hegde Feb 20 '17 at 08:32
  • for example I have 3 column name: Age, School and Class but if I do above command, the column name excluded @Akshay Hegde . I want include the column name inside my data – FARAH Feb 20 '17 at 08:33
  • @FARAH: Its because of `NR>1` just remove `NR>1` from your command, and see the result – Akshay Hegde Feb 20 '17 at 08:35
  • I cannot delete NR>1. Because I only get column name. I want column name and the data in my splitting file. @Akshay Hegde – FARAH Feb 20 '17 at 08:37
  • @FARAH: You mean you want header for each file right ? – Akshay Hegde Feb 20 '17 at 08:38
  • Not. I want column name for each column. @Akshay Hegde – FARAH Feb 20 '17 at 08:40
  • @FARAH: I am sorry I didn't get you, if you can provide sample input and expected o/p so much time can be saved, this retains field name of each column while splitting `awk -v DATE="$(date +'%Y%m%d')" 'NR==1{h=$0;next}{s=$1; gsub(/"/,"",s); f="Assignment_"s"_"DATE".csv"; if(p!=f){ print h>f; p=f } print >f}' your_file` – Akshay Hegde Feb 20 '17 at 08:44
0
awk -v DATE="$(date +"%d%m%Y")" -F"," 'BEGIN{OFS=","}NR>1 { gsub(/"/,"",$1); print > "Assignment_"$1"_"DATE".csv"}' Text_01012020.CSV

Just set "OFS" configuration, then output will be with comma.

Jimmy
  • 1
  • Thanks @Jimmy I already get it but wait I also want column name inside my file. when i use this, the column name excluded. – FARAH Feb 20 '17 at 07:57
  • awk -v DATE="$(date +"%d%m%Y")" -F"," 'BEGIN{OFS=","}NR==1{FIRST_LINE=$0}NR>1 { gsub(/"/,"",$1); if(!a[$1]++){print FIRST_LINE > "Assignment_"$1"_"DATE".csv" } print > "Assignment_"$1"_"DATE".csv"}' Text_01012020.CSV add column name with first line, and output it when you met the first "$1" – Jimmy Feb 21 '17 at 03:12
0

If you want to include your column name, then read it with the case when NR==1:

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

And you can also learn for yourself with this great tutorial

J. Chomel
  • 8,193
  • 15
  • 41
  • 69