delete record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting delete record
# 1  
Old 10-22-2008
delete record

I have a log file , that keep the record of system activities , as below , now the log is appending so that it becomes very large .

"
05/01/08 Normal userA
05/01/08 Normal userB
05/02/08 Alnormal userC
"
"
10/01/08 Normal userA
10/02/08 Normal userA
"


Now I would like to keep the log current , so if the date is over 60 days , then remove this row , like above , the first three row should be removed as 05/01/08 and 05/02/08 is over 60 days ( today is 10/08/08 ) , can advise how to do it ? thx
# 2  
Old 10-22-2008
hi,
below one can roughly meet your desire, but you need to modify it to be correct enough.

Code:
nawk -v d="10/08/08" 'BEGIN{
	split(d,brr,"/")
}
{
	split($1,arr,"/")
	days=(brr[1]-arr[1])*30+(brr[2]-arr[2])+(brr[3]-arr[3])*365
	if(days<60)
		print
}' filename

# 3  
Old 10-22-2008
You requirement need 2 steps.
1. Calculate the date 61 days ago, and the way you can do that depends on your OS or your "date" version, search the forum for "date time calculation"
2. If your "sed" support -i option you can delete from beginning until date, if not you have to look for another way.
# 4  
Old 10-23-2008
Quote:
Originally Posted by summer_cherry
hi,
below one can roughly meet your desire, but you need to modify it to be correct enough.

Code:
nawk -v d="10/08/08" 'BEGIN{
	split(d,brr,"/")
}
{
	split($1,arr,"/")
	days=(brr[1]-arr[1])*30+(brr[2]-arr[2])+(brr[3]-arr[3])*365
	if(days<60)
		print
}' filename



thx ,

As you told me , it needs to modify sth, i found two thing need to be modify ,
1. it only print the line meet the condition , how can I remove the line ?
2. the current idate s fixed , how to change it to today ?

thx
# 5  
Old 10-23-2008
Hammer & Screwdriver NOt exactly what you asked, but an interesting approach

A datafile, with dates mm/dd/yy before other text
Code:
> cat file296
05/01/08 blah
05/01/08 more blah
05/02/08 yep
06/01/08 ummmm
06/02/08 hmmmm
06/02/08 arrgggg
06/03/08 cool
07/04/08 hot hot hot
07/04/08 hooray

script that adjusts date to yy/mm/dd so can get last three (head -3) dates to keep
Code:
> cat order_file296
awk '{print substr($0,7,2)"/"substr($0,1,2)"/"substr($0,4,2)"|"substr($0,1,8)}' file296 | sort -u >file296.dte
sort -rn file296.dte | head -3 | cut -d"|" -f2 >file296.good

egrep -f file296.good file296 >file296.keep

echo "original"
cat file296
echo "keep stuff "
cat file296.keep

program execution shows the original file, and only those records that were of the last three days of entries.
Code:
> order_file296 
original
05/01/08 blah
05/01/08 more blah
05/02/08 yep
06/01/08 ummmm
06/02/08 hmmmm
06/02/08 arrgggg
06/03/08 cool
07/04/08 hot hot hot
07/04/08 hooray
keep stuff 
06/02/08 hmmmm
06/02/08 arrgggg
06/03/08 cool
07/04/08 hot hot hot
07/04/08 hooray
>

So, this approach sort of does what you are looking to do. Depends on the data you have - if there is an entry for everyday, then this logic would be fine. Probably a couple other tweaks necessary, but I leave those to you to figure out.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete only if duplicates found in each record

Hi, i have another problem. I have been trying to solve it by myself but failed. inputfile ;; ID T08578 NAME T08578 SBASE 30696 EBASE 32083 TYPE P func just test func chronology func cholesterol func null INT 30765-37333 INT 37154-37318 Link 5546 Link 8142 (4 Replies)
Discussion started by: redse171
4 Replies

2. Shell Programming and Scripting

Delete last 2 fields from every record in a file

Sample file record : "20130617003","2013-06-18T07:00:03","OUTWARD","01001011","TEST PLC","","HFX834346364364","20130617","10","DUM87534758","","1.28","826","020201","65879278","","","","","","010101","56789","DUMMY... (3 Replies)
Discussion started by: bigbuk
3 Replies

3. Shell Programming and Scripting

Delete record filter by column

Dear friend, I have a file 2 files with column wise FILE_A ------------------------------ x,1,@ y,3,$ x,5,% FILE_B -------------------- x,1,@ i like to delete the all lines in FILE_A ,if first column available in FILE_B. output (in FILE_A) y,3,$ x,5,% (10 Replies)
Discussion started by: Jewel
10 Replies

4. Linux

perl program to delete the complete record

Hi all, I want a perl program to delete the record and its contents from a file if there is no particular line in the record given that all records are separated by a blank line. For example: #100 abcd efgh hijk 123 klm #200 abcd efgh hijk klm So, the pattern here is 123. If... (0 Replies)
Discussion started by: kaav06
0 Replies

5. Shell Programming and Scripting

delete text from each record in a file

Hi guys, I have been given a small task to do and I am stuck already. I have to format a file with people's emails address in it ready for pasting into the BCC section of an email. The file looks like this:- bob@ibm.com SMTP BOB SMITH text text text sue@icl.org SMTP Susy Smith text text... (8 Replies)
Discussion started by: joe_evans
8 Replies

6. Shell Programming and Scripting

How to delete 1 record in large file!

Hi All, I'm a newbie here, I'm just wondering on how to delete a single record in a large file in unix. ex. file1.txt is 1000 records nikki1 nikki2 nikki3 what i want to do is delete the nikki2 record in file1.txt. is it possible? Please advise, Thanks, (3 Replies)
Discussion started by: nikki1200
3 Replies

7. Shell Programming and Scripting

How to delete first record from all the file?

hi All, need help...!! I want to delete header record from all the files in current directory. using sed command i can delete first record from a file but i want to delete first record from all the files so can anybosy help me how can i do this? I will appreciate your help. (3 Replies)
Discussion started by: NirajThakar
3 Replies

8. Shell Programming and Scripting

prompt to delete each record when pattern is found

Hello!. I am working on a very simple program and I have been trying different things. This is so far what I have done and there is one small detail that still does not work. It finds all the records in a phonebook per say: ./rem Susan More than one match; Please select the one to remove: ... (3 Replies)
Discussion started by: bartsimpsong
3 Replies

9. UNIX for Dummies Questions & Answers

Delete a single record from a file

Hello all, Is there a function for deleting a single record from a file? Thanks in advance... (4 Replies)
Discussion started by: klafte
4 Replies

10. UNIX for Dummies Questions & Answers

How to delete a record from a csv file

Hi Guys I have downloaded a table from oracle database in .csv format. it has many fields as Title, First Name, Last Name etc. I have to download distinct titles from database and now i have to check all those titles from data of First Name one by one. and then i have to delete matched record.... (1 Reply)
Discussion started by: Rajeev Agrawal
1 Replies
Login or Register to Ask a Question