How to combine 2 texts file and create another file from it?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to combine 2 texts file and create another file from it?
# 1  
Old 01-07-2013
How to combine 2 texts file and create another file from it?

Hi all,

I've the following hostnames & ip addresses saved in host_ip.txt

Quote:
H1 1.1.1.1
H2 2.2.2.2
H3 3.3.3.3
....
H99 99.99.99.99
Based on this list, I need to create the following script.

Quote:
create host_plain HH
modify network_objects HH ipaddr XXXX
update network_objects HH
where by HH is a hostname
and XXXX is an ip address

So the final output would be something like this.

Quote:
create host_plain H1
modify network_objects H1 ipaddr 1.1.1.1
update network_objects H1

create host_plain H2
modify network_objects H2 ipaddr 2.2.2.2
update network_objects H2

create host_plain H3
modify network_objects H3 ipaddr 3.3.3.3
update network_objects H3

....

create host_plain H99
modify network_objects H99 ipaddr 9.9.9.9
update network_objects H99
Currently, I'm doing it manually, replace, copy and paste. Since there are hundreds of Hosts & IP Addresses, I really appreciate if someone could help me to automate this task using a script or any methods. Thanks in advance
# 2  
Old 01-07-2013
Code:
awk '{
      print "create host_plain " $1;
      print "modify network_objects "$1" ipaddr " $2;
      print "update network_objects "$1;
      print " ";
}' host_ip.txt

This User Gave Thanks to Yoda For This Post:
# 3  
Old 01-07-2013
Quote:
Originally Posted by bipinajith
Code:
awk '{
      print "create host_plain " $1;
      print "modify network_objects "$1" ipaddr " $2;
      print "update network_objects "$1;
      print " ";
}' host_ip.txt

Thanks, it works like a charm. One more question. How to save this output, let say into output.txt?

Nevermind, found the answer Smilie
# 4  
Old 01-07-2013
Code:
awk '{
      print "create host_plain " $1;
      print "modify network_objects "$1" ipaddr " $2;
      print "update network_objects "$1;
      print " ";
}' host_ip.txt > output.txt

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

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Need to combine two lines in a file based on first character of each line in a file

Hi, I have a requirement where I need to combine two lines in a file based on first character of each line in a file. Please find the sample content of the file below: Code: _______________________ 5, jaya, male, 4-5-90, single smart 6, prakash, male, 5-4-84, married fair 7, raghavi,... (1 Reply)
Discussion started by: jayaP
1 Replies

2. UNIX for Dummies Questions & Answers

Need to combine two lines in a file based on first character of each line in a file

Hi, I have a requirement where I need to combine two lines in a file based on first character of each line in a file. Please find the sample content of the file below: Code: _______________________ 5, jaya, male, 4-5-90, single smart 6, prakash, male, 5-4-84, married fair 7, raghavi,... (1 Reply)
Discussion started by: jayaP
1 Replies

3. Shell Programming and Scripting

Serach a string for multiple occurrences in a file and cut related texts

Hi, I want a help in this forum for my below issue. 1. I have a file where I'm searching for a text . 2. When I get the line where the above string is present I want to cut some texts from the line where the particular string was found. These above two steps will repeat in the... (2 Replies)
Discussion started by: bhaski2012
2 Replies

4. UNIX for Dummies Questions & Answers

Searching for Multiple texts in a file

Hello guys, I hope anyone can help me with this ... I have a file in which i have around 6000 lines of same format text like 1234567 2345678 3456789 .................... Now what I have to do is that there I have to search these numbers in another file which contains hundreds of... (1 Reply)
Discussion started by: m_usmanayub
1 Replies

5. Shell Programming and Scripting

Two Huge Texts and Combine Result to Third

hi, i want to examine two file and write some codes to a third file. note that seperators are TAB, not space. first file: 192.168.1.1 3 192.168.1.2 2 192.168.3.2 2 192.168.7.3 1 ... second file: 192.168.1.1 1 10.15.1.1 3 30 10.15.2.1 2 40 192.168.1.1 2 10.23.4.5... (3 Replies)
Discussion started by: gc_sw
3 Replies

6. Shell Programming and Scripting

Help me to combine two file

Hi, can anybody help me. I want to combine these two file into one horizontally. 1st File: ... 10:00 10:01 10:02 10:03 10:04 10:05 ... 2nd File: ... 0715 10:00 Caps: 50 0715 10:01 Caps: 65 0715 10:02 Caps: 75 0715 10:04 Caps: 50 0715 10:05 Caps: 87 (3 Replies)
Discussion started by: lurak
3 Replies

7. Shell Programming and Scripting

How to write a shell script to notify when certain texts appear in a file?

I have a server and occasionally the file mysqld.log would show something like /usr/libexec/mysqld: Disk is full writing './example_com_-_wordpress/wp_statpress.MYD' (Errcode: 122). Waiting for someone to free space... Retry in 60 secs How do I write a simple shell script to check mysqld.log... (1 Reply)
Discussion started by: acider
1 Replies

8. Shell Programming and Scripting

Read a file and search a value in another file create third file using AWK

Hi, I have two files with the format shown below. I need to read first field(value before comma) from file 1 and search for a record in file 2 that has the same value in the field "KEY=" and write the complete record of file 2 with corresponding field 2 of the first file in to result file. ... (11 Replies)
Discussion started by: King Kalyan
11 Replies

9. Shell Programming and Scripting

Separate sentences from two texts, combine them

I havet two books, one in Swedish, the other in English. Two text files. I want to combine them into one, with each sentence having it's translation next to it. ------------ Text file one. Example sentence in English. Example 2 sentence 2 in English 2. -------------- Text file two. ... (2 Replies)
Discussion started by: LaraMej
2 Replies
Login or Register to Ask a Question