How to print a range of IP addresses with Linux seq command

26,674

Solution 1

Use a format:

$ seq -f "10.20.30.%g" 40 50
10.20.30.40
10.20.30.41
10.20.30.42
10.20.30.43
10.20.30.44
10.20.30.45
10.20.30.46
10.20.30.47
10.20.30.48
10.20.30.49
10.20.30.50

Unfortunately this is non-obvious as GNU doesn't like to write man pages.

Solution 2

There is the prips utility which generates an IP list from a range or CIDR. Useful for work with large ranges:

$ prips 10.0.0.20 10.0.0.23
10.0.0.20
10.0.0.21
10.0.0.22
10.0.0.23

$ prips 10.0.0.0/23
10.0.0.0
10.0.0.1
10.0.0.2
<...>
10.0.1.254
10.0.1.255

Solution 3

You can use sed command with seq to print range of IP address.

seq 2 23 | sed 's/^/10.0.0./'

OR using echo and tr

echo 10.0.0.{2..23} | tr ' ' '\n'

Solution 4

The printf command performs implicit iteration if it is given more arguments than conversion specifiers. For example:

$ printf "%s-%s\n" 1 2 3 4 5 6
1-2
3-4
5-6

There are two conversions, but six arguments. So three repetitions of the formatting logic occur, marching over the arguments pairwise.

With that we can do:

printf "10.0.0.%s\n" $(seq 1 23)

The printf command, and its repeating behavior, are POSIX standard: "The format operand shall be reused as often as necessary to satisfy the argument operands." On the other hand, the seq command isn't.

Solution 5

for i in $(seq 2 23); do echo "10.0.0.$i"; done
Share:
26,674

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin over 1 year

    How can I print a range of ip addresses on linux command line using the "seq" command? For eg: I need seq to print a range of ip from 10.0.0.1 to 10.0.0.23 . Seems like the period in between the octets causes the number to behave like a floating point . I am getting a "invalid floating point argument error" . I tried using the -f option . May be I am not using it correctly. But it still gave me an error. I am trying to something similar to

    seq 10.0.0.2 10.0.0.23
    

    Is there another way to print IP addresses in a range in Linux other than switching over to excel ?

  • Volkan Sevindik
    Volkan Sevindik over 9 years
    I think it would be better to reference the manual they do provide than only noting that the man page specifically is overly brief. See info coreutils 'seq invocation'.
  • Volkan Sevindik
    Volkan Sevindik over 9 years
    Absolutely, referencing man 3 printf as well is a good idea in case the reader is not familiar with printf format strings. That said, I'm not a big fan of their use of texinfo either but this was just my suggestion for improving your answer by making use of the documentation that does exist over just pointing out the lacking documentation in a specific format.
  • don_crissti
    don_crissti about 8 years
    "Why" wha​​​​t​​​​​​ ​ ?
  • user163009
    user163009 about 8 years
    the edit and downvote?
  • don_crissti
    don_crissti about 8 years
    I'm not the downvoter - most likely the system automatically downvoted your answer. I only reviewed it (edited it). As to why edit ? Well, because it's code and should be properly formatted.