how to get the last word from file
Ralph W. Reid
rreid at sunset.net
Thu Mar 31 18:06:12 EST 2005
As you have probably seen, others have posted _much_ simpler solutions
to this problem than I did. There is a `case` statement in bash, but
you would have to determine how to check for different values while
performing only a single `cal` command. See your favorite man page
for bash by running `man bash` on your favorite linux box--it is big,
but there is a lot of good stuff in there.
As a side note, you might be able to use a simple word count to get
the days in any given month since `cal` should always produce the same
headings, and then a range of numbers ranging from 28 through 31
depending on the month and whether or not the month is February during
a leap year. The following seems to work, and uses the math feature
available in `bash` to assign the result to a variable, and then echos
the result for output:
let totaldays=`cal | wc -w`-9
echo $totaldays
There should always be 9 words of header info produced by `cal`, so
subtracting the count of the words in the header (9) leaves the number
of dates displayed for the month. I might have recognized this way of
producing the total days if I had slept a little before posting the
somewhat lengthy and complicated solution shown below--maybe next time
I will not be so hasty with my post if it seems a little messy
again;).
Just one more of many possible ways of producing the output you seem
to be looking for :) . HTH, and have a _great_ day!
On Wed, Mar 30, 2005 at 03:48:02PM -0700, Ned wrote:
> This is yet to be seen!
> Wow, what a syntax.
> semicolon at the end of if, elif, then,
> and the most apealing is that fi at the end.
> Thanks a lot!
> It's a totally new stuff to me.
> Is there a case structure in bash? That would fit here just perfectly.
>
> Many thanks!
> Ned
> ----- Original Message -----
> From: "Ralph W. Reid" <rreid at sunset.net>
> To: "Speakup is a screen review system for Linux." <speakup at braille.uwo.ca>
> Sent: Wednesday, March 30, 2005 1:16 PM
> Subject: Re: how to get the last word from file
>
>
> >On Tue, Mar 29, 2005 at 12:17:43PM -0700, Ned wrote:
> >>Hi list,
> >>I am using the cal command to get current month. What I need for my shell
> >>script is how many days are there in the current month, that is, the last
> >>listed date for that month is that very number.
> >>How can I get that number?
> >>cal | tail -1
> >>and then what?
> >>
> >>Many thanks!
> >>Ned
> >
> >The above command line will produce a blank line because the last line
> >produced by `cal` is a blank line. If you are looking specifically
> >for days in the current month (as opposed to a more general
> >last-word-in file search), the following is one of several ways to
> >produce it:
> >
> >if [ `cal | grep -c 31` == 1 ];
> >then
> >echo 31
> >elif [ `cal | grep -c 30` == 1 ];
> >then
> >echo 30
> >elif [ `cal | grep -c 29` == 1 ];
> >then
> >echo 29
> >elif [ `cal | grep -c 28` == 1 ];
> >then
> >echo 28
> >else
> >echo Cal did not produce a calendar.
> >fi
> >
> >HTH, and have a _great_ day!
> >
> >--
> >Ralph. N6BNO. Wisdom comes from central processing, not from I/O.
> >rreid at sunset.net http://personalweb.sunset.net/~rreid
> >...passing through The City of Internet at the speed of light!
> >_PI = 4 * ARCTAN (1)
--
Ralph. N6BNO. Wisdom comes from central processing, not from I/O.
rreid at sunset.net http://personalweb.sunset.net/~rreid
...passing through The City of Internet at the speed of light!
1 = x^0
More information about the Speakup
mailing list