[SOLVED] Sort certain field but preserve other field data
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.
#!/usr/bin/gawk -f
BEGIN {
# Accept any newline convention, removing leading and trailing whitespace.
RS = "[\t\v\f ]*(\r\n|\n\r|\r|\n)[\t\v\f ]*"
# Field are spearated by whitespace.
FS = "[\t\v\f ]+"
# In output, use Unix newline convention,
ORS = "\n"
# TABs between fields, and
OFS = "\t"
# four decimal digits for floating-point values with 'print'.
OFMT = "%.4f"
# Clear the fourth and fifth field arrays.
split("", field4)
split("", field5)
# No fields yet.
fields = 0
# Clear current keys.
key1 = ""
key2 = ""
key3 = ""
}
# Output array contents.
function output() {
# Sort the fields.
asort(field4)
asort(field5)
# Output.
for (i = 1; i <= fields; i++)
print key1, key2, key3, field4[i], field5[i]
# Clear.
fields = 0
split("", field4)
split("", field5)
}
# Remove leading whitespace from first input line.
(FNR == 1) {
sub(/^[\t\v\f ]+/, "", $0)
}
# Input record:
{
# If any of the keys don't match, this is a new set.
if (key1 != $1 || key2 != $2 || key3 != $3) {
# Output the previous set, if any.
output()
key1 = $1
key2 = $2
key3 = $3
}
# Add data to current set.
fields++
field4[fields] = $4 * 1.0 # Make sure it is a number
field5[fields] = $5 * 1.0 # Make sure it is a number
}
# After the final input record, output the final set, if any.
END {
output()
}
This considers all consecutive lines where the first three fields match to be a single set. For each set, the values in the fourth field, and the values in the fifth field, are separately sorted in ascending order. The input is streamed, with just the three common fields and the values in fourth and fifth field for the current set kept in memory.
Integers are printed without a decimal point. If you want four decimal digits on all fourth and fifth field values, or perhaps some other format, use
in the output() function. The keys are treated as strings (and therefore will be exactly like they were in the input), but you can format them too if you want.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.