The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
delete n last lines of a file ncatdesigner Shell Programming and Scripting 7 05-17-2008 07:07 AM
delete first 100 lines from a file salaathi SUN Solaris 3 11-15-2007 12:01 AM
delete first 100 lines rather than zero out of file thepurple SUN Solaris 7 11-14-2007 09:35 AM
delete the lines from file sameersam Shell Programming and Scripting 2 04-03-2006 10:32 PM
delete all lines in file strok UNIX for Dummies Questions & Answers 6 03-11-2002 06:27 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-06-2006
Registered User
 

Join Date: Feb 2006
Posts: 3
Delete lines in a file

I wish to delete lines from a log file. Should take a date string as a variable. Find the date in the file, get the line number for the string. Delete lines from 1 to that (line number - 1), all from within a shell script. Is that possible ?.

Thnks,

Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 02-06-2006
Registered User
 

Join Date: Nov 2004
Posts: 68
umal,

Yes you can. Use sed, cat and grep then you shou,d be able to do that or sed and awk.
Reply With Quote
  #3 (permalink)  
Old 02-07-2006
linuxpenguin's Avatar
Registered User
 

Join Date: May 2002
Location: India
Posts: 295
line=`grep -n "${datevariable}" log_filename|cut -d":" -f1`
sed "1,${line} d" log_filename
__________________
War doesnt determine who is right, it determines who is left
Reply With Quote
  #4 (permalink)  
Old 02-07-2006
Registered User
 

Join Date: Feb 2006
Posts: 3
I tried this out , sed "1,${line} d" log_filename

This leaves the log_filename as it is. I would have to
sed "1,${line} d" log_filename > log_filename.new
mv log_filename log_filename.old
mv log_filename.new log_filename

the log file is constantly being written to, so how can I guarantee there wont be any loss of data...

I wish to delete lines from the log as with "vi". Besides wont inodes change ?
Reply With Quote
  #5 (permalink)  
Old 02-07-2006
Registered User
 

Join Date: Jul 2005
Posts: 137
Code:
ruby -i -ne 'BEGIN{S=ARGV.shift}
  f=true if $_.index(S);print if f' "Dec 13" myfile
Reply With Quote
  #6 (permalink)  
Old 02-07-2006
Registered User
 

Join Date: Feb 2006
Posts: 3
What does ruby mean ?
Reply With Quote
  #7 (permalink)  
Old 02-08-2006
linuxpenguin's Avatar
Registered User
 

Join Date: May 2002
Location: India
Posts: 295
i dont have idea bt ruby
but as far as maintaining the same file inode, try this

perl -pie "s/.....
the idea here is to use perl with flags -pie, doubt if you have perl installed.
I have posted this solution of perl -pie on numerous occassions where a temp file creation is to be omitted.
you can find it in some searches.
__________________
War doesnt determine who is right, it determines who is left
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 10:34 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0