1

I have a file with dates (formatted YYYY M DD) and their max/min/avg temperature, alongside with the precipitation for each day. The dates are in the first three columns of strings (1900 9 19 for example), and the spacebar character is the delimiter.

I need to sort them by maximum temperature (used the "sort -k 5 -n" command to sort by that column), and now want to show the dates, not the temperature and other data. What can I do to pick only these columns, without removing anything from the original file?

Example (only the last line is real data found in a file):

FIELDS
   1 2  3   4   5   6     7  8    9
YYYY M DD AVG MAX MIN ????? ?? ????

1980 1 12 252 360 140 10073 59 2692
K7AAY
  • 17,202

1 Answers1

0

After you do your sorting as you described above, run the file through other text processing tools, such as awk as shown in the comment by Rinzwind above or sed, every time you want to display it. This is text processing, and it's one of the things which makes working with Linux (and Ubuntu) so useful.

Then, put the commands in a shell script. Scripting it will make running it flawlessly easier, because instead of typing the exact command every time, you only need to type the name of the script to run it.

Scripting means write a file with the commands you want to use with a shebang on the very first line, then make that text file executable as a shell script. If the name of the script you make is "showmax", then you would make it executable so you could run it by typing its name. The contents of "showmax" could contain:

#!/bin/sh
# /user/local/bin/showmax 2019-08-15 fedeaimar 
# Sort the sourcefile on maximum temp then show just the date
# 
sort -k 5 -n /path/to/sourcefile | awk '{print $1, $2, $3}'  
# end of script

In "showmax', you will need to replace /path/to/ with the location of the data file, and sourcefile with the name of the file your data is in.

To make "showmax" executable, do

chmod u+x showmax

then move it into /usr/local/bin so it can be found from anywhere.

You could even put it on your desktop if you wish.

K7AAY
  • 17,202