Shell script to convert IP range csv into a list of single IP's


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to convert IP range csv into a list of single IP's
# 1  
Old 02-10-2016
Shell script to convert IP range csv into a list of single IP's

Hi All,

I am looking for some help to convert a csv with IP ranges in.. in the format e.g.


Code:
1.1.1.2, 1.1.1.5
2.1.1.10, 2.1.1.20

and would be looking to output as follows:
Code:
1.1.1.2
1.1.1.3
1.1.1.4
1.1.1.5
2.1.1.10
2.1.1.11

etc etc up to 2.1.1.20

I have tried a few google things out but so far no luck... i have never done any shell scripting before...

Thanks

Last edited by Scrutinizer; 02-10-2016 at 02:23 PM.. Reason: code tags
# 2  
Old 02-10-2016
Hello zippyzip,

Please use code tags for commands/codes/Inputs you are using into your posts as per forum rules. Could you please try following and let me know if this helps.
Code:
awk -F", " '{m=split($1, A,".");n=split($2, B,".");num=A[m];till_num=B[n];Q=A[1]"."A[2]"."A[3];for(;num<=till_num;num++){print Q OFS num}}'  Input_file

Output will be as follows.
Code:
1.1.1 2
1.1.1 3
1.1.1 4
1.1.1 5
2.1.1 10
2.1.1 11
2.1.1 12
2.1.1 13
2.1.1 14
2.1.1 15
2.1.1 16
2.1.1 17
2.1.1 18
2.1.1 19
2.1.1 20

Thanks,
R. Singh

Last edited by RavinderSingh13; 02-10-2016 at 10:53 AM..
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 02-10-2016
Thanks Ravinder gives me something to work with...
# 4  
Old 02-10-2016
Another awk:
Code:
awk '{for(i=$4; i<=$8; i++) print $1,$2,$3,i}' FS='[.]|, *' OFS=. file

Code:
1.1.1.2
1.1.1.3
1.1.1.4
1.1.1.5
2.1.1.10
2.1.1.11
2.1.1.12
2.1.1.13
2.1.1.14
2.1.1.15
2.1.1.16
2.1.1.17
2.1.1.18
2.1.1.19
2.1.1.20

# 5  
Old 02-10-2016
Given your IP range spans byte boundaries (like 10.1.1.251, 10.1.2.5), this might be of some interest:
Code:
while IFS=", " read IP1 IP2
  do    BIP1=$((0x$(printf "%02X" ${IP1//./ })));
        BIP2=$((0x$(printf "%02X" ${IP2//./ })));
        for ((I=BIP1; I<=BIP2; I++))
          do printf "%d.%d.%d.%d\n" $((I>>24)) $((I>>16&255)) $((I>>8&255)) $((I&255))
          done
  done < file
10.1.1.251
10.1.1.252
10.1.1.253
10.1.1.254
10.1.1.255
10.1.2.0
10.1.2.1
10.1.2.2
10.1.2.3
10.1.2.4
10.1.2.5

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Tabbed multiple csv files into one single excel file with using shell script not perl

Hi Experts, I am querying backup status results for multiple databases and getting each and every database result in one csv file. so i need to combine all csv files in one excel file with separate tabs. I am not familiar with perl script so i am using shell script. Could anyone please... (4 Replies)
Discussion started by: ramakrk2
4 Replies

2. Shell Programming and Scripting

Convert list of numbers to text range

Hi, I'd like to take a list of numbers (with a prefix) and convert to a range, for example: cn001 cn004 cn016 cn017 cn018 cn019 cn020 cn021 cn031 cn032 cn038 cn042 cn043 cn044 cn045 (5 Replies)
Discussion started by: chrissycc
5 Replies

3. Shell Programming and Scripting

Convert XML to CSV using awk or shell script

Hello, I am working on a part of code where I need a awk or shell script to convert the given XML file to CSV or TXT file. There are multiple xml files and of different structure, so a single script is required for converting data. I did find a lot of solutions in the forum but... (16 Replies)
Discussion started by: Rashmitha
16 Replies

4. Shell Programming and Scripting

How to create or convert to pdf files from csv files using shell script?

Hi, Can anyone help me how to convert a .csv file to a .pdf file using shell script Thanks (2 Replies)
Discussion started by: ssk250
2 Replies

5. Shell Programming and Scripting

Convert a two-column list into a csv

Hi experts, I have a very large (1.5M lines), sorted but unstructured list that looks like this: process_nameA valueA process_nameA valueA ... process_nameB valueB process_nameB valueB ... process_nameN valueN I'd like to turn this into a csv. The values are all... (4 Replies)
Discussion started by: abercrom
4 Replies

6. Shell Programming and Scripting

Shell Script to convert multilines to single lines

Hi, I need to create an script which reads multi lines and convert it to single line or enter escape sequence at end "\". e.g. #!/bin/sh echo -e "provide me query to run" read query create table test( id int(11), name char); echo $query But it's failing because of multi line... (8 Replies)
Discussion started by: mirfan
8 Replies

7. Shell Programming and Scripting

script to convert CSV to SQL

I am trying to write a script to convert csv files into SQL format. What I am missing is: 1. handling of the first row and create it as a insert into format 2. better handling of any other row, and create a line with the data. *The while read line needs a better work. I thought of using awk. ... (1 Reply)
Discussion started by: saariko
1 Replies

8. Shell Programming and Scripting

Shell convert xls to csv

Hi does anybody know how to convert xls to csv undex linux. I need only data (that is log from test, dont need any macro and so on) from xls. Any idea how to do that? Perl? shell? Could you give me any example? Thanks in advance for answer. Gracjan (4 Replies)
Discussion started by: Gracjan
4 Replies

9. Shell Programming and Scripting

convert this into csv using awk/shell script

Hi Scripting gurus, I need to convert following text snippet into csv. please help Input heading1 = data1 heading2 = data2 .. .. heading n = data n heading 1 = data1 .. .. Output data1,data2,....,data n (3 Replies)
Discussion started by: azs0309
3 Replies

10. Shell Programming and Scripting

here-doc convert 2 script convert to single script?

I have my main script calling another script to retrive a "ls -alt" of a directory that's located in a remote location I'm sftping into. main.sh #!/bin/ksh getLS.sh > output.txt getLS.sh #!/bin/sh /home<..>/sftp <host@ip> <<! cd /some/dir/Log ls -alt quit ! Basically I'd like to be... (2 Replies)
Discussion started by: yongho
2 Replies
Login or Register to Ask a Question