Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 ?
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.