LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-23-2011, 08:44 PM   #1
dysn
LQ Newbie
 
Registered: Jul 2011
Posts: 1

Rep: Reputation: Disabled
Data Formating


I have a big file (with more fields) like this:
Username: John Wright
Compony: New Consultancy
Phone: 303-234-4567
Address: XXXXXXX

Username: Peter Bill
Compony: Anderson Consultancy
Phone: 520-123-4567
Address: zzzzzzzz

Username: John Wright
Compony: Westfiled Pharmacy
Phone: 303-234-4567
Address: YYYYYYYY


Expected output is
===================

John Wright, New Consultancy,303-234-4567,XXXXXXX
Peter Bill, Anderson Consultancy, 520-123-4567,zzzzzzz
John Wright, Westfiled Pharmacy,303-234-4567,YYYYYYYY




I am able to do this using awk . But it is taking long time to achieve.

Is there any better way to do using Tcl, perl or awk ?
 
Old 07-23-2011, 09:00 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,623

Rep: Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964
Quote:
Originally Posted by dysn View Post
I have a big file (with more fields) like this:
Username: John Wright
Compony: New Consultancy
Phone: 303-234-4567
Address: XXXXXXX

I am able to do this using awk . But it is taking long time to achieve. Is there any better way to do using Tcl, perl or awk ?
Well, perl *CAN* do it, but the question back is, why re-write what's already working? If you can do this with awk, let it run and be finished with it. The only real need to rewrite this would be if you had to run this very often, and the file was always growing, and how MUCH time it takes.

If you have to run it once a week, and even if it takes 20 minutes to run, and you've already got it working, that's a project to save for a rainy day. If you have to run it five times a day...that's different.

If I were to do it in perl (which I would), I'd probably use the split directive, and split the fields on the ":". You'd wind up with two variables, the description and value, like "Username" and "John Wright". From there, you could then output the data to an output file easily. Test variable one to see what it is, then set variable 2 accordingly. The good part of that is if you have to re-order the output (such as putting the Company first), all you've got to do is edit one line of the Perl script. Similarly easy to modify the input...just put a new test in to add new fields, and even if the data starts coming at you in a different order, the tests will catch it, and put it right.
 
Old 07-23-2011, 09:25 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Hi, welcome to LQ!

Quote:
Originally Posted by dysn View Post
I have a big file (with more fields) like this:
Username: John Wright
Compony: New Consultancy
Phone: 303-234-4567
Address: XXXXXXX

Username: Peter Bill
Compony: Anderson Consultancy
Phone: 520-123-4567
Address: zzzzzzzz

Username: John Wright
Compony: Westfiled Pharmacy
Phone: 303-234-4567
Address: YYYYYYYY


Expected output is
===================

John Wright, New Consultancy,303-234-4567,XXXXXXX
Peter Bill, Anderson Consultancy, 520-123-4567,zzzzzzz
John Wright, Westfiled Pharmacy,303-234-4567,YYYYYYYY




I am able to do this using awk . But it is taking long time to achieve.

Is there any better way to do using Tcl, perl or awk ?


Just HOW are you doing it in awk? Hard to say what a better way might
look like w/o seeing how you're doing it.



Cheers,
Tink

Last edited by Tinkster; 07-23-2011 at 09:31 PM.
 
  


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
Formating Eugney Linux - Newbie 2 05-02-2005 03:50 AM
C - fsanf() and the formating the data xviddivxoggmp3 Programming 4 04-15-2004 01:10 PM
formating Galilee Linux - Software 2 09-30-2003 11:23 PM
formating.... s9722 Linux - Hardware 10 06-17-2003 12:51 PM
formating without losing some data nakkaya Linux - Hardware 7 02-20-2003 09:54 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:17 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