How to remove double quotes using awk [duplicate]

2019-09-21 06:19发布

This question already has an answer here:

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?

3条回答
干净又极端
2楼-- · 2019-09-21 06:45

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
查看更多
虎瘦雄心在
3楼-- · 2019-09-21 06:45

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

查看更多
The star\"
4楼-- · 2019-09-21 07:08
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.

查看更多
登录 后发表回答