Explore BrainMass
Share

Unix command to sort a file based on a certain column numerically

This content was STOLEN from BrainMass.com - View the original, and get the already-completed solution here!

Please see the attached file for the complete problem.
Please help me with this programming question:
Create a single command string that will modify the file called myfile and change all occurrences of the ! to a :. The command string must also sort the file numerically on the GID field and then save the output to a file called newfile.
Replace the ## in the command string below with the correct characters to make the above described work.
sed 's/!/:/g' myfile | sort -### -t: > ~/newfile

© BrainMass Inc. brainmass.com December 20, 2018, 11:35 am ad1c9bdddf
https://brainmass.com/computer-science/files/unix-command-sort-file-certain-column-numerically-567825

Attachments

Solution Preview

(Please see the attached files for the complete solution).
In order to sort a file according to a certain field, you need to specify a Field Separator or delimiter using -t.
Since the GID values are numbers, we need to specify another sorting option "n".
Finally, we want to sort the file according to the third field (the GID field), therefore, we need to use the -k option which specifies the range of fields to ...

Solution Summary

The solution explains how to use different options with the important Unix sort command to accomplish numerical sorting of a certain field in a field separated file.

$2.19