如何使用Awk通过Whitespace凹痕识别部分

发布于 2025-01-25 07:28:07 字数 1017 浏览 1 评论 0原文

这是数据文件:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万劫不复 2025-02-01 07:28:07

关于awk默认情况下删除whitespace - 不,它没有。不知道您对此有何想法。

在您的代码中,您只是忘了将Regexp定界符/.../围绕您的Regexp,因此您可以写

$0 ~ /^[a-z]+:/  {print "section title";}
$0 ~ /^[ ]+[a-z]+/  {print "item";}

  1. 为 > [] ),因为它已经是字面意思,
  2. 您不需要编写$ 0〜/foo/,因为/foo/表示
  3. 您不喜欢的东西' t需要测试[az]+,因为这一切都在任何领先的空白之后,

因此您实际需要的只是:

$ awk 'NF{print (/^ / ? "item" : "section title")}' file
section title
item
item
section title
item
item

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:

$0 ~ /^[a-z]+:/  {print "section title";}
$0 ~ /^[ ]+[a-z]+/  {print "item";}

but:

  1. you don't need to put a blank inside a bracket expression ([ ]) as it's already literal
  2. You don't need to write $0 ~ /foo/ as just /foo/ means the same thing
  3. You don't need to test for [a-z]+ as that's all there is after any leading blanks

so all you actually need is:

$ awk 'NF{print (/^ / ? "item" : "section title")}' file
section title
item
item
section title
item
item
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文