Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove date from log files

Tags:

regex

sed

awk

I have an irc log as follows

04/14/13 21:38<@Hamatti> Lorem ipsum dolor sit amet
04/14/13 21:39<@Hamatti> consectetur adipiscing elit.
04/14/13 21:45<@Hamatti> Duis facilisis convallis lacus

EDIT1. sorry, I was not clear with my intentions. So I would want the output for the previous to be

21:38<@Hamatti> Lorem ipsum dolor sit amet
21:39<@Hamatti> consectetur adipiscing elit.
21:45<@Hamatti> Duis facilisis convallis lacus

so the time is important.

EDIT2 There is also part of the logs, the earlier ones in format

20:12<@Hamatti> Something.
20:13<@Hamatti> Funny.
20:13<@Hamatti> Happened.

and I need those lines to be non-modified.

and since my old logs are in format with no date stamp, I would like to remove the date from later logs.

sed 's/[0-9]{2}\/[0-9]{2}\/[0-9]{2}//g' logfile

Regex in the sed seems to work in regex testers but this sed is not doing anything. I wonder where is the problem? Any tips with bash tools (sed, awk, etc) are highly welcomed. Since only part of the logs have the date, I can't use something like

awk '{$1 = ""; print}'

because I would need the checking first.

like image 681
Hamatti Avatar asked Oct 24 '25 00:10

Hamatti


1 Answers

This sed command will do the job:

sed -i.bak 's/^[^<]*//' logfile

EDIT: Based on your comment, this will only clear date part and preserve the timestamp:

sed -i.bak 's/^[^ ]* //' logfile

EDIT 2: Based on your 2nd time edited question:

sed -i.bak 's#^[0-9]*/[0-9]*/[0-9]* ##' logfile

OR use extended regex capability in sed like this:

Mac:

sed -E -i.bak 's#^[0-9]{1,2}/[0-9]{1,2}/[0-9]{1,2} ##' logfile

Linux:

sed -r -i.bak 's#^[0-9]{1,2}/[0-9]{1,2}/[0-9]{1,2} ##' logfile
like image 196
anubhava Avatar answered Oct 26 '25 17:10

anubhava