Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed - replace second octet on an IP address

Tags:

bash

sed

awk

I would like to replace the second octet on an IP address using sed:

Example:

10.110.30.11

10.133.30.11

What is the easiest way to perform this task?

Thank you

like image 273
Mark Wallace Avatar asked Feb 06 '23 21:02

Mark Wallace


2 Answers

An attempt in pure bash, apart from wonderful awk and sed answers from above.

 $ IFS="." read -r octet1 _ octet3 octet4 <<<"10.110.30.11"
 $ octet2=133
 $ printf "%s.%s.%s.%s\n" "$octet1" "$octet2" "$octet3" "$octet4"
 10.133.30.11
like image 168
Inian Avatar answered Feb 08 '23 16:02

Inian


With GNU sed:

sed 's/\.[0-9]\+\./.133./' file
like image 39
Cyrus Avatar answered Feb 08 '23 15:02

Cyrus