如何使用awk删除双引号

时间:2017-02-20 07:22:37

标签: linux awk scripting

这是我的命令:

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

但它出来了:

Assignment_"A"_01012017

我想删除" ___",你能帮助我吗?

我发现了这个:

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

但是在我运行此命令之后,我的文件可以是assignment_A_01012017但是然后在我的文件中...列没有分隔成列。怎么样?

3 个答案:

答案 0 :(得分:1)

使用以下awk

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

<强>解释

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

答案 1 :(得分:0)

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

只需设置“OFS”配置,然后输出将使用逗号。

答案 2 :(得分:0)

如果要包含列名,请在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

您还可以使用此great tutorial

自学