Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

If we have this string (IP address):

How can I derive the (DNS reverse record form) from this string, so it will be shown like using a shell script?

share|improve this question
It has to be with the shell? –  Braiam May 30 '14 at 22:07
Stop doing that –  Michael Mrozek Jun 8 '14 at 8:30
With this amount of answers this should better be in codegolf ;) –  tkausl Dec 6 '14 at 12:59
@tkausl, share it there if you want to :) –  Networker Dec 6 '14 at 23:58

14 Answers 14

up vote 25 down vote accepted

You can do it with AWK. There are nicer ways to do it, but this is the simplest, I think.

echo '' | awk 'BEGIN{FS="."}{print $4"."$3"."$2"."$1""}'

This will reverse the order of the IP address.

Just to save a few keystrokes, as Mikel suggested, we can further shorten the upper statement:

echo '' | awk -F . '{print $4"."$3"."$2"."$1""}'


echo '' | awk -F. '{print $4"."$3"."$2"."$1""}'


echo '' | awk -F. -vOFS=. '{print $4,$3,$2,$1,""}'

AWK is pretty flexible. :)

share|improve this answer
If you want to save a few keystrokes, -F . should be equivalent to BEGIN{FS="."}. –  Mikel May 30 '14 at 22:03
Thanks, it worked I will mark the question as answered –  Networker May 30 '14 at 22:04

Just for curiosity value... using tac from GNU coreutils: given a variable ip in the form then

$(printf %s "$ip." | tac -s.)


$ ip=
$ rr=$(printf %s "$ip." | tac -s.)
$ echo "$rr"
share|improve this answer
+1 This is definitely my favorite answer here. Thanks for introducing me to tac! –  Jonathon Reinhart May 31 '14 at 17:41
Thanks are also due to @StéphaneChazelas for the elegant printf edit (I originally posted an ugly echo -n) –  steeldriver Jun 1 '14 at 17:07

If you want to use only shell (zsh, ksh93, bash), here's another way:

IFS=. read w x y z <<<''
printf '' "$z" "$y" "$x" "$w"

Or in plain old shell:

echo '' | { IFS=. read w x y z; echo "$z.$y.$w.$"; }
share|improve this answer

Easily with Perl, thusly:

$ echo|perl -nle 'print join ".",reverse(split /\./,$_)'
share|improve this answer
Can be made even more compact: perl -F'\.' -lane '$,=".";print reverse @F' –  Joseph R. May 30 '14 at 22:15

To round it out, Ruby:

ruby -r ipaddr -e 'puts'

Which also supports IPv6

share|improve this answer
you could take the "require" out of the script: ruby -r ipaddr -e 'puts ...' –  glenn jackman Dec 5 '14 at 19:22

Through GNU sed,

sed -r 's/^([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})$/\4.\3.\2.\' file

It reverses any IPv4-address format.


$ echo '' | sed -r 's/^([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})$/\4.\3.\2.\'

$ echo '' | sed -r 's/^([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})$/\4.\3.\2.\'

$ echo '' | sed -r 's/^([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})$/\4.\3.\2.\'

$ sed -r 's/^([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})$/\4.\3.\2.\' <<< ''
share|improve this answer

With zsh:

$ ip=
$ echo ${(j:.:)${(s:.:Oa)ip}}

Those are variable expansion flags:

  • s:.:: split on .
  • Oa: reverse order the array
  • j:.:: join on .
share|improve this answer

Another possibility is to use the "dig" command line tool with the "-x" switch.

It actually does a request on the PTR entry, but if you filter on "PTR" it will show you one commented line (the request) and maybe some replies.

Using "dig" can be handy for a quick writing of the PTR name, without having to write a small script. Particularly if you need it interactively (to cut and paste the result). It works on IPv6 too.

share|improve this answer

If you want it working with IPv6 as well, you can use dig -x.

For example:

$ dig -x | egrep '^;.*PTR$' | cut -c 2- | awk '{print $1}'

$ dig -x 2001:db8:dc61:2a61::1 | egrep '^;.*PTR$' | cut -c 2- | awk '{print $1}'
share|improve this answer

Using Python’s standard library:

>>> ipaddress.ip_address('').reverse_pointer
share|improve this answer
This is in the 3.5 standard library and scheduled to be released September 2015. From a script you can do: python3.5 -c "import ipaddress; ipaddress.ip_address('').reverse_pointer" (all on one line) –  Anthon Nov 24 '14 at 20:40
$ while read ip
while> do
while> n=( $(echo $ip) ) && echo "${n[4]}"'.'"${n[3]}"'.'"${n[2]}"'.'"${n[1]}"'.'""
while> done

This way you can type in an address and hit return for your result.

share|improve this answer

With host command from dnsutils:

$ host -t ptr | cut -d' ' -f 2
share|improve this answer

In Python

 a = ""
 import re
 m ='(\d+)\.(\d+)\.(\d+)\.(\d+)',a)
 ip =,,,
 '.'.join(ip) + ""
share|improve this answer
Or more idiomatically, "{}".format(".".join(reversed(a.split(".")))) in Python2.7 –  1_CR Jun 8 '14 at 11:42
IFS=. ; set -- ${0+}
printf %b. $4 $3 $2 $1\\c

IFS=. ; printf %s\\n \ ${0+} |    
sed '1!G;$s/\n/./gp;h;d'

IFS=. ; printf '[%b.] ' \
    ${0+]PPPPP\\c} |dc
share|improve this answer

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.