Swapping IP Addresses In A File With Those From Another File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Swapping IP Addresses In A File With Those From Another File
# 1  
Old 01-20-2011
Swapping IP Addresses In A File With Those From Another File

Hello,
I'm looking for a way to change the IP addresses in a large host file with those of matching hostnames in another file.

For example, I have a file with

file1 contains lines like:
Code:
192.168.0.55    hostname hostname.network.lan
192.168.0.52    junkhost junkhost.network.dev
192.168.0.99    newhost-ab

and file2 has:
Code:
10.12.1.6          hostname hostname.network.lan
192.168.0.52    junkhost junkhost.network.dev
10.155.1.9        newhost-ab newhost-ab.network.new

In reality, file1 has about 800 lines, and file2 has about 100 lines, and I need to change the IP address in file1 with those new ones in file2 for the hosts that match.

I'm looking at using just sh or ksh with sed/awk, but can't seem to get the logic straight. It would be easy enuf if it were just changing one entry with sed and redirecting to new a file, but when it comes to changing 100+ entries in a file, not sure how to approach it. This is on Solaris.

Any and all help appreciated.

Thanks.
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 01-20-2011 at 02:25 PM.. Reason: code tags, please!
# 2  
Old 01-20-2011
Code:
nawk 'FNR==NR{f2[$2]=$1;next} $2 in f2 {$1=f2[$2]}1' file2 file1

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 01-26-2011
Thanks vgersh99. That got me off and running.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script (sh file) logic to compare contents of one file with another file and output to file

Shell script logic Hi I have 2 input files like with file 1 content as (file1) "BRGTEST-242" a.txt "BRGTEST-240" a.txt "BRGTEST-219" e.txt File 2 contents as fle(2) "BRGTEST-244" a.txt "BRGTEST-244" b.txt "BRGTEST-231" c.txt "BRGTEST-231" d.txt "BRGTEST-221" e.txt I want to get... (22 Replies)
Discussion started by: pottic
22 Replies

2. Shell Programming and Scripting

Swapping of fields in file

Hi Friends , I have file1.txt 1|b|46|123|47673|348738 2|c|63|124|7346|4783 3|y|45|125|5555|78789 output should swap the 4th field to the first field. output 123|1|b|46|47673|348738 124|2|c|63|7346|4783 125|3|y|45|5555|78789 (3 Replies)
Discussion started by: i150371485
3 Replies

3. UNIX for Dummies Questions & Answers

Swapping the columns of a text file for a subset of rows

Hi, I'd like to swap the columns 1 and 2 of a space-delimited text file but only for the first 1000 rows. How do I go about doing that? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

4. Shell Programming and Scripting

Creating a sed script to change ip addresses in a file

So I'm new to this sed command and I am trying to create a script that replaces ip addresses when I name a file but can't tweak it to work. Here is what it looks like: #!/bin/bash # file=$1 # sed -e 's/-CPUaddr 10.30.10.166/-CPUaddr 10.30.10.151/g' -i "$file" sed -e 's/-CPUaddr... (10 Replies)
Discussion started by: uradunce
10 Replies

5. Shell Programming and Scripting

Extract list of IP addresses from a text file.

I have an xml file with IP addresses all over the show. I want to print only the IP addresses and cut off any text before or after the IP address. Example: Note: The IP addresses (x.x.x.x) do not consistently appear in the xml file as per the pattern below. Sometimes there are text before... (8 Replies)
Discussion started by: lewk
8 Replies

6. Shell Programming and Scripting

Extracting email addresses from a flat file

All, I have a flat file which contains an email address in every line. I am trying to find a way to extract all the email addresses delimited by comma (,). Can you please show me a way, it will be very helpful, thanks. (3 Replies)
Discussion started by: sed_beginner19
3 Replies

7. Shell Programming and Scripting

script to get all ip addresses of servers into a file

Hi all i need to create a script that pings every server in my range (0-254) adn then returns the values to a file? can anyone please help. i am working in the tcsh ( and yes i know how to ping ) but i dont know how to ping them all in one script without copying and pasting a 254 times? ... (1 Reply)
Discussion started by: brian112
1 Replies

8. Shell Programming and Scripting

Swapping strings in a file

Hi, How to swap two strings in a file ? Ex: "ABC" to be swapped with "XYZ" "ABC" and "XYZ" donot occur in a same line . String has "" Regards Tej (8 Replies)
Discussion started by: tej_89
8 Replies

9. Shell Programming and Scripting

Swapping of fields in a file

Hi I have afile with 15fields,say f1,f2....f15 delimited on comma. How can i swap the f1,f15 fields using unix shell commands or any script? Thanks (3 Replies)
Discussion started by: ksailesh
3 Replies

10. UNIX for Advanced & Expert Users

Clueless about how to lookup and reverse lookup IP addresses under a file!!.pls help

Write a quick shell snippet to find all of the IPV4 IP addresses in any and all of the files under /var/lib/output/*, ignoring whatever else may be in those files. Perform a reverse lookup on each, and format the output neatly, like "IP=192.168.0.1, ... (0 Replies)
Discussion started by: choco4202002
0 Replies
Login or Register to Ask a Question