LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 06-13-2010, 03:22 PM   #1
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,027

Rep: Reputation: 69
How can I add '-' in this string?


The command
Code:
date
produces
Code:
Sun Jun 13 14:18:52 CDT 2010
I want to show some of this information in a file. So I use
Code:
$(date | awk '{print $2$3$4}')
which produces
Code:
Jun1314:21:44
How can I insert dashes '-' in between each field so it appears as
Jun-13-14:21:44?
 
Old 06-13-2010, 03:26 PM   #2
colo
LQ Newbie
 
Registered: Mar 2004
Location: Austria
Distribution: Gentoo, Debian, Ubuntu
Posts: 14

Rep: Reputation: 0
You're thinking too complicated. Just use `date "+%h-%d-%R:%S"` instead of all your awk magic.
 
Old 06-13-2010, 03:31 PM   #3
rfernandez
Member
 
Registered: Mar 2010
Location: Brazil
Distribution: Slackware64
Posts: 264

Rep: Reputation: 41
Just add a "-" between them:

Code:
$(date | awk '{print $2"-"$3"-"$4}')
 
Old 06-13-2010, 03:37 PM   #4
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,027

Original Poster
Rep: Reputation: 69
Ah yes the double quotes worked. I tried the single quotes but I just now noticed that using single quotes would cancel my previous single quote. Thanks rfernandez.
 
Old 06-13-2010, 04:35 PM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

Why use awk: date '+%a-%d-%X'

Man date for all the options.
 
  


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] How to Read a String and add it at the end in Unix funonnet Programming 7 06-11-2010 10:53 AM
how to add two string in c renuaseri Linux - Newbie 2 06-10-2010 08:11 AM
add String in awk an_sush Linux - Newbie 1 11-20-2008 11:42 AM
quick way to add string to a text file ufmale Linux - Newbie 1 03-11-2008 05:03 PM


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

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