Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 10-02-2007, 04:51 AM   #1
LQ Newbie
Registered: Oct 2007
Posts: 1

Rep: Reputation: 0
Simple Log File Purge

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,
Old 10-02-2007, 12:50 PM   #2
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
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,376

Rep: Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755
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
LQ Newbie
Registered: Oct 2007
Posts: 1

Original Poster
Rep: Reputation: 0

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


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 > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:07 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration