I'm trying to clean up a log file for better readability - there's a load of un-necessary stuff there to my needs - i basically have to replace sshd[xxxx] where xxxx is an arbitrary number with, well, either a space or nothing. While i can replace a known string, i have no idea how to do a sed wildcard in this case- So- how do i do that?
2 Answers
its not clear whether you want the replace the "sshd" as well, so i assumed you it is.
sed 's/sshd\[[0-9]*\]//g' file
otherwise,
sed 's/sshd\[[0-9]*\]/sshd[]/g' file
You are looking to replace all numbers after sshd
inside []
with nothing. To select numbers you can use a range [0-9]\+
, where [0-9]
matches any digit and \+
means one or more of that before.
So for your case the regexp to replace numbers with zeros would be
sed -r s/\[[0-9]\+\]/\[\]/
where the -r
enables extended regexp's like the one above. The \[
and \]
are just to escape the [
and ]
so they don't get interpreted by sed
.
For regexp operators like \+
have a look at this section in the gawk
manual, for character list like [0-9]
see this section -- most of what you see there applies to sed
as well.
-
shouldn't it be
sed -r s/\[[0-9]\+\]/\[\]/
, in order to keep the [] without the number?– SnarkMar 19, 2010 at 6:26 -
-
1You can omit the
-r
if you quote the command. If you do both (use-r
and quote the command), you can omit the escaping of the +.sed 's/\[[0-9]\+\]/\[\]/'
orsed -r 's/\[[0-9]+\]/\[\]/'
Often, when there are grouping parentheses, using-r
with quoting can save several characters and improve readability since the parentheses don't need to be escaped. Mar 19, 2010 at 9:08 -