简体   繁体   English

插入符号正则表达式在 mawk 中不产生 output

[英]Caret regexp produces no output in mawk

I am trying to print all files in /usr/bin/ where the filename starts with a v .我正在尝试打印/usr/bin/中文件名以v开头的所有文件。 This works,这行得通,

ls -lA /usr/bin/ | awk '{print $9}' | grep ^v

Surprisingly, this returns no output,令人惊讶的是,这没有返回 output,

ls -lA /usr/bin/ | awk '/^v/ {print $9}' ls -lA /usr/bin/ | awk '/^v/ {print $9}' . ls -lA /usr/bin/ | awk '/^v/ {print $9}'

I don't understand the difference.我不明白其中的区别。 I am running Ubuntu 21.10 with awk -W version saying that it is on 1.3.4 20200120 .我正在运行Ubuntu 21.10awk -W version ,说它在1.3.4 20200120上。

Edit: I understand that awk may not be the best way to accomplish what I am wanting to do here.编辑:我知道awk可能不是完成我想要在这里做的事情的最佳方式。 But, this is an exercise in learning awk by testing my understanding via comparing it to the real output.但是,这是一个学习 awk 的练习,通过将其与真实的 output 进行比较来测试我的理解。

The difference between the two pipelines is that the first outputs the 9th column and then check to see if that starts with a v the second checks to see if the line starts with a v , change the second to:两个管道之间的区别在于,第一个输出第 9 列,然后检查它是否以v开头,第二个检查该行是否以v开头,将第二个更改为:

$ ls -lA /usr/bin/ | awk '$9 ~ /^v/ {print $9}'

When writing:写作时:

/pattern/ { ... }

it's the same as writing和写作一样

$0 ~ /pattern/ { ... }

but in your case you want to compare the 9th column, so write that instead.但是在您的情况下,您想比较第 9 列,因此请改写。


But you really don't want to create a pipeline for this, and what would happen if your files contain a space?但是您真的不想为此创建管道,如果您的文件包含空格会发生什么?

You can consider using find or globs instead:您可以考虑改用find或 glob:

$ printf '%s\n' /usr/bin/v*
/usr/bin/vi
/usr/bin/view
...

or或者

$ find /usr/bin -name 'v*' -print
/usr/bin/vi
/usr/bin/view
...

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

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