ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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'm trying to generate a report showing what projects are using which tools and I have 350 projects and over 1000 tools to parse through. Any help would be greatly appreciated.
First, please confirm that you need to key on the actual content of the first field---ie that you don't know always what will be there.
Here's a stab at how this might go (pseudocode)
Code:
while reading the file, one line at a time:
read the first field into a variable F1, and into a variable TMP
continue reading as long as the first field matches F1
remove the first field
append the second field to TMP
end inner loop
write TMP to the output file
end outer loop
Truly an amazing one liner. Unfortunately I don't have a clue how it works.
Maybe the following will help a little, but I strongly suggest to read some good reference manual (the official gawk manual being the best, in my opinion). The statement
Code:
a[$1]=a[$1]" "$2
assigns values to array "a". Index in arrays can be any string, so that here we can use the first field $1 as index. The value is: the current value of the corresponding element of "a", followed by a blank space, followed by the content of the second field (simple string concatenation).
In other words the first field of each line of the input file is an index of the array, whereas the corresponding second fields are the values concatenated together.
In the END statement the whole array is scanned and each index is printed out together with the value of each array's element.
Maybe the following will help a little, but I strongly suggest to read some good reference manual (the official gawk manual being the best, in my opinion). The statement
Code:
a[$1]=a[$1]" "$2
assigns values to array "a". Index in arrays can be any string, so that here we can use the first field $1 as index. The value is: the current value of the corresponding element of "a", followed by a blank space, followed by the content of the second field (simple string concatenation).
In other words the first field of each line of the input file is an index of the array, whereas the corresponding second fields are the values concatenated together.
In the END statement the whole array is scanned and each index is printed out together with the value of each array's element.
This is what blows me away, "Index in arrays can be any string". That is handy. I would probably know that if I read the freakin manual.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.