PDA

View Full Version : shell script to mv lines from log file to other file



depele
October 6th, 2008, 08:09 AM
Hello,

I am looking for a shell script to move all lines from a log file starting with date before1/7/2008 to another file and then gzip it.

any suggestions.

thanks

Greyed
October 6th, 2008, 08:12 AM
Your sentence is unclear. Do the lines start with the date or the file? If the lines, what is the format of the line?

depele
October 6th, 2008, 08:16 AM
1 file
multiple lines starting with date

mv all lines date before 1/7/2008

I am looking for a preview now.

grtz...

depele
October 6th, 2008, 08:42 AM
will this line do to cp all files?

cat logfile.log | grep "2008-07-*" >> test.txt


thanks in advance ?

ghostdog74
October 6th, 2008, 09:05 AM
show a sample of your file.

depele
October 6th, 2008, 09:36 AM
2007-12-08 13:07:44,887 ************************OK
2007-12-08 13:07:44,887 ************************************************** ************************************************** *
2007-12-08 13:07:44,928 DEBUG ************************************************** ************
2007-12-08 13:07:44,928 DEBUG ************************************************** ****************
<Destination>***************</Destination>
<Origin Role="DEFAULT_ROLE" OrgUnit="***************">*************</Origin>
*****************

2007-12-08 13:07:44,929 ************************************************** *

...... and so on.

==> all dates has to be all dates until 2008-07-01


grep "2008-[0-7]-*" > text.txt ==> will not work.

ghostdog74
October 6th, 2008, 10:01 AM
describe what you want clearly. It obviously did not work because you are grepping for 2008

depele
October 6th, 2008, 10:18 AM
There are also 2008 entries in the file. So that is not the problem.

I have a file (structured as above).
I need to add all lines starting with 2008-01-01 till 2008-07-01 to old.log
and I need to do the same
with all lines 2008-07-02 to new.log



is it clear now?

thank you

geirha
October 6th, 2008, 11:05 AM
Try with
egrep '^(2008-0[1-6]-|2008-07-01).*'

Greyed
October 6th, 2008, 11:11 AM
I have a file (structured as above).
I need to add all lines starting with 2008-01-01 till 2008-07-01 to old.log
and I need to do the same
with all lines 2008-07-02 to new.log


A file?



from datetime import date

infile = 'filename'
oldfile = 'filename.old'
newfile = 'filename.new'
div = date(2008,7,2)
log = open(infile,'r')
new = open(newfile,'w')
old = open(oldfile,'w')

for line in log:
words = line.split()
stamp = words[0].split('-')
filedate = date(stamp[0], stamp[1], stamp[2])
if filedate < div:
old.write(line)
else:
new.write(line)
Off-the-hip and untested but the gist is there. Python, of course, not shell. Basically, open the files needed, set a date object to the dividing line, read the lines in, parse out the date, construct a date object from it, compare the timestamp of the line to the dividing line, older than it write it to the old file, newer write to the new file. Could be shorter but wouldn't be as clear.

However if you're really needing it in shell for who-knows-what reason I defer to ghostdog74. My answer to "shell script" is Python. ;)

depele
October 6th, 2008, 01:01 PM
thanks a lot.

I'm working on it.

grtz

ghostdog74
October 6th, 2008, 01:17 PM
However if you're really needing it in shell for who-knows-what reason



egrep '^(2008-0[1-6]-|2008-07-01).*'


there's the reason : short and simple vs long winded. :)

skotos
October 6th, 2008, 02:00 PM
If you used logrotate even with the files you are generating, the system would simply manage the rotation for you.:guitar:

Thus you might simply set a cron job - a bash one might be good enough - and complete the work, day by day, by taking away the rotated files, automatically compressed, if you need them to be compressed, gzipped, tarred or whatever.
:popcorn:

depele
October 6th, 2008, 03:20 PM
If you used logrotate even with the files you are generating, the system would simply manage the rotation for you.:guitar:

Thus you might simply set a cron job - a bash one might be good enough - and complete the work, day by day, by taking away the rotated files, automatically compressed, if you need them to be compressed, gzipped, tarred or whatever.
:popcorn:

==> software not in my power to change logs everything in 1 file so the file keep on growing.
I already requested a change. But.........

it keeps on going

Greyed
October 6th, 2008, 06:56 PM
there's the reason : short and simple vs long winded. :)

I could have used regex as well. I decided against it since split was perfectly servicable. Also that egrep magic isn't splitting like he requested.

ghostdog74
October 7th, 2008, 01:02 AM
Also that egrep magic isn't splitting like he requested.

yes it sure does. Mind showing why it doesn't?

Greyed
October 7th, 2008, 01:12 AM
Ye requested lines to go into TWO files. The egrep line, on its own, can only extract either lines that do match or do not match. Therefore you need two passes across the file to get the same results that my script does in a single pass. Also a single regex compile.

ghostdog74
October 7th, 2008, 02:34 AM
Ye requested lines to go into TWO files. The egrep line, on its own, can only extract either lines that do match or do not match. Therefore you need two passes across the file to get the same results that my script does in a single pass. Also a single regex compile.
i see what you mean.


awk '/^(2008-0[1-6]-|2008-07-01)/{print >"testfile"}
/2008-07-02/{print> "nextfile"}' file