The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Insert line into file vinay123 Shell Programming and Scripting 3 07-01-2008 04:07 AM
How to insert new line in the data file using the script Sona UNIX for Dummies Questions & Answers 2 08-22-2006 02:17 AM
Insert text file at a certain line. insania Shell Programming and Scripting 4 08-01-2006 03:46 AM
insert a line in a file RishiPahuja Shell Programming and Scripting 7 06-22-2005 04:47 AM
Insert a line as the first line into a very huge file shriek UNIX for Advanced & Expert Users 3 03-09-2005 01:22 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-05-2008
canopus15 canopus15 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 3
Smile Need Script to insert colons in each line of file

I have a file with over 500 MAC addresses. Each address is on a new line. However, the MACs do not have ":"
I need a script that will read the file, line by line and insert colons in the addresses and then print the results to a new file.

current.txt looks like this
111111111111
222222222222
333333333333


Newfile.txt Needs to look like this:
11:11:11:11:11:11
22:22:22:22:22:22
etc..
  #2 (permalink)  
Old 08-05-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009

Code:
sed 's/../&:/g;s/:$//' inputfile > outputfile

  #3 (permalink)  
Old 08-05-2008
canopus15 canopus15 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 3
you are the greatest!!! thank you sooo much!
  #4 (permalink)  
Old 08-05-2008
canopus15 canopus15 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 3
would you mind explaining the command? When I looke at the outputfile it was one long string. How can I delimit each mac by a new line? the output file needs to be read by a windows system. I ran your sed command in cygwin.
  #5 (permalink)  
Old 08-05-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
The reason is because Cygwin behaves like Unix by default, so the output file is created in Unix format (LF terminators instead of CR/LF). Use this to convert it to Windblows format:


Code:
sed 's/../&:/g;s/:$//' current.txt | unix2dos > newfile.txt

s/../&:/g does a global search and replace for any 2 characters, replacing them by the matched string (&) followed by a ":". This results in 11:11:11:11:11:11:, so the s/:$// part is just to strip off the terminating ":". $ is a special character to match the end of the line.
Closed Thread

Bookmarks

Tags
editting files scripts

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 09:30 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