Page MenuHomeFreeBSD

Reduce "grep foo | awk" chain of pipes to "awk /foo/"
Needs ReviewPublic

Authored by bcr on Jun 29 2023, 4:45 PM.
Tags
None
Referenced Files
Unknown Object (File)
Mon, Apr 29, 6:46 AM
Unknown Object (File)
Fri, Apr 26, 12:59 AM
Unknown Object (File)
Mon, Apr 22, 9:43 AM
Unknown Object (File)
Sat, Apr 13, 9:44 PM
Unknown Object (File)
Dec 20 2023, 1:28 PM
Unknown Object (File)
Dec 20 2023, 4:19 AM
Unknown Object (File)
Dec 8 2023, 12:03 PM
Unknown Object (File)
Nov 28 2023, 7:06 PM

Details

Reviewers
None
Group Reviewers
Src Committers
Summary

This patch removes grep from passing data to awk via a pipe in favor of using awk's // (constant regular expression search) functionality.

Example:
Bad:
mount | grep zfs | awk '{ print $3 }'

Better (saving one pipe):
mount | awk '/zfs/ { print $3 }'

This occurs in a bunch of places, so I changed most of them. Others, where more complicated filters are used or negation is involved, I left out. This could be done in another pass.
I'm not sure how much this brings in terms of performance (possibly negligible). I think it aids readability, especially when there is a long chain of such pipes.

Test Plan

The scripts should produce the same results, I tested some of the grep | awk replacements and got the same output/values back.

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Skipped
Unit
Tests Skipped

Event Timeline

bcr requested review of this revision.Jun 29 2023, 4:45 PM
garga added inline comments.
tools/test/stress2/misc/all.debug.inc
33

Replacing grep -w by a simple regex /word/ doesn't seem correct

tools/test/stress2/misc/all.debug.inc
33

Maybe not in all instances, but the values are extracted with the same result. There does not seem to be a need for -w here. There are other examples below where a simple vmstat -m | grep foo without the -w is used. Could be the personal coding style of the author of that file.