LinuxQuestions.org
Help answer threads with 0 replies.
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 04-10-2013, 04:34 AM   #1
rajivswe.2k7
LQ Newbie
 
Registered: Apr 2013
Posts: 15

Rep: Reputation: Disabled
My date command is not working


I am running the below command and got the below error.Please advise.


echo -n "20130301_0050" |sed -e 's/_/ /'|xargs -0 date -d "{}" +%s

ERROR: date: extra operand `20130301 0050'
 
Old 04-10-2013, 04:55 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
Is this what you are after:
Code:
echo -n "20130301_0050" | sed 's/_/ /' | xargs -0 -I '{}' date -d '{}' +%s
Result of one liner: 1362095400
 
Old 04-10-2013, 05:58 AM   #3
rajivswe.2k7
LQ Newbie
 
Registered: Apr 2013
Posts: 15

Original Poster
Rep: Reputation: Disabled
Its working.Thank you.
 
Old 04-10-2013, 06:11 AM   #4
rajivswe.2k7
LQ Newbie
 
Registered: Apr 2013
Posts: 15

Original Poster
Rep: Reputation: Disabled
I would like to add 14400 seconds in the output.I have tried the same way,but its not working.Please advise.

[rgandhi@CF-08-2-15 ~]$ echo -n "20130301_0050" | sed 's/_/ /' | xargs -0 -I '{}' date -d '{}' +%s|xargs -0 -I '{}' `expr '{}' + 14400`
expr: non-numeric argument
 
Old 04-10-2013, 06:54 AM   #5
rajivswe.2k7
LQ Newbie
 
Registered: Apr 2013
Posts: 15

Original Poster
Rep: Reputation: Disabled
I have achieved this way. Please advise any other better way to do.
date='20130301_0050'

check_date=$(echo -n "${date}" | sed 's/_/ /' | xargs -0 -I '{}' date -d '{}' +%s|while read f; do echo `expr ${f} - 14400`;done| xargs -0 -I '{}' date -d @'{}' "+%Y%m%d_%H%M")

Thank you.
 
Old 04-10-2013, 06:56 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
What are you actually trying to accomplish? I'm not talking about specifics, but the general problem.

It seems that you are trying to use an elaborate scheme for something that might be accomplished in a simpler more elegant way.

BTW: expr should be avoided (use awk, bc or bash itself to do arithmetic)

Here are a few links that address the arithmetic issue's:
- Bash Math
- Arithmetic in Bash
- Perform arithmetic operations

These general links might be useful:

Bash:
Sed and Awk:
 
Old 04-10-2013, 07:01 AM   #7
rajivswe.2k7
LQ Newbie
 
Registered: Apr 2013
Posts: 15

Original Poster
Rep: Reputation: Disabled
Thank you.I am new to shell script.I tried to add 4 hrs to a date 20130301_0050 and then tried to convert into same format like 20130301_0450.

Thanks again for the material links...
 
Old 04-10-2013, 07:38 AM   #8
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Hi, how about

Code:
mydate=20130301_0050
date -d "${mydate//_/ } + 4 hours" +%Y%m%d_%H%M
 
1 members found this post helpful.
Old 04-10-2013, 08:35 AM   #9
rajivswe.2k7
LQ Newbie
 
Registered: Apr 2013
Posts: 15

Original Poster
Rep: Reputation: Disabled
Thank you soo much.Its working as expected.
 
  


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
date command Volcano Linux - Newbie 17 04-25-2012 07:41 PM
help in date command mmhs Linux - Newbie 2 12-17-2010 04:37 PM
using date command sumeet inani Linux - Newbie 9 05-06-2010 06:40 AM
date command miran_shahverdyan Linux - General 4 10-14-2009 07:59 AM
date command aetucker1 Linux - General 3 01-21-2005 01:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:23 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