LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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, 12:39 PM   #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, 04:04 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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 04:38 PM.
 
1 members found this post helpful.
Old 06-05-2010, 07: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, 02: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 02:46 PM. Reason: added in results after I tried it
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 01:24 PM
[SOLVED] specifying fields for printing in gawk from command line David the H. Programming 8 08-04-2009 04:32 PM
Works from command line but not from the cron WingnutOne Linux - General 8 11-08-2007 09:18 AM
btdownload*.py, works from Command Line, not from cron SkipHuffman Linux - Software 11 12-30-2005 03:55 PM
works on command not on cron rickict Linux - General 1 11-17-2005 03:52 PM


All times are GMT -5. The time now is 05:35 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration