Keeping padding in a date field

Tags
date, field, shell scripts

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 Smilie)

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 Smilie
# 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

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Similar Threads More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Sort by date field in AIX Amit Joshi AIX 2 08-05-2015 01:36 PM
Removing date field from the string skatpally Shell Programming and Scripting 9 11-26-2014 04:32 PM
How do I format a Date field of a .CSV file with multiple commas in a string field? dhruuv369 Linux 3 10-16-2013 05:50 PM
Sort, sed, and zero padding date column csv bash scripting sean1357 Shell Programming and Scripting 5 07-20-2013 04:08 PM
Check if a date field has date or timestamp or date&timestamp machomaddy Shell Programming and Scripting 1 02-25-2012 11:29 PM
Replace date value with another value keeping all as is pchang Shell Programming and Scripting 2 04-01-2011 05:39 PM
change field 2 date format manishma71 UNIX for Advanced & Expert Users 1 07-21-2010 05:10 AM
date field manipulation dsravan Shell Programming and Scripting 5 06-23-2010 03:58 AM
Sort field by date mm/dd/yyyy csierra UNIX for Dummies Questions & Answers 6 03-01-2010 10:11 PM
Sort two columns in a field, one of them being a date sweta_doshi Shell Programming and Scripting 5 07-28-2009 07:15 AM
CSV formatting with prefixing, appending and padding field meself Shell Programming and Scripting 7 01-13-2009 12:40 AM
How to Sort files on date field shivaastrogun Shell Programming and Scripting 4 11-17-2008 07:38 AM
handling date field mgirinath Shell Programming and Scripting 3 04-03-2007 07:00 PM
Arrange date in a field sabercats Shell Programming and Scripting 6 02-28-2006 04:56 PM
All times are GMT -4. The time now is 01:18 AM.

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





Not a Forum Member?
Forgot Password?