👤
Home Man
Search
Today's Posts
Register

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

👤 Login to reply

 
Thread Tools Search this Thread
# 1  
Old 01-09-2009
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 )

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
# 2  
Old 01-09-2009
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

# 3  
Old 01-09-2009
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 01-12-2009
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!
# 5  
Old 01-12-2009
Quote:
Originally Posted by seanbyrne
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

# 6  
Old 01-12-2009
Ah rite. Solves the problem, thanks dude!
👤 Login to reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How do I format a Date field of a .CSV file with multiple commas in a string field? dhruuv369 Linux 3 10-16-2013 04:50 PM
Sort, sed, and zero padding date column csv bash scripting sean1357 Shell Programming and Scripting 5 07-20-2013 03:08 PM
Check if a date field has date or timestamp or date&timestamp machomaddy Shell Programming and Scripting 1 02-25-2012 10:29 PM
Replace date value with another value keeping all as is pchang Shell Programming and Scripting 2 04-01-2011 04:39 PM
date field manipulation dsravan Shell Programming and Scripting 5 06-23-2010 02:58 AM
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
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:28 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password