LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Trouble with sed. (Updated) (http://www.linuxquestions.org/questions/linux-newbie-8/trouble-with-sed-updated-852122/)

lupusarcanus 12-23-2010 08:05 PM

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.

lupusarcanus 12-23-2010 08:48 PM

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. :o

Still need help on the second question.

Edit: Shortened first post.

GrapefruiTgirl 12-23-2010 08:54 PM

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


lupusarcanus 12-23-2010 08:59 PM

Quote:

Originally Posted by GrapefruiTgirl (Post 4201652)
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


catkin 12-23-2010 09:02 PM

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!

GrapefruiTgirl 12-23-2010 09:06 PM

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

ntubski 12-23-2010 09:12 PM

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

lupusarcanus 12-23-2010 09:23 PM

Quote:

Originally Posted by ntubski (Post 4201663)
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. :eek:

Thanks though... Very informative.

lupusarcanus 12-23-2010 09:27 PM

Quote:

Originally Posted by GrapefruiTgirl (Post 4201661)
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... :D

lupusarcanus 12-23-2010 09:29 PM

Quote:

Originally Posted by catkin (Post 4201658)
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!! :D

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

Though, since that is ANSI-C, isn't that more 'correct' since it's standards compliant?

lupusarcanus 12-23-2010 09:37 PM

Well, not exactly the neatest way to say this -- 4 posts in a row. :eek:

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! :hattip:

GrapefruiTgirl 12-23-2010 09:37 PM

Quote:

Originally Posted by leopard (Post 4201670)
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. :p

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!

lupusarcanus 12-24-2010 09:44 PM

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. :hattip:


All times are GMT -5. The time now is 01:39 AM.