简体   繁体   中英

Caret regexp produces no output in mawk

I am trying to print all files in /usr/bin/ where the filename starts with a v . This works,

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

Surprisingly, this returns no output,

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 .

Edit: I understand that awk may not be the best way to accomplish what I am wanting to do here. But, this is an exercise in learning awk by testing my understanding via comparing it to the real 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:

$ 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.


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:

$ 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
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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