Skip to content

refactor: simplify sed/awk/grep uses, etc.#1574

Open
akinomyoga wants to merge 6 commits intoscop:mainfrom
akinomyoga:sed-6
Open

refactor: simplify sed/awk/grep uses, etc.#1574
akinomyoga wants to merge 6 commits intoscop:mainfrom
akinomyoga:sed-6

Conversation

@akinomyoga
Copy link
Collaborator

No description provided.

The variable "prefix" is explicitly initialized when it is declared,
so we do not need to explicitly set it to an empty string on this
line.
The current implementation does not work for several reasons.

* First, for the selection "|" to work, one needs to enclose the
  entire pattern with "@()". The option argument of the "-X" option is
  not a regular expression, but an extended glob pattern.
* Next, the option argument of the "-X" option should not be singly
  quoted in the present case because command substitutions in the
  option argument of "-X" is not going to be expanded.
* Also, one also wants to quote the word of the here string.  In Bash
  < 4.4, the word of the here string is subject to word splitting, so
  it may be affected by IFS.

In addition, we actually do not need to use the "tr" command.  We can
reduce the spawn cost by using "${words[*]}" with an appropriate value
of IFS.
"_comp_cmd_tshark__{prefs,protocols}" are mutable global variables
used to cache the prefixes and protocols, so they should have "_mut_"
in the variable names.
options=$(module help 2>&1 | command grep -E '^[[:space:]]*\+' |
_comp_awk '{print $2}' | command sed -e 's/|/ /g' | sort)
options=$(module help 2>&1 | _comp_awk '/^[[:space:]]*\+/ {
$0 = $2; gsub(/\|/, " "); print }' | sort)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a nagging feeling that there is an awk implementation in existence that does not support gsub, but I can't find it right now and gsub is in POSIX at least down to 2008, so maybe it's all good.

There is one gsub use in the tree in chronyc, but that could be fine if we assume chronyc is Linux specific.

Copy link
Collaborator Author

@akinomyoga akinomyoga Feb 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't that Solaris awk? Solaris <= 11.3 awk indeed doesn't have gsub:

$ awk 'gsub(/a/, "b")' <<< "aaazzz"
awk: syntax error near line 1
awk: bailing out near line 1

We use /usr/xpg4/bin/awk (which has gsub) in Solaris through the wrapper _comp_awk, so the problem doesn't happen in Solaris.

I don't have AIX, NetBSD, and OpenBSD, but online manuals seem to suggest that their awk has gsub.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants