Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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,

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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
...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading