我正在尝试将单个字符添加到引用的csv文件的开头,如下所示:
"a","b","c","d","e","f","g","h"
我需要的输出如下:
"a","b","c","d","e","Nf","g","h"
使用以下awk命令:
awk -F '{ $6="N" $6 }1' file.csv
我得到以下结果,但结果不正确:
"a","b","c","d","e",N"f","g","h"
如何编写awk
命令,该命令会在给定字段的引用文本中正确添加字符?
答案 0 :(得分:1)
使用 awk
的sub()
函数:
awk 'BEGIN{ FS=OFS="," }{ sub(/"/, "&N", $6) }1' file.csv
输出:
"a","b","c","d","e","Nf","g","h"
或使用substr()
功能:
awk 'BEGIN{ FS=OFS="," }{ $6="\042N"substr($6, 2) }1' file.csv
答案 1 :(得分:1)
$ awk 'BEGIN{FS=OFS="\",\""} {$6="N"$6} 1' file
"a","b","c","d","e","Nf","g","h"
答案 2 :(得分:1)
使用GNU sed
$ sed 's/,"/,"N/5' file
"a","b","c","d","e","Nf","g","h"
在第5个逗号之后进行替换,即第6个字段。
答案 3 :(得分:1)
awk
一个班轮
awk 'BEGIN{FS=OFS=","} {sub(/"/,"\"N",$6)}1' f1
"a","b","c","d","e","Nf","g","h"