LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   getting last locale's abbreviated month name through bash scripting (https://www.linuxquestions.org/questions/programming-9/getting-last-locales-abbreviated-month-name-through-bash-scripting-59018/)

markus1982 05-10-2003 05:48 AM

getting last locale's abbreviated month name through bash scripting
 
Is it possible to get the last locale's abbreviated month name using bash scripting? I'd like to archive following:
Code:

gunzip --stdout /var/log/mail.*.gz | grep "^Apr" | sort -g | /usr/local/sbin/reports/mail/pflogsumm.pl
Apr should be replaced with the last month!

Is it possible using bash scripting to do that? I mean it's pretty easy in perl, but I'd like not to wrapper a perl script around if not required!

Hko 05-10-2003 09:08 AM

(Assuming you use Debian and want the German language)
First you need to make sure the "locales" package is installed:

apt-get install locales

if it is already installed, reconfigure it:

dpkg-reconfigure locales

In either case, the debconf screen appears, where you can select locale files to be generated. Choose at for example "de_DE ISO-8859-1" and leave the default locale to "C" as suggested by debconf.

To test, enter (in bash):

LANG=de_DE date

If it works you can use it in the command line as follows:
Code:

gunzip ....  | grep "`LANG=de_DE date +%b`" | sort -g | ....
Or, to use german by default in the current shell, you can "export LANG=de_DE" first and then just use:
Code:

gunzip ....  | grep "`date +%b`" | sort -g | ....
If I get your question right, you would like to have the previous month. Like we are in may right now, and you'd like to grep for april. To do that, use:
Code:

LANG=de_DE date -d '1 month ago' +%b
Hope this helps. For more information see "info date" (not "man date", as the man page does not explain the -d '1 month ago' - like options.

markus1982 05-10-2003 09:31 AM

Quote:

Hope this helps. For more information see "info date" (not "man date", as the man page does not explain the -d '1 month ago' - like options.
I should really have checked the info pages. Didn't think about that at the time I did it.

Thanks alot for reminding me to the importance of the info pages. I was just looking for date -d '1 month ago' +%b :-)


Now the script works just perfect:
Code:

gunzip --stdout /var/log/mail.*.gz | grep "^`date -d '1 month ago' +%b`" | sort -g | /usr/local/sbin/reports/mail/pflogsumm.pl


All times are GMT -5. The time now is 06:44 AM.