使用awk gsub

时间:2017-01-05 14:27:25

标签: awk gsub

我想使用awk将CSV文件转换为只包含原始列子集的新CSV文件。而且我还想用其中一列替换带下划线的空格。我试过这样的事:

gawk -F "," '
{
  name=gsub(/ /,"_",$1);
  label=$2;
  print ","name","label","
}' ./in.csv >> ./out.csv

但是gsub()返回匹配出现次数,而不是替换字符串。所以我得到这样的东西:

,1,label

而不是:

,name_nospace,label

如何使用这样的awk gsub替换一列的字符?

3 个答案:

答案 0 :(得分:4)

唐' T:

name=gsub()

as gsub返回替换次数,而不是字符串。刚

gsub()

并打印您摆弄的字段,即:

gsub(/ /,"_",$1);
label=$2;
print "," $1 "," label "," # or whatever you were doing

答案 1 :(得分:1)

要修改"名称",请更改:

name=gsub(/ /,"_",$1)

to(gawk and newer mawk only):

name=gensub(/ /,"_","g",$1)

或(任何awk):

name=$1
gsub(/ /,"_",name)

您还应该设置OFS而不是硬编码逗号,特别是如果您要修改字段,那么您的脚本应该写成:

awk '
BEGIN { FS=OFS="," }
{
  name=$1
  gsub(/ /,"_",name)
  label=$2
  print "", name, label, ""
}' ./in.csv

假设有一些使用变量而不是直接修改字段的原因。

答案 2 :(得分:0)

gawk -F "," '
{
  gsub(/ /,"_",$1);
  # print only: ,NameValue,LabelValue,   as output
  # so 4 field with first and last empty as in OP
  print "," $1 "," $2 ","
}' ./in.csv >> ./out.csv

在这种情况下,sed也可用

sed -e ':under' -e 's/^\([^[ ,]*\) /\1_/;t under' -e 's/^\([^,]*,[^,]*,\).*/,\1/' ./in.csv >> ./out.csv