How to combine 2 bash script into 1?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to combine 2 bash script into 1?
# 1  
Old 12-04-2014
How to combine 2 bash script into 1?

Hi all, I’ve quick question on bash scripting.

Here is my initial input. This is just an example. The value could be different numbers/ip addresses.

host.txt
Quote:
1.1.1.1
1.1.1.3
1.1.1.5
Final output should be like this.

output-final.txt
Quote:
create host_plain host1.1.1.1
modify network_objects host1.1.1.1 ipaddr 1.1.1.1
update network_objects host1.1.1.1
create host_plain host1.1.1.3
modify network_objects host1.1.1.3 ipaddr 1.1.1.3
update network_objects host1.1.1.3
create host_plain host1.1.1.5
modify network_objects host1.1.1.5 ipaddr 1.1.1.5
update network_objects host1.1.1.5
In order to have this, I’ve 2 different scripts.
The first one is just to append host in front of the ip.

script-1
Quote:
#!/bin/bash
awk '{
print "host"$1" "$1;
}' host.txt > output-1.txt
Second script is to create my final output.

script-2
Quote:
#!/bin/bash
awk '{
print "create host_plain " $1;
print "modify network_objects "$1" ipaddr " $2;
print "update network_objects "$1;
}' output-1.txt > output-final.txt
My question: Is it possible to combine this into one script so that I don’t have to run 2 different scripts?
If there is better way to do this, please let me know. Thanks in advance.

Last edited by type8code0; 12-04-2014 at 10:08 AM..
# 2  
Old 12-04-2014
Try to extend your second script:
Code:
awk     '       {print "create host_plain host" $1;
                 print "modify network_objects host"$1" ipaddr " $1;
                 print "update network_objects host"$1;
                }
        ' file
create host_plain host1.1.1.1
modify network_objects host1.1.1.1 ipaddr 1.1.1.1
update network_objects host1.1.1.1
create host_plain host1.1.1.3
modify network_objects host1.1.1.3 ipaddr 1.1.1.3
update network_objects host1.1.1.3
create host_plain host1.1.1.5
modify network_objects host1.1.1.5 ipaddr 1.1.1.5
update network_objects host1.1.1.5

This User Gave Thanks to RudiC For This Post:
# 3  
Old 12-04-2014
Does this do what you want?

Code:
IPS=$(echo 1.1.1-{1,3,5})
OUT=$HOME/tmp/output-final.txt

for IP in $IPS
do
	echo "create host_plain host $IP
modify network_objects host$IP ipaddr $IP
update network_objects host$IP
" >> $OUT
done

EDIT:
Ops, since the data is in a file:
Code:
OUT=output-final.txt
input_file=host.txt

while read IP
do
	echo "create host_plain host $IP
modify network_objects host$IP ipaddr $IP
update network_objects host$IP
" >> $OUT
done<$input_file

Hope this helps
This User Gave Thanks to sea For This Post:
# 4  
Old 12-04-2014
Thanks guys for your prompt response. I really appreciate it. I forgot to mention on my first post that value of host.txt is just an example. It could be different ip addresses depending on what kind of ip addresses requested by user. Let me provide another set of ip addresses.

host.txt
Quote:
10.5.3.8
172.16.3.210
192.168.0.150
192.168.7.4
output-1.txt
Quote:
host10.5.3.8 10.5.3.8
host172.16.3.210 172.16.3.210
host192.168.0.150 192.168.0.150
host192.168.7.4 192.168.7.4
output-final.txt
Quote:
create host_plain host10.5.3.8
modify network_objects host10.5.3.8 ipaddr 10.5.3.8
update network_objects host10.5.3.8
create host_plain host172.16.3.210
modify network_objects host172.16.3.210 ipaddr 172.16.3.210
update network_objects host172.16.3.210
create host_plain host192.168.0.150
modify network_objects host192.168.0.150 ipaddr 192.168.0.150
update network_objects host192.168.0.150
create host_plain host192.168.7.4
modify network_objects host192.168.7.4 ipaddr 192.168.7.4
update network_objects host192.168.7.4
Quote:
me@box:~/dbedit$ cat host.txt
10.5.3.8
172.16.3.210
192.168.0.150
192.168.7.4
me@box:~/dbedit$
me@box:~/dbedit$./script-1
me@box:~/dbedit$
me@box:~/dbedit$ cat output-1.txt
host10.5.3.8 10.5.3.8
host172.16.3.210 172.16.3.210
host192.168.0.150 192.168.0.150
host192.168.7.4 192.168.7.4
me@box:~/dbedit$
me@box:~/dbedit$./script-2
me@box:~/dbedit$
me@box:~/dbedit$ cat output-final.txt
create host_plain host10.5.3.8
modify network_objects host10.5.3.8 ipaddr 10.5.3.8
update network_objects host10.5.3.8
create host_plain host172.16.3.210
modify network_objects host172.16.3.210 ipaddr 172.16.3.210
update network_objects host172.16.3.210
create host_plain host192.168.0.150
modify network_objects host192.168.0.150 ipaddr 192.168.0.150
update network_objects host192.168.0.150
create host_plain host192.168.7.4
modify network_objects host192.168.7.4 ipaddr 192.168.7.4
update network_objects host192.168.7.4
me@box:~/dbedit$
Instead of having 2 different scripts to get the final output, is it possible to combine them so that I can run a single script to produce the final output from the initial input?

Last edited by type8code0; 12-04-2014 at 10:26 AM..
# 5  
Old 12-04-2014
Did you run the proposals against your new host.txt? As "single script"? What be the result?
# 6  
Old 12-04-2014
Quote:
Is it possible to combine this into one script so that I don’t have to run 2 different scripts?
Yes. E.g.
Code:
#!/bin/bash

awk '{
print "host"$1" "$1;
}' host.txt > output-1.txt 

awk '{
print "create host_plain " $1;
print "modify network_objects "$1" ipaddr " $2;
print "update network_objects "$1;
}' output-1.txt > output-final.txt

Quote:
If there is better way to do this, please let me know.
Yes. The awk command mentioned in posting #2 unifies your two awk commands. In fact, #3 does it too, just the other way.
This User Gave Thanks to junior-helper For This Post:
# 7  
Old 12-04-2014
Quote:
Originally Posted by junior-helper
Yes. E.g.
Code:
#!/bin/bash

awk '{
print "host"$1" "$1;
}' host.txt > output-1.txt 

awk '{
print "create host_plain " $1;
print "modify network_objects "$1" ipaddr " $2;
print "update network_objects "$1;
}' output-1.txt > output-final.txt

Yes. The awk command mentioned in posting #2 unifies your two awk commands. In fact, #3 does it too, just the other way.
Wow!!! This is perfect man. Many thanks Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to block first bash script until second bash script script launches web server/site?

I'm new to utilities like socat and netcat and I'm not clear if they will do what I need. I have a "compileDeployStartWebServer.sh" script and a "StartBrowser.sh" script that are started by emacs/elisp at the same time in two different processes. I'm using Cygwin bash on Windows 10. My... (3 Replies)
Discussion started by: siegfried
3 Replies

2. Shell Programming and Scripting

Script to combine lines in a file

Greetings, I have large file with following format name1 name2 name3 name4 name5 name6 child7 child8 child9 <== there is leading blank space child10 child11 child12 <== there is leading blank space name13 name14 name15 name16 child17 child18... (6 Replies)
Discussion started by: rnnyusa
6 Replies

3. Shell Programming and Scripting

Korn shell Script to combine Two files in one

Hello All , I am new to this Forum, I am trying to write a script to combine two data files with 1 column in common and others columns are different . File1 Apple 29 tomatao 4 grapes 25 File2 Apple fruit tomatao veg grapes fruit other (3 Replies)
Discussion started by: gagan0119
3 Replies

4. UNIX for Dummies Questions & Answers

Combine Both Output from the awk Script

Hi, Is there anyway to combine output from the awk scripting. file01.txt: AUE_CHMOD AUE_CHOWN AUE_CHROOT AUE_CONNECT AUE_ACCEPT AUE_FCHOWN AUE_FCHMOD AUE_SETREUID AUE_SETREGID AUE_FCHROOT AUE_PFEXEC AUE_SETUID AUE_NICE AUE_SETGID (9 Replies)
Discussion started by: alvinoo
9 Replies

5. Shell Programming and Scripting

Combine several commands in a bash script

Hi all, I have large files with url-s ending on "|<number>" which is the Page Rank for the website as shown in the example below http://www.machinokairo.com/2012/05/post-39.html|2 I am using "grep" to sort out all url-s in a particular way: first, remove all ending on "|0" and write the... (9 Replies)
Discussion started by: georgi58
9 Replies

6. Shell Programming and Scripting

How to combine awk and bash commands in script ?

Dear friends, I am just trying write one script using 2 files one file will contain details like below #X SERVER X LOCATION URL="http://www.abcd.com" FILENAME="abc.txt" ID_NAME="myabc_xyz" SERVER_PATH="/usr/local/dummy/html/....." #Y SERVER Y LOCATION URL="http://www.xyz.com"... (10 Replies)
Discussion started by: Akshay Hegde
10 Replies

7. Shell Programming and Scripting

Combine two files using script

Hi please help me to combine below two files into one file file1 10.238.54.1 enk-ras-bng-cse-01 10.10.10.10 10.238.56.225 ngp-ras-bng-cto-01 10.10.10.10 file2 10.238.54.1 enk-ras-bng-cse-01 20.20.20.20 10.238.56.225 ngp-ras-bng-cto-01 20.20.20.20 Required output file ... (5 Replies)
Discussion started by: surender reddy
5 Replies

8. UNIX for Dummies Questions & Answers

Combine two files using shell script

I need to combine two files based on the content in first column and combine it into one file . For example : file1: A 10 B 20 C 30 D 40 File2: B 200 E 500 A 100 D 400 Need the output in this format: file 3 : column 1 Column 2 Column 3 A 10 100 B 20 ... (4 Replies)
Discussion started by: tsm2011
4 Replies

9. Shell Programming and Scripting

bash: combine arrays with weird substitution/references

Hi all. I'm trying to finish a bash script with the following elements: ARRAY="blah $ITEM blah blah" ARRAY="blah blah $ITEM blah bluh" #ARRAY="...." # ...the ARRAY elements represent a variable but defined # syntax and they're all hard-coded in the script. #(...) ITEMS='1.0 2.3... (2 Replies)
Discussion started by: yomaya
2 Replies

10. Shell Programming and Scripting

Combine file using shell script

Hi, i have 2 file 1.txt : 2.txt then i would the result become : can anybody help me.. regards, (1 Reply)
Discussion started by: justbow
1 Replies
Login or Register to Ask a Question