如果字符串不是以双引号开头,则向其添加双引号
Add a double quote to a string if it doesn't start with a double quote
我有一个这样的文本文件:
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 (~/^"/) s =
else s = addQuote()
print ,,s
}
addQuote
功能似乎不起作用,但我不知道如何修复它。
我知道在 sed
中我可以通过 sed 's/^/"/' line
轻松地在行首添加双引号,但我不知道如何使它与 [=15= 一起工作].
请帮忙。谢谢!
关注 awk
可能会对您有所帮助。
awk 'BEGIN{FS=OFS=","} !~ /^"/{="\"" } 1' Input_file
或
awk 'BEGIN{FS=OFS=","} {= !~ /^"/?"\"" :} 1' Input_file
编辑: 根据 Jonathan 先生在评论部分的评论添加以下代码,现在将处理 3 个案例,它应该添加 "
是不是完全在第 3 个字段上,它也会在字段的最后或字段的开头添加 "
。
假设我们有以下 Input_file:
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 个 permutations/combinations:
awk 'BEGIN{FS=OFS=","} {= !~ /\"/?"\"" "\"":( !~ /^\"/?"\"" :( !~ /\"$/? "\"":))} 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 中没有特定的运算符 - 两个并排的字符串 are 连接在一起,例如``"\"" 输入`.
因此,如果您将函数编写为:
function addQuote(input) {
return ("\"" input)
}
它会做你想做的事。我添加了括号以提高可读性。
话虽如此,这可能是一种更好的方法,因为它涵盖了前面 and/or 后面缺少的引号,并确保重新编译每一行,这在您更改 OFS 值时很重要:借用来自@RavinderSing13 答案的输入:
$ awk 'BEGIN{FS=OFS=","} {gsub(/^"|"$/,"",); ="\"" "\""} 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"
我有一个这样的文本文件:
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 (~/^"/) s =
else s = addQuote()
print ,,s
}
addQuote
功能似乎不起作用,但我不知道如何修复它。
我知道在 sed
中我可以通过 sed 's/^/"/' line
轻松地在行首添加双引号,但我不知道如何使它与 [=15= 一起工作].
请帮忙。谢谢!
关注 awk
可能会对您有所帮助。
awk 'BEGIN{FS=OFS=","} !~ /^"/{="\"" } 1' Input_file
或
awk 'BEGIN{FS=OFS=","} {= !~ /^"/?"\"" :} 1' Input_file
编辑: 根据 Jonathan 先生在评论部分的评论添加以下代码,现在将处理 3 个案例,它应该添加 "
是不是完全在第 3 个字段上,它也会在字段的最后或字段的开头添加 "
。
假设我们有以下 Input_file:
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 个 permutations/combinations:
awk 'BEGIN{FS=OFS=","} {= !~ /\"/?"\"" "\"":( !~ /^\"/?"\"" :( !~ /\"$/? "\"":))} 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 中没有特定的运算符 - 两个并排的字符串 are 连接在一起,例如``"\"" 输入`.
因此,如果您将函数编写为:
function addQuote(input) {
return ("\"" input)
}
它会做你想做的事。我添加了括号以提高可读性。
话虽如此,这可能是一种更好的方法,因为它涵盖了前面 and/or 后面缺少的引号,并确保重新编译每一行,这在您更改 OFS 值时很重要:借用来自@RavinderSing13 答案的输入:
$ awk 'BEGIN{FS=OFS=","} {gsub(/^"|"$/,"",); ="\"" "\""} 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"