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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
12-23-2010, 08:05 PM
|
#1
|
|
Senior Member
Registered: Mar 2009
Location: USA
Distribution: Arch
Posts: 1,019
Rep: 
|
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.
|
|
|
|
12-23-2010, 08:48 PM
|
#2
|
|
Senior Member
Registered: Mar 2009
Location: USA
Distribution: Arch
Posts: 1,019
Original Poster
Rep: 
|
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.
|
|
|
|
12-23-2010, 08:54 PM
|
#3
|
|
Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
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
|
|
|
|
12-23-2010, 08:59 PM
|
#4
|
|
Senior Member
Registered: Mar 2009
Location: USA
Distribution: Arch
Posts: 1,019
Original Poster
Rep: 
|
Quote:
Originally Posted by GrapefruiTgirl
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
|
|
|
|
12-23-2010, 09:02 PM
|
#5
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,367
|
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
|
|
|
|
12-23-2010, 09:06 PM
|
#6
|
|
Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
Indeed, that's pesky!
Not thoroughly tested, but maybe:
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
|
|
|
|
12-23-2010, 09:12 PM
|
#7
|
|
Senior Member
Registered: Nov 2005
Distribution: Debian
Posts: 2,056
|
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
|
|
|
|
12-23-2010, 09:23 PM
|
#8
|
|
Senior Member
Registered: Mar 2009
Location: USA
Distribution: Arch
Posts: 1,019
Original Poster
Rep: 
|
Quote:
Originally Posted by ntubski
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.
|
|
|
|
12-23-2010, 09:27 PM
|
#9
|
|
Senior Member
Registered: Mar 2009
Location: USA
Distribution: Arch
Posts: 1,019
Original Poster
Rep: 
|
Quote:
Originally Posted by GrapefruiTgirl
Indeed, that's pesky!
Not thoroughly tested, but maybe:
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... 
|
|
|
|
12-23-2010, 09:29 PM
|
#10
|
|
Senior Member
Registered: Mar 2009
Location: USA
Distribution: Arch
Posts: 1,019
Original Poster
Rep: 
|
Quote:
Originally Posted by catkin
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?
|
|
|
|
12-23-2010, 09:37 PM
|
#11
|
|
Senior Member
Registered: Mar 2009
Location: USA
Distribution: Arch
Posts: 1,019
Original Poster
Rep: 
|
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! 
|
|
|
|
12-23-2010, 09:37 PM
|
#12
|
|
Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
Quote:
Originally Posted by leopard
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!
|
|
|
|
12-24-2010, 09:44 PM
|
#13
|
|
Senior Member
Registered: Mar 2009
Location: USA
Distribution: Arch
Posts: 1,019
Original Poster
Rep: 
|
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 10:47 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|