LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 02-21-2012, 11:31 AM   #1
vickyk
Member
 
Registered: Dec 2009
Posts: 38

Rep: Reputation: 6
Arrow Replace blank space by underscore in output of a command.


Hi All,

I want to replace the spaces in output of the following command
with underscore ( _ ).

Code:
# echo `date +%c`
Tue 21 Feb 2012 09:53:27 PM IST
I am ultimately trying to achieve the following.
Code:
mv /home/vicky/maxima/logs /home/vicky/maxima/backup/logs.`date +%c`
That is the moved file should have a name with complete timestamp as follows.

Code:
logs.Tue_21_Feb_2012_10:00:02_PM_IST
Please share your ideas
 
Old 02-21-2012, 11:36 AM   #2
theW
Member
 
Registered: Jul 2011
Distribution: arch, vector, mint
Posts: 37

Rep: Reputation: Disabled
Just use sed, it would be someting like sed 's/ /_/'
 
Old 02-21-2012, 11:46 AM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
In BASH:

Code:
[me@comp ~]$ str=$(date +%c)
[me@comp ~]$ echo $str
Tue 21 Feb 2012 09:46:03 AM MST
[me@comp ~]$ echo ${str// /_}
Tue_21_Feb_2012_09:46:03_AM_MST
 
1 members found this post helpful.
Old 02-21-2012, 11:48 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530
Quote:
Originally Posted by vickyk View Post
Please share your ideas
...apart from BASH date can also accomplish this if you could expand %c yourself: `date +%a_%b_%d_%Y_%H:%M:%S_%p_%Z` gets you there. BTW I think the date stamp would not be very useful: using %Y%m%d-style makes sorting easier than your format.
 
1 members found this post helpful.
Old 02-21-2012, 12:04 PM   #5
vickyk
Member
 
Registered: Dec 2009
Posts: 38

Original Poster
Rep: Reputation: 6
Thanks suicidaleggroll and unSpawn !!
 
Old 02-21-2012, 12:05 PM   #6
uhelp
Member
 
Registered: Nov 2011
Location: Germany, Bavaria, Nueremberg area
Distribution: openSUSE, Debian, LFS
Posts: 205

Rep: Reputation: 43
Code:
dat=$( date +%c ) ;  echo ${dat// /_}
oops, too late....
 
Old 02-21-2012, 12:10 PM   #7
vickyk
Member
 
Registered: Dec 2009
Posts: 38

Original Poster
Rep: Reputation: 6
Quote:
Originally Posted by uhelp View Post
Code:
dat=$( date +%c ) ;  echo ${dat// /_}
oops, too late....
Thats the beauty of linux. You ask for one solution and you get many.
We just need to make informative choice !

Thanks uhelp !
 
Old 02-22-2012, 09:44 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Why not just manually format the date string as you want it to appear?

Code:
date "+%a_%d_%b_%Y_%I:%M:%S_%p_%Z"
Edit: Whoops, overlooked unSpawn's post above.

Last edited by David the H.; 02-22-2012 at 09:46 AM.
 
  


Reply


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
[SOLVED] Replacing underscore with a space in a file name recursively. shibaa Linux - Newbie 9 01-12-2012 02:38 PM
bash - output blank line after each command j0hnsmith Linux - General 2 10-22-2009 01:05 AM
what is the Linux command line character for a blank space? Peter Shepard Red Hat 3 06-21-2006 07:25 PM
Replace blank/almost blank lines in file Wynd Linux - General 3 01-27-2004 05:49 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 02:04 AM


All times are GMT -5. The time now is 09:12 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration