Marcox
Marcox

Reputation: 102

substituting a dot (.) with a space (" ") awk, sed or grep

I need to go through tcpdump files which have IP addresses followed by their source or destination port in this way: 192.168.1.0.80 to this one: 192.168.1.0 80.

How can I do this using awk, sed or grep?

Upvotes: 0

Views: 266

Answers (1)

Gilles Quénot
Gilles Quénot

Reputation: 185570

With sed :

tcpdump -v -n |
sed -r 's@([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\.([0-9]{1,5})@\1 \2@g'

EXPLANATION

  • -r switch stands for extented regex (I use it to avoid parentheses backslash)
  • s@@@ is a substitution skeleton, the delimiter can be anything we want, not only s///. s/before/after/
  • ( group and capture to \1 (to \N)
  • [0-9]{1,3} any character of: '0' to '9' (between 1 and 3 times (matching the most amount possible))
  • \. a literal '.'
  • ) end of a capture
  • \1 and \2 are the captured stuff
  • g modifier stands for all occurences

Upvotes: 2

Related Questions