LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Replace blank space by underscore in output of a command. (https://www.linuxquestions.org/questions/linux-newbie-8/replace-blank-space-by-underscore-in-output-of-a-command-930515/)

vickyk 02-21-2012 10:31 AM

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

theW 02-21-2012 10:36 AM

Just use sed, it would be someting like sed 's/ /_/'

suicidaleggroll 02-21-2012 10:46 AM

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


unSpawn 02-21-2012 10:48 AM

Quote:

Originally Posted by vickyk (Post 4608302)
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.

vickyk 02-21-2012 11:04 AM

Thanks suicidaleggroll and unSpawn !!

uhelp 02-21-2012 11:05 AM

Code:

dat=$( date +%c ) ;  echo ${dat// /_}
oops, too late....

vickyk 02-21-2012 11:10 AM

Quote:

Originally Posted by uhelp (Post 4608342)
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 !

David the H. 02-22-2012 08:44 AM

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


All times are GMT -5. The time now is 03:31 PM.