使用 awk 正则表达式捕获插入符 (^)

发布于 2024-12-14 04:25:01 字数 523 浏览 8 评论 0原文

我有这种格式的输出:

/ignore-this/^/../I/want/this@ignore-this

我正在尝试使用 awk 正则表达式来捕获以下内容:

../I/want/this

这不会特别困难,除了我无法弄清楚如何正确转义 ^ 所以它不是解释为换行或不换行。下面是我到目前为止所拥有的,它几乎可以工作,除了打印出来:

/ignore-this/^/../I/want/this

这是代码:

#!/bin/awk -f                                                                              
{
    if (match($0, "\^.*@")){
        print substr($0, RSTART, RLENGTH-1);
    }
}

I have output with this format:

/ignore-this/^/../I/want/this@ignore-this

I am trying to use an awk regex to capture the following:

../I/want/this

This wouldn't be particularly hard except that I cannot figure out how to properly escape the ^ so it is not interpretted as an new line or a not. Below is what I have so far, it almost works except it prints out:

/ignore-this/^/../I/want/this

Here is the code:

#!/bin/awk -f                                                                              
{
    if (match($0, "\^.*@")){
        print substr($0, RSTART, RLENGTH-1);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

南冥有猫 2024-12-21 04:25:01

另一种可能性,使用 gawk:

#!/opt/local/bin/gawk -f
{
    if (match($0, /[\^]\/(.*)@/, pieces)) {
        print pieces[1];
    }
}

Another possibility, using gawk:

#!/opt/local/bin/gawk -f
{
    if (match($0, /[\^]\/(.*)@/, pieces)) {
        print pieces[1];
    }
}
生生漫 2024-12-21 04:25:01
> echo '/ignore-this/^/../I/want/this@ignore-this' |\ 
awk -F"^" '{split($NF,a,"@");print a[1]}' 

输出:

/../I/want/this

这将输入流拆分为所有“^”。然后它获取最后一个字段并将其拆分为“@”并打印字符串的前半部分。

编辑:
或使用:

awk '/\^/{split($0,a,"[@^]");print a[2]}' file

HTH Chris

> echo '/ignore-this/^/../I/want/this@ignore-this' |\ 
awk -F"^" '{split($NF,a,"@");print a[1]}' 

output:

/../I/want/this

This splits the input stream on all "^". Then it takes the last field and splits it on "@" and prints the first half of the string.

EDIT:
Or use:

awk '/\^/{split($0,a,"[@^]");print a[2]}' file

HTH Chris

黎歌 2024-12-21 04:25:01
awk -F'\\^|@' '{print $2}'

在这种情况下应该有效

kent$  echo "/ignore-this/^/../I/want/this@ignore-this"\
        |awk -F'\\^|@' '{print $2}' 
/../I/want/this
awk -F'\\^|@' '{print $2}'

should work in this case

kent$  echo "/ignore-this/^/../I/want/this@ignore-this"\
        |awk -F'\\^|@' '{print $2}' 
/../I/want/this
我不会写诗 2024-12-21 04:25:01

说到 awk regex 和插入符 "^",乍一看这可能看起来很奇怪,因为为什么后一个仅对第一个加倍有效正则表达式:

无效 :: /[^]/

有效 :: <代码>/[^][^]/

第一个无效,原因很明显:

  • 字符类 [...]^ 缺乏转义>

第二个是有效的,因为它实际上代表:

  • 任何东西除了 ][^

此处的顺序实际上很重要:将其写为 /[^[]^] / 相反,所发生的情况是 gawkmawk 只是默默地失败(或者匹配一些与你的初衷完全不相近的东西) ) 而 nawk 只是出错

speaking of awk regex with carets "^", this may look strange at first glance regarding why the latter one is valid for merely doubling the first regex:

invalid :: /[^]/

VALID :: /[^][^]/

1st one is invalid for the obvious reason :

  • lack of escaping for the stand-alone caret ^ inside a character class […]

2nd one is valid cuz it actually stands for :

  • anything except ], [, or ^

The order actually matters here : write it as /[^[]^]/ instead, and what happens is that gawk and mawk simply fail silently (or match something not at all close to what ur original intentions were) while nawk simply errors out

禾厶谷欠 2024-12-21 04:25:01
echo '/ignore-this/^/../I/want/this@ignore-this' | 

gawk NF=NF FS='.*\^|@.*' OFS=

mawk '$0=$2;' FS='[@^]'

/../I/want/this
echo '/ignore-this/^/../I/want/this@ignore-this' | 

gawk NF=NF FS='.*\^|@.*' OFS=

mawk '$0=$2;' FS='[@^]'

/../I/want/this
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文