Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

removing data from a log file with specific date range

Status
Not open for further replies.

visvid

Technical User
Jun 3, 2002
131
GB
Wonder if anyone can help.

I have a log which starts from Dec 4 and is constantly updated. Rather than empty the log, I want to just keep the last 7 days worth.

I have tried find . -mtime +7 , but this cmd works well when looking for files within a filesystem, I want to just edit that log file ,

I have tried some awk cmds, but unsure how to progress further .

Dec 4 21:49:10 cspcws snmpd[8776]: NOTICE: SMUX relation started with (172.30.5.253+54268+985) is the start of the log

Jan 2 23:38:36 cspcws snmpd[8776]: EXCEPTIONS: simpleOpen rejected (badIdentity): enterprises.1031.1.1 (SMUX 172.30.5.253+50404+2371)

Is the end of the file
 
This may get you started.

BEGIN { m["Jan"]=1;m["Feb"]=2;m["Mar"]=3;m["Apr"]=4
m["May"]=5;m["Jun"]=6;m["Jul"]=7;m["Aug"]=8
m["Sep"]=9;m["Oct"]=10;m["Nov"]=11;m["Dec"]=12
}
{
if (NF < 3) next
m1 = m[$1]
d1 = $2
if (!flg && m1 < hm) flg = 1
hm = m1
if (m1 > mm) next
if (flg || m1 > mm || (m1 == vm && d1 >= dd)) print
hm = m1
}
~
~
~
~

Run it by entering

awk -f above-file -v mm=12 -v dd=27 in.log > out.log

where mm and dd are the month and day of the first date you want to keep.

If you double post a question, it is a good idea to mention that in your posts so someone doesn't waste time solving a problem that has already been solved. CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top