sed woes
Lonnie Olson
fungus at aros.net
Thu Dec 14 12:24:36 MST 2006
Daniel wrote:
> 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 ===
> ALL:125.206.122.49
> ALL:211.94.73.199
> ALL:213.215.228.250
> ALL:213.215.228.250
> ALL:211.94.73.199
> ALL:211.94.73.199
> ALL:71.213.9.247
> === 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.
sed '/211\.94\.73\./{
h
d
}
$G' /etc/hosts.deny
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
clipboard instead)
http://www.grymoire.com/Unix/Sed.html is a pretty good tutorial on more
advanced sed.
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
better approach.
--lonnie
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3589 bytes
Desc: S/MIME Cryptographic Signature
Url : http://plug.org/pipermail/plug/attachments/20061214/3061bfcb/smime.bin
More information about the PLUG
mailing list