简体   繁体   English

awk regexp 仅在一个选项卡上匹配

[英]awk regexp match on only one tab

I have a simple input file for awk, called tabmatch.input and with the below content:我有一个简单的 awk 输入文件,名为 tabmatch.input,内容如下:

        : (test1
            : (test2

The first line has one tab, then the ":", and the second line has two tab, then the ":".第一行有一个制表符,然后是“:”,第二行有两个制表符,然后是“:”。 The words "test1" and "test2" could be any word in the real file I try to parse. “test1”和“test2”这两个词可以是我尝试解析的真实文件中的任何词。

I am trying to create a regexp that matches the first line, but not the second.我正在尝试创建一个匹配第一行但不匹配第二行的正则表达式。 For example I try this:例如我试试这个:

user@lab-client:~$ cat tabmatch.input |awk '/\t: \(test/ {  {print $2} }'
(test1
(test2

Even though specify only one \\t and then ":", it still matches on two \\t and the ":".即使只指定一个 \\t 和 ":",它仍然匹配两个 \\t 和 ":"。 If I instead match on two \\t it only matches the second line which has two \\t.如果我改为匹配两个 \\t,它只会匹配具有两个 \\t 的第二行。

user@lab-client:~$ cat tabmatch.input |awk '/\t\t: \(test/ {  {print $2} }'
(test2

I have looked around but not found why \\t matches several \\t, or how to make it only match one.我环顾四周,但没有发现为什么 \\t 匹配多个 \\t,或者如何使它只匹配一个。

Other attempts I have made are:我所做的其他尝试是:

user@lab-client:~$ cat tabmatch.input |awk '/[\t]: \(test/ {  {print $2} }'
(test1
(test2

user@lab-client:~$ cat tabmatch.input |awk '/[\t]?: \(test/ {  {print $2} }'
(test1
(test2

Both of your lines match the pattern.你的两条线都匹配模式。

If you want to only match one tab from the start of the line, then you need to add an anchor ^ :如果您只想匹配行首的一个选项卡,则需要添加一个锚点^

awk '/^\t: \(test/ { print $2 }' tabmatch.input

I removed the inner curly braces as they weren't doing anything useful.我删除了内部大括号,因为它们没有做任何有用的事情。

Bear in mind that awk can read files all by itself so you don't need to pipe data to it using cat.请记住,awk 可以自己读取文件,因此您无需使用 cat 将数据通过管道传输到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM