How to remove double quotes using awk [duplicate]

风流意气都作罢 提交于 2019-12-25 17:45:10

问题


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?


回答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



回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/42338647/how-to-remove-double-quotes-using-awk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!