Skip navigation

I needed to inspect a relatively small portion of a large log file (~1Gb), which will make chock even powerfull text-editors like vi(m) or emacs

I proceded in two steps:
1) found the match in the file and pulled the line number

—————————————————————————————-
awk ‘/ May 10 /{a=$0; b = NR;}END{print a,” :: “,b}’ log.txt
—————————————————————————————-
which yielded:
Thu May 10 02:17:05 ART 2012 :: 29199076

2) then I dumped the content from the that line and filtered it with head
—————————————————————-
tail -n +29199076 log.txt | head -n 100
—————————————————————-
That is possible with the trick of using “tail -n +(N)” which brings lines from the N line onwards

As and alternative to the last one, as explained here, sed could’ve been used in the following manner:
—————————————————————-
sed -n -e 29199076,29199176 -e 29199077q log.txt
—————————————————————-
(the last parameter, for efficiency, tells to quit at the limit line + 1 )

About these ads

2 Comments

  1. Or: Perl is great for getting a range between two matches, like

    perl -ane ‘print if /May 10/../May 11/’ a.txt

    If the end line cannot be found with a match, just get the file from that point and use head:

    perl -ane ‘print if /May 10/..0′ a.txt | head -10

    - Carsten

  2. Thanks Carsten,
    you are right, I often forget Perl’s power resourcing to more basic unix commands. Your suggestion has the benefit of simplicity by condensing two steps in one.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: