LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 12-23-2010, 08:05 PM   #1
lupusarcanus
Senior Member
 
Registered: Mar 2009
Location: USA
Distribution: Arch
Posts: 1,022
Blog Entries: 19

Rep: Reputation: 146Reputation: 146
Unhappy Trouble with sed. (Updated)


Code:
echo Todays date is $(date +%A\,\ %B\ %d\,\ %Y).
Lets go back in time and say the output for that command is:
Code:
Todays date is Tuesday, September 08, 1995.
How do I use sed (or something else) to print it so it looks like this:
Code:
Todays date is Tuesday, September 8, 1995.
The zero annoys me, but for the life of me I can't figure out how to remove it without making something else incorrect,
Code:
i.e. September 30, 1995.
or
Code:
September 08, 2000.
And I need one to work here too:-
Code:
echo It is currently $(date +%I\:%M\ %p).
And some example output:
Code:
It is currently 08:58 AM.
Same deal. I want to remove the 0 in front of the first hour that is unnecessary. But without altering everything else, i.e.
Code:
It is currently 08:50 AM.
I want to make it look like:
Code:
It is currently 8:50 AM.
I've tried all kinds of different sed commands and can't get it working.

Last edited by lupusarcanus; 12-23-2010 at 08:52 PM.
 
Old 12-23-2010, 08:48 PM   #2
lupusarcanus
Senior Member
 
Registered: Mar 2009
Location: USA
Distribution: Arch
Posts: 1,022
Blog Entries: 19

Original Poster
Rep: Reputation: 146Reputation: 146
Solved first question.
Quote:
alias tyme="echo Today\'s date is $(date +%A\,\ %B\ %d\,\ %Y). It is currently $(date +%I\:%M\ %p)."
I feel bad for even have asked the first question now.

Still need help on the second question.

Edit: Shortened first post.

Last edited by lupusarcanus; 12-23-2010 at 08:54 PM.
 
Old 12-23-2010, 08:54 PM   #3
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
For problem #2, I believe the answer is in the man page for `date` - look at the formatting modifiers further down the page. A snippet:

Code:
       By default, date pads numeric fields with zeroes.  The following optional flags may follow `%':

       -      (hyphen) do not pad the field

       _      (underscore) pad with spaces

       0      (zero) pad with zeros
 
Old 12-23-2010, 08:59 PM   #4
lupusarcanus
Senior Member
 
Registered: Mar 2009
Location: USA
Distribution: Arch
Posts: 1,022
Blog Entries: 19

Original Poster
Rep: Reputation: 146Reputation: 146
Quote:
Originally Posted by GrapefruiTgirl View Post
For problem #2, I believe the answer is in the man page for `date` - look at the formatting modifiers further down the page. A snippet:

Code:
       By default, date pads numeric fields with zeroes.  The following optional flags may follow `%':

       -      (hyphen) do not pad the field

       _      (underscore) pad with spaces

       0      (zero) pad with zeros
Meh, stupid Mac OS X. The Mac OS X implementation of date doesn't allow me to do it.
http://developer.apple.com/library/m...n1/date.1.html
I can't wait to get a new computer. For now however, I have to deal with it.
Code:
Andrew@MacBook-Pro:~$ echo "Today\'s date is $(date +%A\,\ %B\ %d\,\ %Y). It is currently $(date %-I\:%M\ %p)."
date: illegal time format
 
Old 12-23-2010, 09:02 PM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,546
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
You have tried hard and qudos for that!

First problem solution: alias tyme="echo Today$'\''s date is \$(date +%A\,\ %B\ %d\,\ %Y). It is currently \$(date +%I\:%M\ %p).". The $'\'' is ANSI-C Quoting.

Second problem solution: use %-d and %-M instead of %d and %M. From the date man page: "By default, date pads numeric fields with zeroes. The following optional flags may follow `%': - (hyphen) do not pad the field".

Your first problem solution does not work because it sets the time when the alias is defined (as alias tyme would show) but a slightly modified form dos work: alias tyme="echo Today\'s date is \$(date +%A\,\ %B\ %d\,\ %Y). It is currently \$(date +%I\:%M\ %p)."

EDIT: your solution is neater than the ANSI-C Quoting solution -- would have sworn I'd tried it!

Last edited by catkin; 12-23-2010 at 09:05 PM. Reason: bu -> but
 
Old 12-23-2010, 09:06 PM   #6
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Indeed, that's pesky!

Not thoroughly tested, but maybe:
Code:
sed 's/ [0]*/ /g'
It just replaces any space followed by one or more zeroes, with a space only. Seems over simplistic, but who knows, maybe it will catch all situations. And maybe not..

Good luck! (Sorry if you already tried this one - I did not examine ALL of your attempts intensely).

Last edited by GrapefruiTgirl; 12-23-2010 at 09:08 PM. Reason: for clarity, I removed the garbage string I was echoing into the sed
 
Old 12-23-2010, 09:12 PM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,455

Rep: Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843
By the way, you can save some backslashes, commas don't need escapes quotes can be used instead of escaping the spaces:

Code:
alias tyme="echo Today\'s date is $(date '+%A, %B %e, %Y'). It is currently $(date '+%l:%M %p')."
The %-x may not work, but there is still:
Code:
     %d    is replaced by the day of the month as a decimal number (01-31).
...
     %e    is replaced by the day of month as a decimal number (1-31); single digits are preceded by a
           blank.
...
     %I    is replaced by the hour (12-hour clock) as a decimal number (01-12).
...
     %l    is replaced by the hour (12-hour clock) as a decimal number (1-12); single digits are preceded by
           a blank.
http://developer.apple.com/library/m...man/3/strftime

Last edited by ntubski; 12-23-2010 at 09:13 PM. Reason: add manpage url
 
Old 12-23-2010, 09:23 PM   #8
lupusarcanus
Senior Member
 
Registered: Mar 2009
Location: USA
Distribution: Arch
Posts: 1,022
Blog Entries: 19

Original Poster
Rep: Reputation: 146Reputation: 146
Quote:
Originally Posted by ntubski View Post
By the way, you can save some backslashes, commas don't need escapes quotes can be used instead of escaping the spaces:

Code:
alias tyme="echo Today\'s date is $(date '+%A, %B %e, %Y'). It is currently $(date '+%l:%M %p')."
The %-x may not work, but there is still:
Code:
     %d    is replaced by the day of the month as a decimal number (01-31).
...
     %e    is replaced by the day of month as a decimal number (1-31); single digits are preceded by a
           blank.
...
     %I    is replaced by the hour (12-hour clock) as a decimal number (01-12).
...
     %l    is replaced by the hour (12-hour clock) as a decimal number (1-12); single digits are preceded by
           a blank.
http://developer.apple.com/library/m...man/3/strftime
Damn, everytime I ask a question on LQ about bash scripting I walk away feeling more stupid than ever... I need to get my eyes checked.

Thanks though... Very informative.

Last edited by lupusarcanus; 12-23-2010 at 09:25 PM.
 
Old 12-23-2010, 09:27 PM   #9
lupusarcanus
Senior Member
 
Registered: Mar 2009
Location: USA
Distribution: Arch
Posts: 1,022
Blog Entries: 19

Original Poster
Rep: Reputation: 146Reputation: 146
Quote:
Originally Posted by GrapefruiTgirl View Post
Indeed, that's pesky!

Not thoroughly tested, but maybe:
Code:
sed 's/ [0]*/ /g'
It just replaces any space followed by one or more zeroes, with a space only. Seems over simplistic, but who knows, maybe it will catch all situations. And maybe not..

Good luck! (Sorry if you already tried this one - I did not examine ALL of your attempts intensely).
Thanks GrapefruiTgirl -- that works perfectly as well. Back to the sed guides I go...
 
Old 12-23-2010, 09:29 PM   #10
lupusarcanus
Senior Member
 
Registered: Mar 2009
Location: USA
Distribution: Arch
Posts: 1,022
Blog Entries: 19

Original Poster
Rep: Reputation: 146Reputation: 146
Quote:
Originally Posted by catkin View Post
You have tried hard and qudos for that!

First problem solution: alias tyme="echo Today$'\''s date is \$(date +%A\,\ %B\ %d\,\ %Y). It is currently \$(date +%I\:%M\ %p).". The $'\'' is ANSI-C Quoting.

Second problem solution: use %-d and %-M instead of %d and %M. From the date man page: "By default, date pads numeric fields with zeroes. The following optional flags may follow `%': - (hyphen) do not pad the field".

Your first problem solution does not work because it sets the time when the alias is defined (as alias tyme would show) but a slightly modified form dos work: alias tyme="echo Today\'s date is \$(date +%A\,\ %B\ %d\,\ %Y). It is currently \$(date +%I\:%M\ %p)."

EDIT: your solution is neater than the ANSI-C Quoting solution -- would have sworn I'd tried it!
Thanks!!

I swear I tried it too... Somehow it snuck past me.

Though, since that is ANSI-C, isn't that more 'correct' since it's standards compliant?
 
Old 12-23-2010, 09:37 PM   #11
lupusarcanus
Senior Member
 
Registered: Mar 2009
Location: USA
Distribution: Arch
Posts: 1,022
Blog Entries: 19

Original Poster
Rep: Reputation: 146Reputation: 146
Well, not exactly the neatest way to say this -- 4 posts in a row.

Anyway, I gave everyone here reputation and a very customized thank you! Within an hour and a half I had all kinds of good answers!

Don't know how to say thanks anymore so I'll say it once more:

Thanks again to GrapefruiTgirl, catkin and ntubski!
 
Old 12-23-2010, 09:37 PM   #12
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by leopard View Post
Damn, everytime I ask a question on LQ about bash scripting I walk away feeling more stupid than ever...
Nah - don't be too hard on yourself. You made a valiant effort in this one! Silly MAC - get a Linux.

Everyone who has some idea what they're talking about today, began at nothing in the beginning. You're further back on the scripting than some of us, and also you're further ahead than some of us; but there's ALWAYS something more to learn, no matter how much one knows..

Cheers!
 
Old 12-24-2010, 09:44 PM   #13
lupusarcanus
Senior Member
 
Registered: Mar 2009
Location: USA
Distribution: Arch
Posts: 1,022
Blog Entries: 19

Original Poster
Rep: Reputation: 146Reputation: 146
Just to follow up on the thread, I ended up using this string:-
Code:
alias tyme="echo Today\'s date is $(date +%A\,\ %B\ %d\,\ %Y | sed 's/ [0]*/ /g'). It is currently $(date +%I\:%M\ %p | sed 's/ [0]*/ /g')."
While using ntubski's recommendation is probably the 'most proper' way to do it, I ended up using GrapefruiTgirl's solution since the output looks nicer in certain situations (replacing the zero with a space made two spaces in a row, which made it look uglier). I'm very happy with my alias now, even though I am WAY to OCD. I am going to keep this bookmarked for later.
Hope this helps anyone passing along (though I doubt anyone will come across this situation).
Thanks again.

Last edited by lupusarcanus; 12-24-2010 at 09:47 PM.
 
  


Reply

Tags
bash, date, quotes, script, sed


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
sed updated/touched also files that i didnt search replace in them tomerbd Linux - Newbie 2 10-15-2009 06:57 PM
Trouble with sed substitution and file output bezeek Programming 2 09-03-2009 03:45 AM
trouble backreferencing with sed wds Programming 2 06-19-2008 08:13 PM
sed trouble hfawzy Programming 5 08-07-2006 10:37 PM
Having some trouble with sed P0ldy Linux - General 6 01-04-2006 04:26 PM


All times are GMT -5. The time now is 05:44 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