LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-15-2011, 06:22 AM   #1
freakin.raja
LQ Newbie
 
Registered: Sep 2011
Posts: 5

Rep: Reputation: Disabled
script to read and delete a line from a file, if the line is 30 days old


Hi All,
I need a script where in i need read a file and delete lines based on a criteria.


The content of the file is like below


Sun Aug 14 10:11:28 CST 2011| Maximum line count reached. CTPVoIPTCA file does not include all known VoIP devices
Wed Sep 14 10:11:28 CST 2011| Maximum line count reached. CTPVoIPTCA file does not include all known VoIP devices


Here i need to get the date from line by line before "|" and delete the line if it is 30days old.

So if i execute the script the line starts with Sun Aug 14 should be deleted because it more than 30 days.

Can anyone please help me with this???
 
Old 09-15-2011, 07:50 AM   #2
kakaka
Member
 
Registered: Sep 2003
Posts: 382

Rep: Reputation: 87
This a straight forward brute force awk script that should do the job, if you take the existing log file as input and direct the output to a replacement log file which will not contain lines older than 30 days. Using your two lines of sample data, it only provided the newest line as output.



Code:
BEGIN {
		month_strs="JanFebMarAprMayJunJulAugSepOctNovDec"
		secs_in_30_days = 86400 * 30
		time_now_in_secs = systime()
      }

{
    log_time_in_secs = mktime(  substr(  $6 , 1 ,  4  )  " "  (  ( index( month_strs , $2  ) - 1  ) / 3 ) + 1   " "  $3  " "  substr(  $4 ,  1  ,  2  )  " "  substr(  $4 ,  4  ,  2  )  " "  substr(  $4 ,  7  ,  2  )   " "  ( (  $5  ==  "CDT"  )  ?  1  :  0  )   )

    if (  ( time_now_in_secs - log_time_in_secs  )  <=  secs_in_30_days  )
       print $0
}
Hope this helps.
 
Old 09-15-2011, 09:45 AM   #3
freakin.raja
LQ Newbie
 
Registered: Sep 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
Hi Kakaka,
Thanks for your response, it would be great if you could let me know, how did you give input file to this script. I am not much familiar with awk scripting.
 
Old 09-15-2011, 10:07 AM   #4
freakin.raja
LQ Newbie
 
Registered: Sep 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
Hi Kakaka,

Does this mktime() method works only in Linux or it does work in Solaris also?
 
Old 09-15-2011, 10:23 AM   #5
freakin.raja
LQ Newbie
 
Registered: Sep 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
when i tried to run the script i am getting errors

Code:
#!/usr/bin/awk -f

BEGIN {
		month_strs="JanFebMarAprMayJunJulAugSepOctNovDec"
		secs_in_30_days = 86400 * 30
		time_now_in_secs = systime()
      }

{
    log_time_in_secs = mktime(  substr(  $6 , 1 ,  4  )  " "  (  ( index( month_strs , $2  ) - 1  ) / 3 ) + 1   " "  $3  " "  substr(  $4 ,  1  ,  2  )  " "  substr(  $4 ,  4  ,  2  )  " "  substr(  $4 ,  7  ,  2  )   " "  ( (  $5  ==  "CDT"  )  ?  1  :  0  )   )

    if (  ( time_now_in_secs - log_time_in_secs  )  <=  secs_in_30_days  )
       print $0
}
I m getting the following error

awk: syntax error near line 6
awk: illegal statement near line 6
awk: syntax error near line 10
awk: illegal statement near line 10


looks like systime() and mktime() is not working for me..

this my server details

uname -a
SunOS jstz0201 5.10 Generic_142900-06 sun4v sparc SUNW,SPARC-Enterprise-T5220
 
Old 09-16-2011, 05:33 AM   #6
kakaka
Member
 
Registered: Sep 2003
Posts: 382

Rep: Reputation: 87
Hi freakin.raja,

I take it that you did discover how to get input to the program.

Since we are having this discussion on LinuxQuestions.org, not surprisingly I am using gawk on Linux; yes systime() and mktime() are from gawk.

Given that you are using SunOS, I understand that you may wish to use only things that come with it to script a task. But just in case you might be open to something else, I should mention that some people have installed and run gawk successfully on SunOS.

Your uname output which shows SunOS 5.10, means you've got what, Solaris 10?

I'm sorry but at the moment I'm not able to fire up my old Sparc machines, and even if I were, they run much older versions of Solaris. So I have no way of testing anything on a live Solaris 10 machine.

I considered writing replacement systime() and mktime() functions to work with nawk on Solaris. That would rely on output from the Solaris date command. The date command on Linux has the necessary capabilities. But according to Oracle's on-line man pages, the date command on Solaris 10 doesn't seem to have what's needed.

Since I've used perl on much older versions of Solaris, and Oracle shows a man page for perl with Solaris 10, you should be able to use a perl script.

I've attached one I tested on Linux with the data you provided. In principle, the perl on Solaris 10 should be new enough to handle the capabilities I used in the script.

BTW, if you are going to ask non-Linux questions on LinuxQuestions.org, AFAIK, the Programming forum might be appropriate. If have questions that are somewhat specific to Solaris, there are other sites which might be more helpful . For example, there is a Solaris forum at http://www.unix.com/.

Hope this helps.
Attached Files
File Type: txt del_gt_30days.txt (839 Bytes, 9 views)
 
Old 09-16-2011, 08:04 PM   #7
ramram29
Member
 
Registered: Jul 2003
Location: Miami, Florida, USA
Distribution: Debian
Posts: 848
Blog Entries: 1

Rep: Reputation: 47
I'd do the following. Write a small bash script that will accept the file as input, parses it, subtracts the dates and prints the dates that are less then the days specified; like so:

#!/bin/bash

SYNTAX="$0 <file>"
if [ $# -lt 1 ]; then
echo $SYNTAX
exit 1
fi

DAYSBACK=30
FILE=$1
AGODATE=`date -d "$DAYSBACK days ago" +%Y%m%d%H%M%S`

cat $FILE | while read LINE;
do
LINEDATEORIG=`echo $LINE | awk -F "|" '{print $1}'`
LINEDATE=`echo $LINE | awk -F "|" '{print $1}' | date -d "$LINEDATEORIG" +%Y%m%d%H%M%S`
if [ ! $LINEDATE -lt $AGODATE ]; then
echo $LINE
fi
done

To change the days going back go into the script and change the variable DAYSBACK. Provided the list is called datelist.txt, then all you have to do is pass the list as an argument, like so:

$ ./dateago.sh datelist.txt
Wed Sep 14 10:11:28 EDT 2011| Maximum line count reached. CTPVoIPTCA file does not include all known VoIP devices

If you want to send the result to another file then add the following:

$ ./dateago.sh datelist.txt > newdatelist.txt

There may be a better way; but there's always a thousand different ways to script your way out of a problem.
 
Old 09-17-2011, 09:52 AM   #8
freakin.raja
LQ Newbie
 
Registered: Sep 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
Hi RamRam,

Thank you so much.

---------- Post added 09-17-11 at 08:52 AM ----------

Hi RamRam,

Thank you so much.
 
  


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
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 07:07 PM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 02:43 PM
Script to read line by line from a file kushalkoolwal Programming 20 01-27-2006 05:17 AM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 11:24 PM


All times are GMT -5. The time now is 08:38 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration