LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 06-05-2010, 11:39 AM   #1
fantasygoat
Member
 
Registered: Sep 2009
Posts: 119

Rep: Reputation: 17
gawk works from command line but not from cron


I have a very simple gawk script to add commas to numbers to make them more readable:


total=`echo "$total" | /bin/gawk '{ printf "%'"'"'d", $1 }'`

It works perfectly from the command line, but when run from cron it doesn't add the commas.

Any ideas?
 
Old 06-05-2010, 03:04 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
The %'d format works only for locales that support this features. Usually crontab does not set the environment variable LANG and the crontab's locale is POSIX. Try to set
Code:
LANG=en_US.UTF-8
at the beginning of the shell script or in the crontab entry itself (obviously en_US.UTF-8 is just an example, you'd choose your actual locale):
Code:
0 22 * * * LANG=en_US.UTF-8 /path/to/scheduled/script
or even immediately before the gawk command:
Code:
total=`echo "$total" | LANG=en_US.UTF-8 /bin/gawk '{ printf "%'"'"'d", $1 }'`
Edit: an aside note: maybe you don't really nedit awk to do the job, since even the printf command supports the %'d format:
Code:
total=$(env LANG=en_US.UTF-8 printf "%'d" $total)
Hope this helps.

Last edited by colucix; 06-05-2010 at 03:38 PM.
 
1 members found this post helpful.
Old 06-05-2010, 06:02 PM   #3
fantasygoat
Member
 
Registered: Sep 2009
Posts: 119

Original Poster
Rep: Reputation: 17
Perfect, exactly the info I was looking for!
 
Old 10-25-2011, 01:18 PM   #4
s_mk
LQ Newbie
 
Registered: Oct 2011
Posts: 1

Rep: Reputation: Disabled
Thanks!

Ugh, 3 days trying to find an answer to this one. Ur a rockstar man.

One note on ur post tho... it was printf that was skipping the thousands separator.

Nevertheless, it's the same issue. After I added LANG=en_US.UTF-8 to the top of the script it worked as expected. --jic someone else doesn't see this as a likely solution for most locale formatting issues.

Last edited by s_mk; 10-25-2011 at 01:46 PM. Reason: added in results after I tried it
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script works from command line, fails from cron cmfarley19 Linux - General 4 08-14-2009 12:24 PM
[SOLVED] specifying fields for printing in gawk from command line David the H. Programming 8 08-04-2009 03:32 PM
Works from command line but not from the cron WingnutOne Linux - General 8 11-08-2007 08:18 AM
btdownload*.py, works from Command Line, not from cron SkipHuffman Linux - Software 11 12-30-2005 02:55 PM
works on command not on cron rickict Linux - General 1 11-17-2005 02:52 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 12:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration