Awk 如果字符串没有';不要以双引号开头

Awk 如果字符串没有';不要以双引号开头,awk,text-processing,command-line-tool,Awk,Text Processing,Command Line Tool,我有这样一个文本文件: 1,a,"some strings in a pair of double quotes" 2,b,"more strings in a pair of double quotes" 3,c,some messy strings with only right half double quotes" 4.d,"more strings in a pair of double quotes" 我尝试使用awk和sed将缺少的左双引号添加到第3行: function add

我有这样一个文本文件:

1,a,"some strings in a pair of double quotes"
2,b,"more strings in a pair of double quotes"
3,c,some messy strings with only right half double quotes"
4.d,"more strings in a pair of double quotes"
我尝试使用awk和sed将缺少的左双引号添加到第3行:

function addQuote(input) {
 return '"' + input
}    
BEGIN{
   FS=","
}

{
  if ($3~/^"/) s = $3
  else s = addQuote($3)

  print $1,$2,s
}
似乎
addQuote
函数不起作用,但我不知道如何修复它

我知道在
sed
中,通过执行
sed的/^/“/”行
,我可以轻松地在行的开头添加双引号,但我不知道如何使它与
awk
一起工作。
请帮忙。谢谢!

下面的
awk
可能会对您有所帮助

awk 'BEGIN{FS=OFS=","} $3 !~ /^"/{$3="\"" $3} 1'  Input_file


编辑:根据Jonathan爵士在评论部分的评论,添加以下代码,现在将处理3个案例,它应该添加
如果它不完全在第3个字段上,它将在字段的最后或字段的开头添加

假设我们有以下输入文件:

cat Input_file
1,a,"some strings in a pair of double quotes"
2,b,"more strings in a pair of double quotes"
3,c,some messy strings with only right half double quotes"
4,d,"more strings in a pair of double quotes
4,d,more strings in a pair of double
现在,以下代码可能涵盖此处提到的所有3种排列/组合:

awk 'BEGIN{FS=OFS=","} {$3=$3 !~ /\"/?"\"" $3 "\"":($3 !~ /^\"/?"\"" $3:($3 !~ /\"$/?$3 "\"":$3))} 1'  Input_file
1,a,"some strings in a pair of double quotes"
2,b,"more strings in a pair of double quotes"
3,c,"some messy strings with only right half double quotes"
4,d,"more strings in a pair of double quotes"
4,d,"more strings in a pair of double"

addQuote()函数的问题:

function addQuote(input) {
 return '"' + input
}
它们是:

  • 字符串分隔符是
    ,而不是
    ,因此应该使用
    “\”“
    而不是
  • +
    是awk中的算术运算符,因此
    “\”
    +
    input
    告诉awk将
    “\”
    input
    的内容转换为数字,然后将它们相加。取而代之的是连接,在awk中没有特定的运算符-并排的两个字符串是连接的,例如输入
  • 因此,如果您将函数编写为:

    function addQuote(input) {
     return ("\"" input)
    }
    
    它可以做你想做的。为了可读性,我添加了参数

    话虽如此,这可能是一种更好的方法,因为它覆盖了前面和/或后面缺少的引号,并确保每一行都得到重新编译,这在您更改OFS值时非常重要:借用@RavinderSing13答案中的输入:

    $ awk 'BEGIN{FS=OFS=","} {gsub(/^"|"$/,"",$3); $3="\"" $3 "\""} 1' file
    1,a,"some strings in a pair of double quotes"
    2,b,"more strings in a pair of double quotes"
    3,c,"some messy strings with only right half double quotes"
    4,d,"more strings in a pair of double quotes"
    4,d,"more strings in a pair of double"
    

    你能修复创建此断开文件的过程吗?这是我从其他人那里得到的原始文件…你告诉过其他人该文件包含错误吗?当带引号的字符串包含逗号时会出现问题!@user3768495,酷,也请查看此链接一次,如果有人帮助,我注意这将转换
    1,33,根本不带引号
    1,33中,“完全没有引号,
    ,缺少第二个双引号。我不确定这是否是一个重大问题。升级正则表达式以匹配并不困难:
    /^[^”].*$/
    可能会处理它,而我的示例输入保持不变。如果它应该被完全引用,那也可以处理(稍微不同,但同样直接)。@JonathanLeffler,当然,我现在添加了编辑解决方案,先生,它应该涵盖没有
    无论是在开始还是结束,或者不是在线路本身,请务必让我知道我们是否可以在这里做任何进一步的改进。避免负面逻辑(
    ),因为这会导致无法理解的双重否定,并避免冗余(在三元逻辑的真/假结果上增加
    $3
    )。更改
    $3=$3!~/^“/?”\”“$3:$3
    $3=($3~/^”/?”:“\”“)$3
    。我还添加了parens和空格,这样在一些awk中就不会产生语法错误,并且可以提高易读性。
    $ awk 'BEGIN{FS=OFS=","} {gsub(/^"|"$/,"",$3); $3="\"" $3 "\""} 1' file
    1,a,"some strings in a pair of double quotes"
    2,b,"more strings in a pair of double quotes"
    3,c,"some messy strings with only right half double quotes"
    4,d,"more strings in a pair of double quotes"
    4,d,"more strings in a pair of double"