sed woes

Lonnie Olson fungus at
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:
> ALL:
> ALL:
> ALL:
> ALL:
> ALL:
> ALL:
> === 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\./{ 
                              $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) 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.

-------------- 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 : 

More information about the PLUG mailing list