Extracting text from a file.
Hello everyone:
There are some log files that I wish to get some information from (Apache Access Log) but it is HUGE! All I need as of right now is any information from date and time A to date and time B. What commands can I use to extract this information from the access_log and put it into another file with just that information? I created a file called "access_info" by doing Code:
touch access_info |
grep should do the job.
grep [date] /path-to/filename. The date has to be in the same format used in the file. See man grep for more. |
So just to be sure before I do so, can I do something like this:
Code:
grep 2011-03-18 access_log > access_info |
I think so (I'm still learning grep).
You can test with grep 2011-03-18 access_log to see whether the desired output appears on the screen. Depending on your current directory, you may have to include the full path to access_log. |
You may want to adopt the practice of enclosing regular expressions in single quotes when invoking grep within bash. Bash uses some of the same meta-characters as used in regular expressions (even though they mean different things).
Code:
grep '2011-03-18' access_log |
Telengard, becoming proficient at regular expressions is next on my list.
Thanks. |
Quote:
A few tips you may find helpful:
HTH |
there are other tools besides grep that can do pattern matching, example awk, Perl, Python etc. I prefer Ruby.
Code:
$ ruby -ne 'print if /your date pattern/../next date pattern/' file |
Quote:
Code:
awk '/2011-03-18/' access_log Code:
while read ; do [[ "$REPLY" =~ 2011-03-18 ]] && echo "$REPLY" ; done < access_log |
Quote:
|
Quote:
|
Quote:
Code:
awk '/pattern1/,/pattern2/' file |
Quote:
|
the "awk" worked out great! Thanks again for everyones help! This really helped me on this big access_log file which was 7.2 GIGS
|
All times are GMT -5. The time now is 12:01 AM. |