LinuxQuestions.org
Visit Jeremy's Blog.
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 10-02-2007, 04:51 AM   #1
kellyj
LQ Newbie
 
Registered: Oct 2007
Posts: 1

Rep: Reputation: 0
Simple Log File Purge


Hi!
I am new to shell scripting and am having some trouble implementing a simple file purge script. What I want to do is search for a word in a file ie a datestamp and then delete all lines in the file before that specific datestamp.

What I have so far is a grep -n command to get the line number/ It comes in the format 'lineumber:datestamp' is there anyway to get rid of the ':datestamp' portion.

Once I have this linenumber is there an sed command that could be used to delete all lines up to this point?
Any suggestions would be very helpful!
Thanks in Advance,
JK
 
Old 10-02-2007, 12:50 PM   #2
cmnorton
Member
 
Registered: Feb 2005
Distribution: Ubuntu, CentOS
Posts: 585

Rep: Reputation: 35
Rewrite using head/tail

You can branch within sed, but I believe an easier way to do this is to use combinations of head/tail to isolate what you want, and then rewrite the file.
 
Old 10-02-2007, 07:48 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,430

Rep: Reputation: 2788Reputation: 2788Reputation: 2788Reputation: 2788Reputation: 2788Reputation: 2788Reputation: 2788Reputation: 2788Reputation: 2788Reputation: 2788Reputation: 2788
re head/tail; yep, that'll work. Look up the cut cmd (man cut), specifically the -d option to split the value on the ':'
 
Old 10-03-2007, 05:39 AM   #4
kellyj
LQ Newbie
 
Registered: Oct 2007
Posts: 1

Original Poster
Rep: Reputation: 0
Success!

Thanks guys for the suggestions what I have done now is use a grep -n and a cut command to isolate the linenumber followed by a head command in case there is more than one instance of the pattern (I only want the first) and then pass this variable to an sed command and it works fine


grep -n "pattern" log.txt | cut -f1 -d:>TEST.TXT
firstOccurenceLineNum=`head -1 TEST.TXT`


# Get linenum-1 of first instance and delete up to there
firstOccurenceLineNum=`expr $firstline - 1`
sed "1,$firstline d" <log.txt >TEST.TXT
 
  


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
how to purge a set of packages contained in a file? kpachopoulos Debian 4 02-11-2007 03:40 PM
email / file purge made between two dates mikmok Linux - General 3 12-29-2006 08:41 AM
any ideas to reduce log file size or make log file size managed? George2 Programming 2 08-13-2006 06:55 AM
Setting up simple file/print server to notify me of log activity across the LAN adamsjw2 Linux - Networking 1 04-10-2006 03:59 PM
iptables, changing log file from /var/log/messages acid2000 Linux - Networking 3 03-11-2003 08:38 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:29 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
Open Source Consulting | Domain Registration