Below is a list of hostnames that is stored in a text file.
web1.maxi.com
web3.maxi.com
web4.maxi.com
web5.maxi.com
web6.maxi.com
web7.maxi.com
web8.maxi.com
web9.maxi.com
web11.maxi.com
for the sake of displaying it shortly it needs to be parsed and rewritten/displayed as
web[1,3-9,11].maxi.com
can you guys help me on this, any suggestions will be helpful.
With perl you can use the
Set::IntSpanmodule for compressing the number sequence.The below solution can handle mixed and unordered site listings.
infile
parse.pl
Run it like this:
Output:
The perhaps cryptic
@numbers = $nums =~ /$pre(\d+)$post/gexpands to an array of items matched by the regular expression and saves it in@numbers.Note that this solution loads the whole file into memory.