Issue for going back to previous line under conditions
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.
Thanks for your help, but this alternative is too quick ! :-)
It doesn't work for me...
The point here is to say if a[$1] exist only once then print the entire line, and if a[$1] exist more than once then go back to the first occurence and add the supplementary fields from the next occurrences.
Caveats are that it assumes there are only two fields per line, and the output is (as you can see) unsorted in relation to the original, due to awk's internal array index tracking.
Last edited by David the H.; 07-10-2012 at 02:19 AM.
Reason: formatting clean-up
I guess from the initial data I was of the understanding the data was sorted by column 1 (hence my suggested solution).
Your current process obviously cannot work as using print will leave the line intact but not allow for additional entries to be added.
Therefore, in an unsorted list (although will of course work for sorted, but requires storing before printing), David's solution is the way to go
Further to David's solution, if name order were important you could use an asorti in the END solution.
Yeah, maybe I should've added that to my caveats. If the list is unsorted, then you're going to have to store every line in memory and print everything out at the end. That's not a problem for small amounts of input text, but it won't work if there's more than the system memory can handle.
Rather than using asorti though, if you want the output sorted alphabetically, for example, you can simply add a PROCINFO setting to the BEGIN section:
Note that only recent versions of gawk can do this. older gawk and other awk implementations don't have any sorting features built-in, and you'd have to manually roll your own index tracking function. You'll also have to do so if you need the output order to be identical to the input, and it isn't already in one of the pre-set sorting types.
(And wouldn't it be nice if the gawk developers added a setting or two for "input order"?)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.