LinuxQuestions.org
Review your favorite Linux distribution.
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 09-23-2011, 06:24 PM   #1
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 294

Rep: Reputation: 10
Date Calculations using script!!


Hi all,

Thanks in Advance , i am very new to programming part in script i think using some caluations+ sed command only we can do this process in script.

for example:

i have file in that one line is like this
Quote:
between '2011-08-01' and '2011-12-01'
using sed i can replace the date and all but my requirement is

The script runs every friday , suppose if i run this on 10th month means.

the script need to change the value in the file like this
Quote:
between '2011-09-01' and '2012-01-01'
every time the month alone need to change in this format


Quote:
between 'current month -1month' and 'current month + 3 months'
Ideas welcome to solve this script
 
Old 09-23-2011, 08:31 PM   #2
claudelepoisson
LQ Newbie
 
Registered: Jul 2011
Location: Montreal, Quebec, Canada
Distribution: Arch Linux
Posts: 16

Rep: Reputation: 8
sed -i "$line c between '$(date --date="- 1 month" +%Y-%m)-01' and '$(date --date="+ 3 months" +%Y-%m)-01'" $filename

where $line is the line number.
c is the sed command to replace the line with the following text.
date has handy time difference calculation and output format options.
If the line number might change, you will want to use regular expressions to find the right line.
Please ask where you need more explainations.

Yours,
 
1 members found this post helpful.
Old 09-23-2011, 09:35 PM   #3
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 294

Original Poster
Rep: Reputation: 10
Hi ,

It works, but it entirely replace the other content in the line ,

This is the content of the file: this all content comes in a single line

Quote:
select b.id as cli_id,b.login as cli_login, b.pname as cli_name, b.cname as cli_company, b.phone as cli_phone, b.email as cli_email, (select (value / 1048576) from Limits where limit_name='disk_space' and id=b.limits_id) as Client_Package, b.cr_date, (select FROM_UNIXTIME(value,"%Y-%m-%d") from Limits where limit_name='expiration' and id=b.limits_id) as Client_expire, If(b.status=0,'Active','Inactive') as Cli_Status, a.name as dom_name, If(a.status=0,'Active','Inactive') as Dom_Package, a.cr_date as dom_create, (select FROM_UNIXTIME(value,"%Y-%m-%d") from Limits where limit_name='expiration' and id=a.limits_id) as dom_expire, (select (value / 1048576) from Limits where limit_name='disk_space' and id=a.limits_id) as Dom_Package, round((a.real_size / 1048576)) as Dom_usage from domains a, clients b where (select FROM_UNIXTIME(value,"%Y-%m-%d") from Limits where limit_name='expiration' and id=a.limits_id and (FROM_UNIXTIME(value,"%Y-%m-%d") between '2011-08-01' and '2011-12-01') ) and a.cl_id=b.id group by a.id;
Quote:
between '2011-08-01' and '2011-12-01'
in that i need to replace the date part alone.

if i used your command means it entirely replace the other content in a line and display "between '2011-08-01' and '2011-12-01'" only this content


i think in sed there is option to change the particuar word in a line, i will check thanks for your help dude you made my day.
 
Old 09-24-2011, 03:59 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So assuming you have not missed out more of the file that will impact the script, simply use the advised change and search for the word 'between' as it only appears once.
 
Old 09-24-2011, 12:24 PM   #5
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 294

Original Poster
Rep: Reputation: 10
Hi all,

Code:
sed -i "/between  '2011-08-01' and '2011-12-01'/'between '$(date --date="- 1 month" +%Y-%m)-01' and '$(date --date="+ 3 months" +%Y-%m)-01'/g" /var/tmp/p.sql

i used this command to search and replace in that file but still no luck :-(
 
Old 09-24-2011, 12:46 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
well there probably should be some escaping around the place but it works fine for me:
Code:
sed "s/between[^)]*/between '$(date --date="- 1 month" +%Y-%m)-01' and '$(date --date="+ 3 months" +%Y-%m)-01'/" /var/tmp/p.sql
 
Old 09-24-2011, 02:04 PM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,613

Rep: Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962
Quote:
Originally Posted by grail View Post
well there probably should be some escaping around the place but it works fine for me:
Code:
sed "s/between[^)]*/between '$(date --date="- 1 month" +%Y-%m)-01' and '$(date --date="+ 3 months" +%Y-%m)-01'/" /var/tmp/p.sql
grail, you are much more patient than I am. You've written so many of his scripts, you should be getting a paycheck by now.

The pattern seems to keep repeating itself:
http://www.linuxquestions.org/questi...e-line-901450/
http://www.linuxquestions.org/questi...g-bash-902673/
http://www.linuxquestions.org/questi...ng-awk-896800/
http://www.linuxquestions.org/questi...script-896608/
http://www.linuxquestions.org/questi...ectory-894137/
 
Old 09-24-2011, 02:25 PM   #8
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 294

Original Poster
Rep: Reputation: 10
Quote:
Originally Posted by TB0ne View Post
hmm i felt bad

hmmm @ Grail Thanks man

Last edited by anishkumarv; 09-24-2011 at 02:28 PM.
 
Old 09-24-2011, 03:12 PM   #9
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,613

Rep: Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962
Quote:
Originally Posted by anishkumarv View Post
hmm i felt bad
Really?? From another of your posts:
Quote:
Originally Posted by aniskumarv
i dont feel ashame never ever!! i know my values and goals, as a senior member i didnt expect from you, words are most harmful than bullets..hmmm days are not to far to become master in linux domain,
Again, I really hope you do advance...but until you start applying what you've been told, learning on your own, and stop asking to be spoon-fed answers, you won't. Instead of posting over and over "I get this error when I ran the command..what do I do??", try experimenting with it, on your own. Figure out your own answers.

After over a year here, you're not a 'newbie' anymore...you should be able to figure out how to write a simple script, use sed and awk, and make things work
 
1 members found this post helpful.
Old 09-24-2011, 03:36 PM   #10
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 294

Original Poster
Rep: Reputation: 10
hmmm sure i will, i can.
 
  


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 calculations on Sun sparc ukursat Linux - Newbie 1 12-05-2008 10:01 PM
shell script to find modified date and last accessed date of any file. parasdua Linux - Newbie 6 04-22-2008 09:59 AM
need help with pi calculations! daven1 Programming 2 01-06-2005 07:21 PM
Date calculations in BASH script Crashman Programming 4 07-03-2004 10:15 AM

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

All times are GMT -5. The time now is 11:17 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
Open Source Consulting | Domain Registration