This is more a sequence of commands than it is a regular expression, but I suppose breaking the sequence down may be instructive.
Read the manpage on ifconfig
to find this
Optionally, the -a flag may be used instead of an interface name. This
flag instructs ifconfig to display information about all interfaces in
the system. The -d flag limits this to interfaces that are down, and
-u limits this to interfaces that are up. When no arguments are given,
-a is implied.
That's one part done. The pipe (|
) sends what ifconfig
would normally print to the standard output to the standard input of sed
instead.
You're passing sed
the option -E
. Again, man sed
is your friend and tells you that this option means
Interpret regular expressions as extended (modern) regular
expressions rather than basic regular expressions (BRE's). The
re_format(7) manual page fully describes both formats.
This isn't all you need though... The first string that you're giving sed
lets it know which operation to perform.
Search the same manual for the word "substitute" to reach this
paragraph:
[2addr]s/regular expression/replacement/flags
Substitute the replacement string for the first instance of
the regular expression in the pattern space. Any character other than
backslash or newline can be used instead of a slash to delimit the RE
and the replacement. Within the RE and the replacement, the RE
delimiter itself can be used as a literal character if it is preceded
by a backslash.
Now we can run man 7 re_format
to decode the first command s/[[:space:]:].*//
which means "for each line passed to standard input, substitute the part matching the extended regular expression [[:space:]:].*
with the empty string"
[[:space:]:]
= match either a :
or any character in the character class [:space:]
.*
= match any character (.
), zero or more times (*
)
To understand the second command look for the [2addr]d
part of the sed
manual page.
[2addr]d
Delete the pattern space and start the next cycle.
Let's then look at the next command /^$/d
which says "for each line passed to standard input, delete it if it corresponds to the extended regex ^$
"
^$
= a line that contains no characters between its start (^
) and its end ($
)
We've discussed how to start with man pages and follow the clues to "decode" commands you see in everyday life.