使用 awk 忽略转义分隔符(逗号)?
如果我有一个带有转义逗号的字符串,如下所示:
a,b,{c\,d\,e},f,g
我如何使用 awk 将其解析为以下项目?
a
b
{c\,d\,e}
f
g
If I had a string with escaped commas like so:
a,b,{c\,d\,e},f,g
How might I use awk to parse that into the following items?
a
b
{c\,d\,e}
f
g
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
a
,使用 ',
' 作为分隔符a
构建数组b
,合并以 结尾的行'\
'b
(注意:从 2 开始,因为第一项为空)此解决方案假定(目前)'
,
'是唯一用 '\
' 转义的字符 - 也就是说,不需要处理输入中的任何\\
,也不需要处理诸如 < 之类的奇怪组合代码>\\\,\\,\\\\,,\,。a
, using ',
' as delimiterb
froma
, merging lines that end in '\
'b
(Note: Starts at 2 since first item is blank)This solution presumes (for now) that '
,
' is the only character that is ever escaped with '\
'--that is, there is no need to handle any\\
in the input, nor weird combinations such as\\\,\\,\\\\,,\,
.我不认为 awk 对这样的事情有任何内置支持。这里有一个解决方案,它不像 DigitalRoss 的那么短,但应该不会有意外碰到你的编弦的危险(!Q!)。由于它使用
if
进行测试,因此您还可以扩展它以小心字符串末尾是否确实有\\,
,这应该是转义斜杠, 不是逗号。I don't think awk has any built-in support for something like this. Here's a solution that's not nearly as short as DigitalRoss's, but should have no danger of ever accidentally hitting your made-up string (!Q!). Since it tests with an
if
, you could also extend it to be careful about whether you actually have\\,
at the end of your string, which should be an escaped slash, not comma.