Sed C \ command, IP address and hexadecimal number conversion, and other wonderful shell code

Source: Internet
Author: User
1. the first is the c \ command in sed. The difference between this command and S/XXX/xxxx/g is that C \ can replace all the rows found by the current sed with the specified text

For example, sed-I "/734/C \ hello" Install. Log, replace the entire line containing a string such as 734 with Hello

When using C \, note that if we add a variable instead of a constant string after the c \ command, we need to add a \ symbol to solve the reference, otherwise, the variable cannot be parsed properly:

Sed-I "/734/C \ $ hello" Install. log -- a row containing 734 is replaced with $ hello

Sed-I "/734/C \ $ hello" Install. log -- a row containing 734 is replaced with the value in the hello variable.

2. there are several good functions, namely converting the IP address to the hexadecimal format, converting the hexadecimal number to the IP address, judging the size of the two IP addresses, and finding the smallest IP address and the largest IP address in a bunch of IP addresses, the command printf is used here. This is a good thing and can be used to format the output. The following code shows printf "% 08x "...... format the following content into a hexadecimal number, with a maximum of 8 digits retained. Iptohex (){
Ifs =.
For STR in $1
Do
Printf "% 02x" $ Str
Done
}

Hextoip (){
Hex = $1
Printf "% d." 0x $ {HEX: 0: 2}
Printf "% d." 0x $ {HEX: 2: 2}
Printf "% d." 0x $ {HEX: 4: 2}
Printf "% d" 0x $ {HEX: 6: 2}
}

Iplarger (){
First = 0x $ (iptohex $1)
Second = 0x $ (iptohex $2)
First = $ (printf "% d" $ first)
Second = $ (printf "% d" $ second)
If [$ first-GT $ second]; then
Return 0
Else
Return 1
Fi
}

Ipsmaller (){
First = 0x $ (iptohex $1)
Second = 0x $ (iptohex $2)
First = $ (printf "% d" $ first)
Second = $ (printf "% d" $ second)
If [$ first-lt $ second]; then
Return 0
Else
Return 1
Fi
}

 

 

Find the smallest and largest IP addresses in a bunch of IP addresses (IPaddress is an array ):

 

Ip_low =$ {IPaddress [1]}
Ip_high = $ ip_low
For ipnow in $ {IPaddress [@]}
Do
If ismasked $ ipnow $ localip $ localmask
Then
If ipsmaller $ ipnow $ ip_low
Then
Ip_low = $ ipnow
Fi
If iplarger $ ipnow $ ip_high
Then
Ip_high = $ ipnow
Fi
Else
Echo IPs not mask
Exit
Fi
Done

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.