Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then 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.
Help us to help you. Provide a sample input file (10-15 lines will do). Construct a sample output file which corresponds to your sample input and post both samples here. With "InFile" and "OutFile" examples we can better understand your needs and also judge if our proposed solution fills those needs.
Indeed - as Daniel pointed out - a sample input/output would be useful to fully understand your requirement. Anyway, here is an example using multi-dimensional arrays:
I should have provided an input file and output but I thought the problem was very clear. Thanks colucix, for the input file and the answer. This is exactly what I was looking for. I am now trying to understand how your elegant solution works.
The output data is sorted but it is so because of the way data is entered in input file. If the input file is:
Two-dimensional arrays are a powerful language feature but that kind of code can be difficult to write, difficult to read. At the expense of execution time you may read the input multiple times but enjoy the simplicity of one-dimensional arrays.
instead of printing directly the content of the multi-dimensional array, we assign the whole string to the array a, then we sort its content and finally print it out.
Ops, you right! Mine was a greenhorn's mistake: the array a retains the values of the last fields of the (previous) longer lines. Just add a delete statement at the end of the for loop:
Code:
for ( i = 1; i <= length(c); i++ ) {
split(f[i],_)
for ( j = 1; j <= length(_); j++ ) {
a[j] = ( _[j] " : " arr[i,_[j]] )
}
asort(a)
for ( j = 1; j <= length(a); j++ ) {
print a[j]
}
print "==============================="
delete a
}
I thought I would just throw 2 cents in ... colucix's solution is still using a 1 dimensional associative array. The SUBSEP delimited array could just as easily use any other character
not already in the values either side of the delimiter. However, as of v4, awk does indeed offer true multi-dimensional arrays.
(See here for more details)
Using a snippet from the script:
Code:
# current associative array
for ( i = 1; i <= NF; i++)
arr[i,$i]++
# 2 dimensional array
for ( i = 1; i <= NF; i++)
arr[i][$i]++
You no longer require the split command and can simply use your for loops to process the array.
The gotcha here would be making sure you sort the second dimension and not the first
#!/bin/bash
gawk '{
for ( i = 1; i <= NF; i++)
counter[i][$i]++
}
END {
for (j=1; j<=length(counter); j++){
for (i in counter[j]) {
print counter[j][i], ":",$counter[j][i];
}
print "========================"
}
}' <$1
But it does not work properly. The counts are correct but not the labels. The output was:
Thanks for your solution.
How can we use 2-dimensional array? I suspect it will be easier to understand.
Other LQ members already posted solutions using 2-dimensional arrays. I did my one-dimensional solution to show a different approach. My computer runs a back-level awk so I cannot use the true two-dimensional language feature shown by others.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.