使用awk为每个字段插入引号

时间:2019-08-26 09:44:44

标签: perl awk sed

我正在根据下面提供的示例寻找下面的输入

示例:

eno~ename~address~zip
123~abc~~560000~"a~b~c"
245~"abc ~ def"~hyd~560102
333~"ghi~jkl"~pub~560103

预期输出:

"eno"~"ename"~"address"~"zip"
"123"~"abc"~""~"560000"~"a~b~c"
"245"~"abc ~ def"~"hyd"~"560102"
"333"~"ghi~jkl"~"pub"~"560103"
我在awk中尝试过的

命令如果分隔符值包含在数据中则不起作用。如果还有其他建议,请加上perl / sed / awk建议。

下面是命令:awk'{for(i = 1; i <= NF; i ++)$ i =“ \”“ $ i” \“”} 1'FS =“〜” OFS =“〜”样本

2 个答案:

答案 0 :(得分:2)

请您尝试以下操作(仅通过提供的示例进行测试)。

awk 'BEGIN{s1="\"";FS=OFS="~"} {for(i=1;i<=NF;i++){if($i!~/^\"|\"$/){$i=s1 $i s1}}} 1' Input_file

输出如下。

"eno"~"ename"~"address"~"zip"
"123"~"abc"~""~"560000"
"245"~"abc ~ def"~"hyd"~"560102"
"333"~"ghi~jkl"~"pub"~"560103"

说明: 现在添加上述代码的说明。

awk '                       ##Starting awk program here.
BEGIN{                      ##Starting BEGIN section of awk program here.
  s1="\""                   ##Setting variable s1 to " here.
  FS=OFS="~"                ##Setting value of FS and OFS as ~ here.
}                           ##Closing BEGIN block of awk code here.
{
  for(i=1;i<=NF;i++){       ##Starting for loop here from i=1 to till value of NF here.
    if($i!~/^\"|\"$/){      ##Checking condition of value of current field is NOT having s1 value in it.
      $i=s1 $i s1           ##Adding s1 variable before and after the value of $i.
    }                       ##Closing block for if condition.
  }                         ##Closing block for for loop here.
}                           ##Closing main block here.
1                           ##Mentioning 1 will print the lines of Input_file.
'  Input_file               ##mentioning Input_file name here.


答案 1 :(得分:0)

您可以在FPAT上使用gnu awk

awk -v FPAT='([^~]*)|("[^"]+")' -v OFS="~" '{for (i=1;i<=NF;i++) if ($i!~/^\"/) $i="\""$i"\""} 1' file
"eno"~"ename"~"address"~"zip"
"123"~"abc"~""~"560000"
"245"~"abc ~ def"~"hyd"~"560102"
"333"~"ghi~jkl"~"pub"~"560103"

我们而不是讲字段分隔符的外观,而是讲字段的外观。然后测试字段是否没有双引号,如果没有,请将其添加。

然后,您可以根据需要轻松更改字段分隔符:

awk -v FPAT='([^~]*)|("[^"]+")' -v OFS="," '{for (i=1;i<=NF;i++) if ($i!~/^\"/) $i="\""$i"\""} 1' file
"eno","ename","address","zip"
"123","abc","","560000"
"245","abc ~ def","hyd","560102"
"333","ghi~jkl","pub","560103"