Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Keeping padding in a date field

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-09-2009
seanbyrne seanbyrne is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 16 January 2009, 10:13 AM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Keeping padding in a date field

Hi Guys,

I'm having a bit of a problem with a script, i need to get the day, month and day of month into a string, so i'm using:


Code:
CURRENT_DATE=`date +"%a %b %e"`

It is getting the correct date out, however it is not keeping the padding on the day of month. The %e is supposed to pad the day of month, but it doesn't seem to want to.

What should be coming out is 'Fri Jan 9' (2 spaces padding before the 9), but it is giving me 'Fri Jan 9' (one space Linux)

If i use the command 'date' in the unix shell it brings it in the right format, however if i use 'echo `date`' it removes the padding on the day of the month.

This is probably a very confusing post, i apologise!! Really hope someone can help Linux
Sponsored Links
    #2  
Old Unix and Linux 01-09-2009
radoulov's Unix or Linux Image
radoulov radoulov is offline Forum Advisor  
Forum Adviser
 
Join Date: Jan 2007
Last Activity: 9 January 2017, 4:40 AM EST
Location: Варна, България / Milano, Italia
Posts: 5,690
Thanks: 184
Thanked 629 Times in 586 Posts
Just quote the identifier:


Code:
bash-3.2.48(21)[~]$ CURRENT_DATE="$(date +"%a %b %e")"
bash-3.2.48(21)[~]$ echo $CURRENT_DATE 
Fri Jan 9
bash-3.2.48(21)[~]$ echo "$CURRENT_DATE"
Fri Jan  9

Sponsored Links
    #3  
Old Unix and Linux 01-09-2009
Ikon's Unix or Linux Image
Ikon Ikon is offline Forum Advisor  
Computer Geek
 
Join Date: Jul 2008
Last Activity: 15 January 2015, 10:57 AM EST
Location: Frederick, MD
Posts: 748
Thanks: 4
Thanked 11 Times in 10 Posts
Why not use:

Code:
# date +"%a %b %d"
Fri Jan 09

It keeps 2 digit date.

%e does pad on my RedHat box:

Code:
# date +"%a %b %e"
Fri Jan  9

EDIT:
Ahh I get it now.. Just do what radoulov says....
    #4  
Old Unix and Linux 01-12-2009
seanbyrne seanbyrne is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 16 January 2009, 10:13 AM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for your help guys!

Still having a problem though, i'm plugging the variable into a sed command and its still not padding the %e variable:


Code:
CURRENT_DATE="$(date +"%a %b %e")"

sed -n '/^"$CURRENT_DATE"/,$ p' /var/sean/errorlog.log > /tmp/tempfile.tmp

Again, any help is appreciated!
Sponsored Links
    #5  
Old Unix and Linux 01-12-2009
cfajohnson's Unix or Linux Image
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 11 July 2016, 2:55 PM EDT
Location: Toronto, Canada
Posts: 2,898
Thanks: 0
Thanked 134 Times in 118 Posts
Quote:
Originally Posted by seanbyrne View Post
Still having a problem though, i'm plugging the variable into a sed command and its still not padding the %e variable:


Code:
CURRENT_DATE="$(date +"%a %b %e")"

sed -n '/^"$CURRENT_DATE"/,$ p' /var/sean/errorlog.log > /tmp/tempfile.tmp


You are not plugging the variable into the sed command; you are giving it a literal string because it is within single quotes.


Code:
CURRENT_DATE=$(date +"%a %b %e")
sed -n "/^$CURRENT_DATE/,$ p" /var/sean/errorlog.log > /tmp/tempfile.tmp

Sponsored Links
    #6  
Old Unix and Linux 01-12-2009
seanbyrne seanbyrne is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 16 January 2009, 10:13 AM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Ah rite. Solves the problem, thanks dude!
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
CSV formatting with prefixing, appending and padding field meself Shell Programming and Scripting 7 01-12-2009 11:40 PM
How to Sort files on date field shivaastrogun Shell Programming and Scripting 4 11-17-2008 06:38 AM
How to sort a field in a file having date values risshanth Shell Programming and Scripting 4 06-04-2008 05:03 AM
handling date field mgirinath Shell Programming and Scripting 3 04-03-2007 06:00 PM
Arrange date in a field sabercats Shell Programming and Scripting 6 02-28-2006 03:56 PM



All times are GMT -4. The time now is 01:07 AM.