Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a question regarding the use of some more advanced grep, awk, sed. I have a log file, for a proprietary MTA, that contains IP's in a string, delimited by [redacted]^~x.x.x.x^[redacted]. So far all of my grepping, awking, and sedding hasn't gotten me very far.

This log file has 331520 lines in it. My goal was to simply grep out the ip's, then do a for loop with sed, to sed 's/$i/redacted'. I'm including a sample of one of the log entries. If you all have any idea, I would be greatly appreciative.

Jun  4 15:21:52 mta-name: 13388^~88/CC-04671-FCA0DCF4^~D^~<redactedmessageid>^~@^~redacteduser@domain.tld^~redacted.hostname^~^~port^~esmtp^~^~external_routing_nobounce^~0^~0.51^~subjectofmessage^~250 2.6.0 <redactedmessageid> [InternalId=2178458] Queued mail for delivery    
share|improve this question
up vote 4 down vote accepted

Do you want to replace the IP addresses with the string "[redacted]"? There's no need to grep then sed in a loop. sed gives you a loop and "grepping" for free.

sed 's/\^~[[:digit:]]\+\(\.[[:digit:]]\+\)\{3\}/^~[redacted]^~/' logfile
share|improve this answer
Thank Dennis. That does it. very appreciative. – Harry Jun 6 '12 at 1:56

I figured this out. More man pages. A little modifying of my grep command with a -o showed me a list of the addresses, with sort | uniq, I was able to get a list

grep -o '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*' log.file | sort -n | uniq
share|improve this answer
That is not quite right since * matches zero-or-more and you want one-or-more so replace each * with + and use egrep or grep -E. You do not need the -n in sort (or if you do want it, you should extend it for each octet using -t and -k) and sort -u means you do not need uniq. – ramruma Jun 24 '12 at 5:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.