Need Script to insert colons in each line of file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Need Script to insert colons in each line of file
# 1  
Old 08-05-2008
Bug 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  
Old 08-05-2008
Code:
sed 's/../&:/g;s/:$//' inputfile > outputfile

# 3  
Old 08-05-2008
you are the greatest!!! thank you sooo much!
# 4  
Old 08-05-2008
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  
Old 08-05-2008
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.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Insert a line of text on nth line of a file

Hi All, I am using UNix Sun OS sun4u sparc SUNW,SPARC-Enterprise My intention is to insert a line of text after 13th line of every file inside a particular directory. While trying to do it for a single file , i am using sed sed '3 i this is the 4th line' filename sed: command garbled: 3... (5 Replies)
Discussion started by: gotamp
5 Replies

2. Solaris

sed insert into line 1 via script

Hi I am trying to run a sed command within a script..unfortunately it wasn't written on Solaris so doesn't work. Can anyone help with the correct coding please? It is: sed -i '1i ROWID;ORDER_ID;JOB_NAME;ORDER_TABLE' ${OUTFILE} (4 Replies)
Discussion started by: Grueben
4 Replies

3. Shell Programming and Scripting

How to read a text file line by line and insert into a database table?

I have a test file that I want to read and insert only certain lines into the the table based on a filter. 1. Rread the log file 12 Hours back Getdate() -12 Hours 2. Extract the following information on for lines that say "DUMP is complete" A. Date B. Database Name C.... (2 Replies)
Discussion started by: JolietJake
2 Replies

4. UNIX for Dummies Questions & Answers

Script to insert a line

Hi Help, I have a file which looks like 123 44 55 344 55 77 600 88 99 123 44 56 342 45 65 600 76 88 I need to insert a line 900 87 65 after everytime it finds the the line with $1=600 that means o/p should be like 123 44 55 344 55 77 600 88 99 900 87 65 and so ... please help..... (13 Replies)
Discussion started by: Indra2011
13 Replies

5. Shell Programming and Scripting

Insert a new line before every 5th line in a file

Hi, I need to insert a new line containing the string "QUERY" above every 5 lines. The below piece of code inserts a new line after every 5th line awk '{print $0} !(NR%5) {print "QUERY"}' sed 'n;n;n;n;G;' --> I do not know how to give "QUERY" string here But I need to insert it before... (4 Replies)
Discussion started by: royalibrahim
4 Replies

6. Programming

insert line into a file

how to insert a line of text that is next to the current line(file pointer pointing to) in the file ?? :wall: ex: suppose a file named 'Sample' has the following content in it. this is to give clear idea about the problem if file pointer is pointing to the first line then i want to... (3 Replies)
Discussion started by: kavitha rao
3 Replies

7. Shell Programming and Scripting

awk script to compare and insert a line

Hi I want to compare a string at fixed position 10-20 for all the lines starting with 6. if they dont match it should take a copy of a line starting with 1 and insert it before the line starting with 6. How do i this? Please help Eg 1 test 1 765533 7643743 6 yes 3 5363653 373833 7... (9 Replies)
Discussion started by: appsguy616
9 Replies

8. Shell Programming and Scripting

Insert line into file

Hi, My File 'temp.txt' contents are like this. <Managers> Mng={{FIL|FAVEI.mng|111}|15.000000|17.000000|17.000000| Mng={{FIL|FAPSV.mng|222}|3.000000|0.000000|0.000000|0.000000| Mng={{FIL|FAVIF.mng|333}|8.000000|8.000000|8.000000|8.000000|... (3 Replies)
Discussion started by: vinay123
3 Replies

9. UNIX for Dummies Questions & Answers

How to insert new line in the data file using the script

Hi all, I have a test.dat file.In that file i have many lines of data. IN between some lines i want to insert a new line while running the test.ksh. Say for ex: In the dat file i have data like N001 100.00 N001 200.00 N001 300.00 N001 400.00 <== After this line i want to... (2 Replies)
Discussion started by: Sona
2 Replies

10. UNIX for Advanced & Expert Users

Insert a line as the first line into a very huge file

Hello, I need to insert a line (like a header) as the first line of a very huge file (about 3 ml rows). I am able to do it with sed, but redirecting the output and creating a new file takes quite some time. I was wondering if there was a more efficient way of doing it? Any help would be... (3 Replies)
Discussion started by: shriek
3 Replies
Login or Register to Ask a Question