Quote:

>Many thanks to all who have helped me with my awk problems so far.

>I worked through most of my problems and got my script working, but I now

>have one question.

>Is it possible to have multiple filed separators at the same time?

>If I change the field separator to : then a space is no longer a separator.

>What I want to do is keep the space as a separator & have a : as a

>separator too.

>I am parsing a statements of the following form

>hours: 44

>but this is a manually constructed database that people just e-mail forms

>to. They get the basic form which has "hours:" and fill it in.

>There things in there like

>hours: 44.0

>hours: 22 hours

>hours: 12.5 hours estimated including ....

>My current script has no problems extracting the number, but what I am afraid

>of is someone puting something like

>hours:44...

>and awk not seeing that as 2 separate fields.

>Thanks

>Mark

My desk clock says 9:15 which reminds me - you may get a colon in the

number of hours (separating hours and minutes) so you need to be careful

about using a colon as a field separator. You might end up throwing away

the minutes and rounding the hours downward.

Assuming :-

1) the line always starts with "hours:"

2) a time of the form 44.1 means 44 and one tenth hours

3) a time of the form 44:06 means 44 hours and 6 minutes

I would suggest something like this

/^hours:/ {

sub(/^hours:[ \t]*/,"") # delete the word "hours:"

if ($1~/^[0-9]+:/) {

sub(/:/," ")

h=$1+$2/60

} else {

h=$1+0

}

print h,"hours" # or do what you want with variable h

Quote:

}

In the line h=$1+0 I add 0 to ensure that h is treated as a number, not

a string so that any trailing non-number characters will be removed.

Using this as input :-

hours: 44.0

hours: 22 hours

hours: 12.5 hours estimated including ....

My current script has no problems extracting the number, but what I am

afraid

of is someone puting something like

hours:44...

hours:41:25

I get this as output :-

44 hours

22 hours

12.5 hours

44 hours

41.4167 hours

I used gawk 3.0.3 but there is nothing gawk-specific here. Some very old

versions of awk might not reparse the line after "hours:" is removed. I

recommend [gnmt]awk.

Hope this helps.

--

Alan Linton