fungus at aros.net
Thu Dec 14 12:24:36 MST 2006
> I am trying to remove the second occurance of the match. All the
> documentation I read says this is how you do it. For me it either is
> global or does not work. Is there a different way of doing this?
> Would awk be better?
> This some relavent lines from my hosts.deny file:
> === snipit ===
> === snipit ===
> sed -e 's/ALL:211\.94\.73\.199//' /etc/hosts.deny
This isn't working, because you are using the wrong sed command.
Think about what you want to happen (delete lines). You are using s
which is for search/replace. You should be using d for deleting lines.
But since you want to just delete all lines after the first, you need to
get a bit more complicated with sed. h (copy) and g (paste). FYI:
using /1 or /2 indicates the occurrence of the pattern in the line, not
the occurrence of the line in the file.
Here the sed script specifies a pattern (the IP address), and uses
braces to signify multiple commands per line that matches this pattern.
h tells sed to copy the line into the clipboard. d deletes the line.
$ means only apply this command to the very last line. G means paste
the clipboard *after* the current line. (g would mean paste the
http://www.grymoire.com/Unix/Sed.html is a pretty good tutorial on more
As you can see, sed is very powerful, and does a whole lot more than
just search/replace. However, it isn't the best tool for everything.
Your case is an example where sed will work, but it is definitely *not*
the best approach. As others have mentioned, since you really want to
just remove duplicates, using sort|uniq, or even sort -u is a much
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 3589 bytes
Desc: S/MIME Cryptographic Signature
Url : http://plug.org/pipermail/plug/attachments/20061214/3061bfcb/attachment.bin
More information about the PLUG