Create and paste two file into one


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create and paste two file into one
# 1  
Old 01-31-2018
Create and paste two file into one

Hello
i want to create this list:
Code:
2a05:b80:0:235::9f/1159
2a05:b80:0:235::a0/1160
2a05:b80:0:235::a1/1161
2a05:b80:0:235::a2/1162
2a05:b80:0:235::a3/1163

so write this shell as well:
Code:
#Global VAR
STR=159
END=200

#INI NET IPV6 PART
SUM1=`expr $END - $STR`
for ((i=STR;i<=END;++i)); do
echo -e "2a05:b80:0:235"$x >> host-part.txt
done

#INI HOST IPV6 PART
STR2=159
END2=200
SUM2=`expr $END2 - $STR2`
for ((i=STR2;i<=END2;++i)); do
x=$( printf "%x" $i ) ; echo $x
echo -e "::"$x >> netpart.txt
done

if [ $SUM1 == $SUM2 ]; then
        paste -d '' host-part.txt netpart.txt > IP-LIST.txt

        ## creating file for port ranges ##
        STR3=1159
        END3=1200
        for ((i=STR3;i<=END3;++i)); do
        echo -e "/"$i >> port.txt
        done
        SUM3=`expr $END3 - $STR3`
                if [ $SUM1 == $SUM3 ]; then
                        paste -d '' IP-LIST.txt port.txt > final-config
                else
                        echo "Error on sum ip port"
                fi

it do what i want but the question is how can i ask user to enter number instead of having three SRT and END variable:
STR STR2 STR3 END END2 END3

and how can i make it More functional and principled?


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 01-31-2018 at 12:21 PM.. Reason: Added CODE tags.
# 2  
Old 01-31-2018
Some comments:
- in your host-part.txt creation loop, you're using the undefined $x variable.
- the final fi is missing for the first if construct, and there is no error handling (else branch) either.


EDIT: How about
Code:
read -p "enter STR1 END1 STR2 END2: " STR1 END1 STR2 END2 
if (( END2 - STR2 != END1 - STR1 ));  then echo different ranges.; fi
DLT=$((STR2 - STR1))
for ((i=STR1;i<=END1;++i))
  do    printf "2a05:b80:0:235::%X/%d\n" $i $((i+DLT))
  done

enter STR1 END1 STR2 END2: 159 200 1159 2000
2a05:b80:0:235::9F/1159
2a05:b80:0:235::A0/1160
2a05:b80:0:235::A1/1161
2a05:b80:0:235::A2/1162
2a05:b80:0:235::A3/1163
.
.
.


Last edited by RudiC; 01-31-2018 at 01:41 PM..
# 3  
Old 01-31-2018
tx
but output is just a line:
Code:
[root@localhost ~]# sh mm
enter STR1 END1 STR2 END2:159 200 1159 2000
2a05:b80:0:235::0/0

# 4  
Old 01-31-2018
My fault, sorry; I used a line from your scipt above instead of mine.
Use
Code:
for ((i=STR1;i<=END1;++i))

in the for loop.
# 5  
Old 01-31-2018
Or, if your shell (which, btw, you fail to mention) allows for "process substitution" ( so - don't use sh to run the script!):

Code:
printf "2a05:b80:0:235::%X/%d\n" $(paste <(seq $STR1 $END1) <(seq $STR2 $END2))
2a05:b80:0:235::9F/1159
2a05:b80:0:235::A0/1160
2a05:b80:0:235::A1/1161
2a05:b80:0:235::A2/1162
2a05:b80:0:235::A3/1163

# 6  
Old 01-31-2018
still oneline is output of
Code:
read -p "enter STR1 END1 STR2 END2:"
if (( END2 - STR2 != END1 - STR1 ));  then echo different ranges.; fi
DLT=$((STR2 - STR1))
#for ((i=STR1;i<=END1;++i))
for ((i=STR1;i<=END1;++i))
  do
printf "2a05:b80:0:235::%X/%d\n" $i $((i+DLT))
$END2))
  done

let me say again what this script do:
this script try to get range of ip and range of port then point each port to ip and save them into one file,
2a05:b80:0:235::XX
is the main ip and this script try to create list of ipv6

my code try to create list of ips then list of ports then paste them together into one file,

---------- Post updated at 04:24 PM ---------- Previous update was at 01:15 PM ----------

oh,
Code:
printf "2a05:b80:0:235::%X/%d\n" $i $((i+DLT))

%X >>>> %x
# 7  
Old 01-31-2018
Quote:
Originally Posted by nimafire
still oneline is output of
Code:
read -p "enter STR1 END1 STR2 END2:"
if (( END2 - STR2 != END1 - STR1 ));  then echo different ranges.; fi
DLT=$((STR2 - STR1))
#for ((i=STR1;i<=END1;++i))
for ((i=STR1;i<=END1;++i))
  do
printf "2a05:b80:0:235::%x/%d\n" $i $((i+DLT))
$END2))
  done

First line is missing list of variables to read, replace line 1 with:

Code:
read -p "enter STR1 END1 STR2 END2:" STR1 END1 STR2 END2

Remove line highlighted in red


Edit: Below is a example using bash with user friendly prompting (entering IPs in Hex with validation):

Code:
#!/bin/bash
hex_to_dec() {
  [[ $2 =~ ^([[:xdigit:]])+$ ]] &&
  printf -v $1 "%d" $((16#$2)) &&
  (($1 <= 16#FFFF ))
}
while true; do
   read -p "Start IP in hex: " sx
   hex_to_dec STR1 $sx && break
   echo "Invalid Start IP please reenter"
done
while true; do
   read -p "End IP in hex: " ex
   hex_to_dec END1 $ex && ((END1 >= STR1)) && break
   echo "Invalid End IP please reenter"
done
read -p "Start port: " STR2

DLT=$((STR2 - STR1))
for ((i=STR1;i<=END1;++i))
do
    printf "2a05:b80:0:235::%x/%d\n" $i $((i+DLT))
done


Last edited by Chubler_XL; 01-31-2018 at 07:08 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to create a report using paste or with awk

Input file will be Name: serve1 has disk :Yes dev (8): Name: serve2 has disk :No dev (8): Name: serve3 has disk :No Name: serve4 has disk :Yes dev (8): Need output like that. I was using pate -d, - - - . But that need all the line in same format in this some server it has... (4 Replies)
Discussion started by: ranjancom2000
4 Replies

2. Shell Programming and Scripting

How to cut a pipe delimited file and paste it with another file to form a comma separated outputfile

Hello ppl I have a requirement to split (cut in unix) a file (A.txt) which is a pipe delimited file into A1.txt and A2.txt Now I have to join (paste in unix) this A2.txt with external file A3.txt to form output file A4.txt which should be CSV (comma separated file) so that third party can... (25 Replies)
Discussion started by: etldev
25 Replies

3. Shell Programming and Scripting

Use of the PASTE command in a script file

Hi, When I use the paste command in the terminal window it works fine, but when i try to use it in a bash script file i get errors. I'm not sure how to use the paste command in a bash script file. my paste command looks like this paste <( code1 ) <(code2) thanks (7 Replies)
Discussion started by: eboye
7 Replies

4. OS X (Apple)

Paste text into vi file?

Hello, I'm running OS X 10.7.4. How can I paste text from the pasteboard into an open vim file? Thanks! (21 Replies)
Discussion started by: palex
21 Replies

5. Shell Programming and Scripting

Cut and paste data in new file

HI Guys, I have file A: Abc XyZ Abc Xyz Kal Kaloo Abc XyZ Abc Xyz Kalpooo Abc XyZ Abc Xyz Kloo Abc Abc Klooo I want file B Abc XyZ Abc Xyz Kal Kaloo Abc XyZ Abc Xyz Kalpooo Abc XyZ Abc Xyz Kloo File A is now 1 lines Abc Abc Klooo Cut all lines which have xyz... (2 Replies)
Discussion started by: asavaliya
2 Replies

6. Shell Programming and Scripting

Help required the cut the whole contents from one file and paste it into new file

Hi, First of all sincere apologies if I have posted in a wrong section ! Please correct me if I am wrong ! I am very new to UNIX scripting. Currently my problem is that I have a code file at the location /home/usr/workarea/GeneratedLogs.log :- Code :- (Feb 7, 571 7:07:29 AM),... (4 Replies)
Discussion started by: acidburn_007
4 Replies

7. Shell Programming and Scripting

How do you paste >> into a file in a terminal?

how do you paste >> into a file in a terminal? it keeps pasting as <<EOF EOF i just want it to show << i have a list of mysql commands in a text file and i want to paste it into another file which i'm using the terminal to edit thanks! (4 Replies)
Discussion started by: vanessafan99
4 Replies

8. Shell Programming and Scripting

Paste second columns from files to its corresponding file

Hi All, I have two sets of files. One set with extension .txt This set has file names with numbers like these. 1.txt, 2.txt, 3.txt until extactly 100.txt. The .txt files look like these: 0.38701788 93750 0.38622013 94456 0.38350296 94440 0.38282126 94057 0.38282126 94439 0.35847232... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

9. UNIX for Dummies Questions & Answers

Cut paste from one file to other

Hello, I am working on unix for the first time. I have to write a shell script where i want to cut paste from one file to other. File "1234.abc" is 03,12345555 16,936,x,x,120 16,936,x,x,100 49,12345555 03,12347710 16,936,x,x,115 16,936,x,x,122 49,12347710 03,12342222... (9 Replies)
Discussion started by: swapsb
9 Replies

10. Shell Programming and Scripting

Paste content of a file to another file and make it as columned

Pls help me on this. I have to 2 files like shown below: File 1 TAIJM AXPKIM BEMGW File 2 PXMPA JYGE IMJP What i want to do is to paste both file to a new file on thir format: File 3 TAIJM PXMPA AXPKIM JYGE BEMGW IMJP I tried cat and print, but it doesn't work. Cn... (6 Replies)
Discussion started by: kingpeejay
6 Replies
Login or Register to Ask a Question