Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

grouping lines with awk

Status
Not open for further replies.

arunrr

Programmer
Oct 2, 2009
103
US
I need to use awk to group the list in file a.list to a one-liner...

file a.list...
Thu Jan 14 2010, SA v Eng, 4th Test day 1
Fri Jan 15 2010, SA v Eng, 4th Test day 2
Sat Jan 16 2010, SA v Eng, 4th Test day 3
Sun Jan 17 2010, SA v Eng, 4th Test day 4
Mon Jan 18 2010, SA v Eng, 4th Test day 5

output should be...
Jan 14-18 2010, SA v Eng, 4th Test

I can remove the 1st and last 2 fields with...
awk '{$1=$NF=$(NF-1)=""}1'

Please help with grouping the remainder as shown above.
Thanks
 
A starting point:
Code:
awk 'NR==1{$1=$NF=$(NF-1)="";$3=$3"-";s=substr($0,2)}{e=$3}END{sub(/-/,"-"e,s);print s}' a.list

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hello Feherke, PH...

Thanks for your input. The prob is as follows. A minor modification from my earlier request. I have the following list file (a.list)...

Fri Nov 13 2009, 16:00 GMT 18:00 Local, South Africa v England,Johannesburg, 1st T20I
Fri Nov 20 2009, 12:30 GMT 14:30 Local, South Africa v England,Johannesburg, 1st ODI
Sun Nov 22 2009, 08:00 GMT 10:00 Local, South Africa v England,Centurion, 2nd ODI
Fri Nov 27 2009, 12:30 GMT 14:30 Local, South Africa v England,Cape Town, 3rd ODI
Wed Dec 16 2009, 08:30 GMT 10:30 Local, South Africa v England,Centurion, 1st Test day 1
Thu Dec 17 2009, 08:30 GMT 10:30 Local, South Africa v England,Centurion, 1st Test day 2
Fri Dec 18 2009, 08:30 GMT 10:30 Local, South Africa v England,Centurion, 1st Test day 3
Sat Dec 19 2009, 08:30 GMT 10:30 Local, South Africa v England,Centurion, 1st Test day 4
Sun Dec 20 2009, 08:30 GMT 10:30 Local, South Africa v England,Centurion, 1st Test day 5
Sat Dec 26 2009, 08:30 GMT 10:30 Local, South Africa v England,Durban, 2nd Test day 1
Sun Dec 27 2009, 08:30 GMT 10:30 Local, South Africa v England,Durban, 2nd Test day 2
Mon Dec 28 2009, 08:30 GMT 10:30 Local, South Africa v England,Durban, 2nd Test day 3
Tue Dec 29 2009, 08:30 GMT 10:30 Local, South Africa v England,Durban, 2nd Test day 4
Wed Dec 30 2009, 08:30 GMT 10:30 Local, South Africa v England,Durban, 2nd Test day 5

If a the end of each line, i have "T20I" or "ODI", the objective is to print as-is.
If i have Test day [1-5], i would like to group the 5 lines into one line as shown below...

Dec 16 (Wed) - 20 (Sun), 08:30 GMT 10:30 Local, South Africa v England,Centurion, 1st Test
Dec 26 (Sat) - 30 (Wed), 08:30 GMT 10:30 Local, South Africa v England,Durban, 2nd Test

The final output must look like...

Fri Nov 13 2009, 16:00 GMT 18:00 Local, South Africa v England,Johannesburg, 1st T20I
Fri Nov 20 2009, 12:30 GMT 14:30 Local, South Africa v England,Johannesburg, 1st ODI
Sun Nov 22 2009, 08:00 GMT 10:00 Local, South Africa v England,Centurion, 2nd ODI
Fri Nov 27 2009, 12:30 GMT 14:30 Local, South Africa v England,Cape Town, 3rd ODI
Dec 16 (Wed) - 20 (Sun), 08:30 GMT 10:30 Local, South Africa v England,Centurion, 1st Test
Dec 26 (Sat) - 30 (Wed), 08:30 GMT 10:30 Local, South Africa v England,Durban, 2nd Test

Hope this is clear...
Thanks
Arun
 
Here is a fairly verbose solution:

Code:
awk '
        # function to print a test, if we have saved one, and reset the test
        # start date
        function p() {
                if (teststart) print teststart " - " testend ", " thistest
                teststart=""
        }
        # not a test match, print previous test (if saved), and print this
        # one-day event, skip to next record
        !/ Test / { p(); print; next }
        # a test match
        {
                # strip off the day number
                sub("day [0-9]$","",$0)
                # get fields which identify this test
                split($0,a,", +")
                thistest=a[2] ", " a[3] ", " a[4]
                # if it is a new test, print the previous one
                if (thistest != prevtest) p()
                # save the start and end dates of this test
                if (!teststart) { teststart=$2 " " $3 " (" $1 ")" }
                testend=$2 " " $3 " (" $1 ")"
                # update the previous test
                prevtest=thistest
        }
        END { p() }
' a.list

Annihilannic.
 
Incidentally, did you intend to leave the years out of the test dates?

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top