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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trimming a log file 1

Status
Not open for further replies.

heprox

IS-IT--Management
Dec 16, 2002
178
US
Is there a man page for trimming a log file? Its not a log file but rather a SQL log or an ODBC log, in ASCII.
 
How about :

cp /path/to/logfile/logfilename /logfile/archive/archivename
> /path/to/logfile/logfilename

 
pls define 'trimming'.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
I want to remove x number of lines from the head of the file while leaving the latest and greatest....

 
heprox,

Depending on how many lines get written to the log file on a daily / weekly basis, you could use sed to trim the file.

To remove the first 20 lines from the file file1:

sed '1,20d' file1 > file2
mv file2 file1

Hope this helps.

John
 
forgot the code-tags...
Code:
#!/bin/bash
#
# remove REMOVE_LINES from file ORIG:
#
ORIG=$1
REMOVE_LINES=$2
tail -n$(echo "$(cat $ORIG | wc -l)-$REMOVE_LINES" | bc) $ORIG
 > .tmp.~tmp
mv .tmp.~tmp $ORIG

seeking a job as java-programmer in Berlin:
 
I want to remove x number of lines from the head of the file while leaving the latest and greatest
f=/path/to/logfile
x=NumberOfRecordsToRemove
The awk way:
cp $f $f.$$ && awk -v x=$x 'NR>x' $f.$$ > $f && rm -f $f.$$
The sed way:
cp $f $f.$$ && sed "1,${x}d" $f.$$ > $f && rm -f $f.$$
The ex way:
echo "1,${x}d\nwq" | ex -s $f
The tail way:
cp $f $f.$$ && tail +${x}l $f.$$ > $f && rm -f $f.$$
Another tail way:
cp $f $f.$$ && tail -n +${x} $f.$$ > $f && rm -f $f.$$

I use cp and rm instead of mv to preserve the file ownerships and permissions.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top