如何使用Awk通过Whitespace凹痕识别部分
这是数据文件:
foo:
item1: 1
item2=2
bar:
item1 = 100
item2 : 200
Whitespace缩进用于指定部分,例如“ foo”和“ bar”是“启动”部分,因为它们处于线路的开头。但是默认情况下,Awk删除了空格,这让我想知道如何编写匹配模式。
我尝试了一下:
$0 ~ ^[a-z]+: {print "section title";}
$0 ~ ^[ ]+[a-z]+ {print "item";}
但是我有语法错误:
awk: /tmp/2.awk:1: $0 ~ ^[a-z]+: {print "section title";}
awk: /tmp/2.awk:1: ^ syntax error
awk: /tmp/2.awk:1: $0 ~ ^[a-z]+: {print "section title";}
awk: /tmp/2.awk:1: ^ syntax error
awk: /tmp/2.awk:2: $0 ~ ^[ ]+[a-z]+ {print "item";}
awk: /tmp/2.awk:2: ^ syntax error
awk: /tmp/2.awk:2: $0 ~ ^[ ]+[a-z]+ {print "item";}
awk: /tmp/2.awk:2: ^ syntax error
请帮助。
[更新]感谢Ravindersingh13,现在有效:
>cat /tmp/2.awk
$0 ~ /^[a-z]+:/ {print "section title";}
$0 ~ /^[ ]+[a-z]+/ {print "item";}
>awk -f /tmp/2.awk /tmp/data
section title
item
item
section title
item
item
Here is the data file:
foo:
item1: 1
item2=2
bar:
item1 = 100
item2 : 200
The whitespace indent is used to designate sections, e.g. "foo" and "bar" are section start because they are at the start of the lines. But AWK by default removes whitespace, this makes me wondering how to write the matching pattern.
I tried this:
$0 ~ ^[a-z]+: {print "section title";}
$0 ~ ^[ ]+[a-z]+ {print "item";}
But I got syntax errors:
awk: /tmp/2.awk:1: $0 ~ ^[a-z]+: {print "section title";}
awk: /tmp/2.awk:1: ^ syntax error
awk: /tmp/2.awk:1: $0 ~ ^[a-z]+: {print "section title";}
awk: /tmp/2.awk:1: ^ syntax error
awk: /tmp/2.awk:2: $0 ~ ^[ ]+[a-z]+ {print "item";}
awk: /tmp/2.awk:2: ^ syntax error
awk: /tmp/2.awk:2: $0 ~ ^[ ]+[a-z]+ {print "item";}
awk: /tmp/2.awk:2: ^ syntax error
Please help.
[UPDATE] Thanks RavinderSingh13, now it works:
>cat /tmp/2.awk
$0 ~ /^[a-z]+:/ {print "section title";}
$0 ~ /^[ ]+[a-z]+/ {print "item";}
>awk -f /tmp/2.awk /tmp/data
section title
item
item
section title
item
item
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于
awk默认情况下删除whitespace
- 不,它没有。不知道您对此有何想法。在您的代码中,您只是忘了将Regexp定界符
/.../
围绕您的Regexp,因此您可以写:
$ 0〜/foo/
,因为/foo/
表示[az]+
,因为这一切都在任何领先的空白之后,因此您实际需要的只是:
Regarding
AWK by default removes whitespace
- no, it doesn't. Not sure what you're thinking of with that.In your code you just forgot to put regexp delimiters
/.../
around your regexp so you COULD write it as:but:
[ ]
) as it's already literal$0 ~ /foo/
as just/foo/
means the same thing[a-z]+
as that's all there is after any leading blanksso all you actually need is: