简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM