Computing.Net > Forums > Unix > URGENT! help with sorting in awk

URGENT! help with sorting in awk

Reply to Message Icon

Original Message
Name: raine
Date: January 7, 2003 at 02:29:47 Pacific
Subject: URGENT! help with sorting in awk
OS: windows
CPU/Ram: pentium
Comment:

Help! If for example I have this kind of table:

black 4-feb-2001
green 3-mar-2002
blue 6-jun-2003
red 1-jan-2000

Can I output it in such a way that it gets sorted by the value of the second field? Like if I want to sort the dates by ascending or descending order? How about displaying just the results that fall within a given range, say just those from jan2000-mar2002?

Any help would be VERY MUCH appreciated!


Report Offensive Message For Removal

Response Number 1
Name: cdac1000
Date: January 7, 2003 at 06:08:17 Pacific
Subject: URGENT! help with sorting in awk
Reply: (edit)

Hope this helps you.

#!/usr/bin/ksh

# ----- file t
# black 4-feb-2001
# green 3-mar-2002
# blue 6-jun-2003
# red 1-jan-2000

# Separate the date part from source and store in tempfile

cat t | nawk '{print $2 }' > p

# Use the tempfile as input break it on -, and get the month part and store in another file
# sort -M to sort as months. ( reference from UNIX man pages )

cat p | nawk -F"-" '{print $2}' | sort -M > m

# the for loop will get each element
# using grep on original source file each time getting data

for element in `cat m`
do
grep $element t >> r
done


Report Offensive Follow Up For Removal

Response Number 2
Name: James Boothe
Date: January 7, 2003 at 08:20:54 Pacific
Subject: URGENT! help with sorting in awk
Reply: (edit)

In the solution below, awk rewrites the file just to remove the dashes from the second field so that they can be treated as separate fields by the sort command.

On the first sort key, -k4 would mean field 4 thru end of line, and would mess up the sort if there were more fields on the line. So you need to say "field 4 thru field 4".

The second sort key will sort properly as months.

The third sort key will sort properly as day. -k2 without the "n" would sort alphanumerically with 10 coming before 2.

awk '{gsub("-"," ",$2);print}' myfile |
sort -k4,4 -k3M -k2n

If you need the sorted output in the original format again, you could pipe it through awk again.


Report Offensive Follow Up For Removal

Response Number 3
Name: raine
Date: January 7, 2003 at 08:39:54 Pacific
Subject: URGENT! help with sorting in awk
Reply: (edit)

Thanks very much for the help cdac and James! I have a further question on a similar problem.

If I have a table of schedules and the fields are:

date activity timestart timeend

And I use awk '/03-feb-2002/' myfile to list my activities for that certain date:

1. how can i sort it so it shows my activities so that the earliest ones show first and then the later ones?

2. how can i make it so that if i supply an argument (let's say a start time), then the result would be all activities starting from that time onwards

3. how can i make it so that if i supply an argument (a range of time) Ex: 10:00am 11:00am (from 10am-11am), then it shows activities falling under there.

Thanks again for any help on this!



Report Offensive Follow Up For Removal

Response Number 4
Name: raine
Date: January 7, 2003 at 22:29:58 Pacific
Subject: URGENT! help with sorting in awk
Reply: (edit)

Sorry to be such a nag...

but please? anyone?

I need it in less than 24 hours -_-; and I can't make the arrays in awk work so that it will sort. (Can't understand the arrays actually.) =(



Report Offensive Follow Up For Removal

Response Number 5
Name: James Boothe
Date: January 8, 2003 at 07:16:28 Pacific
Subject: URGENT! help with sorting in awk
Reply: (edit)

So you need a solution how many hours from now?

All 3 scenarios above can be done with a single script that is provided either one, two, or three parameters:

activities 03-feb-2002
activities 03-feb-2002 10:00
activities 03-feb-2002 10:00 11:00

You need to post a few lines of actual data because the exact format is important. I would hope that the times are military, but it looks like they might be in 10:00am format.

So I need your time constraint, sample data, and the output of the following:

print a |
awk -v x=$SHELL '{sub("a","b");print x,$1}'


Report Offensive Follow Up For Removal


Response Number 6
Name: raine
Date: January 9, 2003 at 06:41:26 Pacific
Subject: URGENT! help with sorting in awk
Reply: (edit)

I still do need it because we passed an almost non-functioning project and thinking of passing another one (late, but maybe we can at least get something to work -_-;).

Actually, all three scenarios you mentioned are needed. The user can either input
1. just the date : where the program shows all activities (that's the only thing we've got working, using awk -_-;)

2. the date and start time : program shows all activities starting from that time

3. date, start time, end time: program shows all activities within that range

The exact format of the records are

13-feb-2003|1:00a|2:00p|activity

where $1 - date
2 - start time (a indicates A.M.)
3 - end time (p indicates P.M.)
4 - activity name

Thanks so much...
We tried everything from putting the records into arrays (we had a problem outputting the contents), from using awk to display it (but it wouldnt take the argument if($2==$*) (so that it would take the date inputted by the user), etc.


Report Offensive Follow Up For Removal

Response Number 7
Name: James Boothe
Date: January 9, 2003 at 08:39:41 Pacific
Subject: URGENT! help with sorting in awk
Reply: (edit)

I will start coding. In the meantime:

1. Show me the output of:

print a |
awk -v x=$SHELL '{sub("a","b");print x,$1}'

2. If start-time and stop-time are provided as parameters, do you want to see all entries that have a start time in that range OR all entries that span that range? For example, if you have:

2:00p 4:00p activity1
4:00p 6:00p activity2
6:00p 8:00p activity3

and you request a range of 3:00p thru 5:00p,
only activity2 has a start time in that range, but activity1 was still occurring in that time span.


Report Offensive Follow Up For Removal

Response Number 8
Name: anukta_c
Date: January 20, 2003 at 04:52:56 Pacific
Subject: URGENT! help with sorting in awk
Reply: (edit)

I am interested in learning the solution to this.
1. the output I get is :
/usr/bin/ksh b
2.What if I like to see all activities that have a start time or end time within that range.

I am just interested to know how you would have solved the problem.

-Anukta


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: URGENT! help with sorting in awk

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software