LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-25-2010, 01:15 AM   #1
hattori.hanzo
Member
 
Registered: Aug 2006
Posts: 168

Rep: Reputation: 15
Parsing datetime domain names


I have spent lots of time trying to figure out how to parse the domain part of this file but am stuck. How should this be approached:

Code:
20100618,00:21:31,aaa.bbb.ccc.ddd,A,(6):12003(4)wins(4)domain(6)dsmain(3)com(0)
20100618,00:21:32,aaa.bbb.ccc.ddd,SOA,(12)SP-CR-S003(4)emea(6)domain(3)com(0)
20100618,00:21:33,aaa.bbb.ccc.ddd,A,(7)altfarm(9)mediaplex(3)com(0)
20100618,00:21:33,aaa.bbb.ccc.ddd,A,(3)img(9)mediaplex(3)com(0)
Code:
2010-06-18 00:21:31,aaa.bbb.ccc.ddd,A,com,dsmain,domain,wins,:12003
2010-06-18 00:21:32,aaa.bbb.ccc.ddd,SOA,com,domain,emea,SP-CR-S003,SOA
2010-06-18 00:21:33,aaa.bbb.ccc.ddd,A,com,mediaflex,altfarm
20100-6-18 00:21:33,aaa.bbb.ccc.ddd,A,com,mediaflex,altfarm
The domains need to be parse at a maximum for 4 levels then the remainder will be put the last field.

Thanks & Regards,

Last edited by hattori.hanzo; 10-25-2010 at 01:45 AM.
 
Old 10-25-2010, 03:46 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Is this what you are looking for:

sed 's/([0-9]*)/@/g' input | awk -F, '{ printf("%s,%s,%s,%s", $1, $2, $3, $4) ; z=split($5,tets,"@") ; for (i = z-1; i > 1; --i) printf(",%s", tets[i]) ; print "" }'

Example run on given example:
Code:
$ cat input
20100618,00:21:31,aaa.bbb.ccc.ddd,A,(6):12003(4)wins(4)domain(6)dsmain(3)com(0)
20100618,00:21:32,aaa.bbb.ccc.ddd,SOA,(12)SP-CR-S003(4)emea(6)domain(3)com(0)
20100618,00:21:33,aaa.bbb.ccc.ddd,A,(7)altfarm(9)mediaplex(3)com(0)
20100618,00:21:33,aaa.bbb.ccc.ddd,A,(3)img(9)mediaplex(3)com(0)

$ sed 's/([0-9]*)/@/g' input | awk -F, '{ printf("%s,%s,%s,%s", $1, $2, $3, $4) ; z=split($5,tets,"@") ; for (i = z-1; i > 1; --i) printf(",%s", tets[i]) ; print "" }'
20100618,00:21:31,aaa.bbb.ccc.ddd,A,com,dsmain,domain,wins,:12003
20100618,00:21:32,aaa.bbb.ccc.ddd,SOA,com,domain,emea,SP-CR-S003
20100618,00:21:33,aaa.bbb.ccc.ddd,A,com,mediaplex,altfarm
20100618,00:21:33,aaa.bbb.ccc.ddd,A,com,mediaplex,img
Hope this helps.

EDIT
I just noticed you also want to change the date itself (I focussed on the reversing of the domain parts). Here's a solution (long one-liner...) that does both:

sed -e 's/^\([0-9]\{4\}\)\([0-9]\{2\}\)\([0-9]\{2\}\),/\1-\2-\3 /' -e 's/([0-9]*)/@/g' input | awk -F, '{ printf("%s,%s,%s", $1, $2, $3) ; z=split($4,tets,"@") ; for (i = z-1; i > 1; --i) printf(",%s", tets[i]) ; print "" }'

Example run:
Code:
$ sed -e 's/^\([0-9]\{4\}\)\([0-9]\{2\}\)\([0-9]\{2\}\),/\1-\2-\3 /' -e 's/([0-9]*)/@/g' input | awk -F, '{ printf("%s,%s,%s", $1, $2, $3) ; z=split($4,tets,"@") ; for (i = z-1; i > 1; --i) printf(",%s", tets[i]) ; print "" }'
2010-06-18 00:21:31,aaa.bbb.ccc.ddd,A,com,dsmain,domain,wins,:12003
2010-06-18 00:21:32,aaa.bbb.ccc.ddd,SOA,com,domain,emea,SP-CR-S003
2010-06-18 00:21:33,aaa.bbb.ccc.ddd,A,com,mediaplex,altfarm
2010-06-18 00:21:33,aaa.bbb.ccc.ddd,A,com,mediaplex,img
/EDIT

Last edited by druuna; 10-25-2010 at 04:10 AM. Reason: Noticed date change
 
1 members found this post helpful.
Old 10-25-2010, 09:27 AM   #3
hattori.hanzo
Member
 
Registered: Aug 2006
Posts: 168

Original Poster
Rep: Reputation: 15
This works perfectly. Thank you very much.

Your coding is an inspiration to me to better my skills and also to learn from.
 
Old 10-25-2010, 09:31 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

You're welcome

Quote:
Your coding is an inspiration to me to better my skills and also to learn from.
If anything is unclear about the one-liner I provided I will gladly explain it to you.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
domain names xaos5 Linux - Networking 2 06-09-2005 02:03 AM
Domain Names...? cparker15 Linux - Networking 8 08-27-2003 07:13 AM
Cheap Domain Names Volcom General 6 08-08-2003 12:33 AM
Domain Names Timbo General 7 02-14-2003 03:10 PM
domain names cic Linux - Networking 3 06-11-2002 03:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration