Visit The New, Modern Unix Linux Community


Getting month older


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting month older
# 1  
Getting month older

I need month older lines from a file
Code:
file
1        666      2013-08-23    04:24:11     
33       543     2013-06-11    18:04:20     
33       413     2013-06-20    1:40:54 
1        776      2013-08-20    18:04:21     
.0       877      2013-08-21    05:50:04

I'm checking older lines from third field, if it is older than 30 days delete the line and before deleting save the second field of the line to another file.
Code:
Output:
new.file
543
413
 
file
1        666      2013-08-23    04:24:11     
1        776      2013-08-20    18:04:21     
.0       877      2013-08-21    05:50:04

I'm using the below one..
Code:
awk -v cudate=`date "+20%y%m%d"`  '{line=$0; dateval=$3;FS="-"; $0=dateval;  thisdate=$3*10000+$1*100+$2; if (thisdate>cmpdate) print line; FS=" ";}' file


Last edited by Roozo; 08-23-2013 at 01:19 AM.. Reason: Removed formatting
# 2  
Need GNU date command:

Code:
#! /bin/bash
d=$(date -d "-30 days" +%Y%m%d%H%M%S)

awk -v d=$d '{split($3,a,"-");split($4,b,":")
    s=sprintf("%04d%02d%02d%02d%02d%02d",a[1],a[2],a[3],b[1],b[2],b[3])
    if (s<d) { print $2> "outputfile1"}
    else {print > "outputfile2"}
    }' file
echo mv outputfile2 file

if command is right, remove the red echo
# 3  
Thanks....
But I'm using KSH. GUN date command is throwing error.
Code:
-d

option is not working
# 4  
If your ksh is ksh93 you can also use the builtin date formatting capability:
Code:
d=$(printf "%(%Y%m%d%H%M%S)T" "30 day ago")

To check for ksh93 just use
Code:
print $KSH_VERSION

If the output is not empty, you have ksh93.
This User Gave Thanks to hergp For This Post:
# 5  
No, Not using ksh93
# 6  
What OS and version are you on?
# 7  
OS: HP-UX
Version: U
Release:0.58

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #260
Difficulty: Easy
Steve Jobs and Steve Wozniak co-founded Apple in 1976 to sell Wozniak's Apple I personal computer.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Hadoop directories delete older than a month

-bash-4.1$ hdfs dfs -ls -R /data/backup/prd/xyz/ | grep '^d' drwxr-xr-x - abisox abadmgrp 0 2018-05-05 01:03 /data/backup/prd/xyz/20180301 drwxr-xr-x - abisox abadmgrp 0 2018-05-05 01:03 /data/backup/prd/xyz/20180302 drwxr-xr-x - abisox abadmgrp 0 2018-05-05 01:03... (2 Replies)
Discussion started by: himanshupant
2 Replies

2. Shell Programming and Scripting

Need last month files after 10th of every month

Hi, I need all file names in a folder which has date >= 10th of last month, Example : files in folder AUTO_F1_20140610.TXT BUTO_F1_20140616.TXT CUTO_F1_20140603.TXT FA_AUTO_06012014.TXT LA_AUTO_06112014.TXT MA_AUTO_06212014.TXT ZA_AUTO_06232014.TXT Output: AUTO_F1_20140610.TXT... (9 Replies)
Discussion started by: nani1984
9 Replies

3. Shell Programming and Scripting

How to add decimal month to some month in sql, php, perl, bash, sh?

Hello, i`m looking for some way to add to some date an partial number of months, for example to 2015y 02m 27d + 2,54m i need to write this script in php or bash or sh or mysql or perl in normal time o unix time i`m asking or there are any simple way to add partial number of month to some... (14 Replies)
Discussion started by: bacarrdy
14 Replies

4. Shell Programming and Scripting

Convert From Month Number to Month Name

Hi, I have a script that accepts an input date from the user in yyyy-mm-dd format. I need to get the mm-dd part and convert it to month name. example: 2011-11-15 I want that to become "Nov 15" I don't have the GNU date, I am using an AIX os. Thanks. (1 Reply)
Discussion started by: erin00
1 Replies

5. Shell Programming and Scripting

Help with getting last date of previous month and first date of previous 4th month from current date

I have requirment to get last date of previous month and the first date of previous 4th month: Example: Current date: 20130320 (yyyymmdd) Last date of previous month: 20130228 (yyyymmdd) First date of previous 4th month: 20121101 (yyyymmdd) In my shell --date, -d, -v switches are not... (3 Replies)
Discussion started by: machomaddy
3 Replies

6. Shell Programming and Scripting

find file older than one month not by x days olds

Hi, I would like to ask about some question on the -mtime option in the find command. I want to move a log files older than one month and planning to used the find -mtime +30 but i have some clarrification does -mtime +30 or -30 refer to x days beyond or between so how about the month. suppose... (2 Replies)
Discussion started by: jao_madn
2 Replies

7. Shell Programming and Scripting

Script to counting a specific word in a logfile on each day of this month, last month etc

Hello All, I am trying to come up with a shell script to count a specific word in a logfile on each day of this month, last month and the month before. I need to produce this report and email it to customer. Any ideas would be appreciated! (5 Replies)
Discussion started by: pnara2
5 Replies

8. UNIX for Dummies Questions & Answers

print previous month (current month minus 1) with Solaris date and ksh

Hi folks month=`date +%m`gives current month Howto print previous month (current month minus 1) with Solaris date and ksh (7 Replies)
Discussion started by: slashdotweenie
7 Replies

9. Shell Programming and Scripting

Zipping files older than one month

I have to zip all files older than a month within a directory. I have to archive them using the file extension I have .dat, .csv ,.cnt files within the directory. I used the following command It doesnt work find /path/*.dat -mtime +30 This command doesnot display .dat files older than a... (2 Replies)
Discussion started by: ramky79
2 Replies

10. UNIX for Dummies Questions & Answers

rm files older than ...

Hello, How can I remove files older than yesterday or the day before or a given day ... Thank you in advance (2 Replies)
Discussion started by: annemar
2 Replies

Featured Tech Videos