The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Content extract of a file using awk nr_shan Shell Programming and Scripting 5 12-19-2007 05:22 AM
How to insert text into first line of the file and middle of the file? ali hussain Shell Programming and Scripting 3 03-05-2007 05:54 AM
Insert file content to a file Manan UNIX for Advanced & Expert Users 2 12-09-2005 09:17 AM
transfer of specific file content to another file mem101 Shell Programming and Scripting 1 10-18-2005 02:01 PM
Insert file content via sed after two searchings nir_s Shell Programming and Scripting 2 09-13-2005 06:59 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-04-2006
fredao fredao is offline
Registered User
  
 

Join Date: Dec 2006
Posts: 59
extract content from a file and insert to another file

please help for the following task...

I have to extract the mac address & IP address from the file1:

...
0100004512EEF4 03 192.168.0.7 192.168.0.1 -1 ...
0100779hF5D212 03 192.168.0.8 192.168.0.1 -1 ...
0100789lF5D212 03 192.168.0.9 192.168.0.1 -1 ...
...

change the format (addidng colon) and insert into file 2

...
host {
hardware ethernet 00:00:45:12:EE:F4;
fixed-address 192.168.0.7;
}

host {
hardware ethernet 00:77:9h:F52:12;
fixed-address 192.168.0.8;
}
...

please tell me how I can exact the data and change the format and insert into another file, thanks!
  #2 (permalink)  
Old 12-04-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
nawk -f fredao.awk file1

fredao.awk:

Code:
{
   gsub("..", "&:",$1)
   gsub(":$", "",$1)

   printf("host {\nhardware ethernet %s;\nfixed-address %s;\n}\n", $1, $3)
}

Last edited by vgersh99; 12-04-2006 at 04:10 PM.. Reason: fixed printf for the IPaddress
  #3 (permalink)  
Old 12-05-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
sed "s/..\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)....\([^ ]*\).*/host {\\
hardware ethernet \1:\2:\3:\4:\5:\6;\\
fixed-address \7;\\
}/w file2" file

Last edited by anbu23; 12-05-2006 at 02:07 AM..
  #4 (permalink)  
Old 12-05-2006
fredao fredao is offline
Registered User
  
 

Join Date: Dec 2006
Posts: 59
Quote:
Originally Posted by anbu23
Code:
sed "s/..\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)....\([^ ]*\).*/host {\\
hardware ethernet \1:\2:\3:\4:\5:\6;\\
fixed-address \7;\\
}/w file2" file

there seems to be a little problem, as I ONLY want to extract the first and 3rd field for each line.
  #5 (permalink)  
Old 12-05-2006
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,509
Python alternative
Code:
#!/usr/bin/python
o = open("file2.txt","a")
for line in open("input.txt"):
 	splitted = line.split()
 	mac, ip = splitted[0] , splitted[2]
 	out = """
 	host {
		hardware ethernet %s;
		fixed-address %s;
	      }
 	""" %(mac,ip)
 	o.write(out + "\n")
o.close()
  #6 (permalink)  
Old 12-06-2006
fredao fredao is offline
Registered User
  
 

Join Date: Dec 2006
Posts: 59
Quote:
Originally Posted by vgersh99
nawk -f fredao.awk file1

fredao.awk:

Code:
{
   gsub("..", "&:",$1)
   gsub(":$", "",$1)

   printf("host {\nhardware ethernet %s;\nfixed-address %s;\n}\n", $1, $3)
}
My enviroment doesn't have nawk, can this be done with awk? thanks!
  #7 (permalink)  
Old 12-06-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Quote:
Originally Posted by fredao
My enviroment doesn't have nawk, can this be done with awk? thanks!
wouldn't hurt tryin'......
Closed Thread

Bookmarks

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:42 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0