views:

2064

answers:

5

Hi

I have a very long file which I want to print but skipping the first 1e6 lines for example. I look into the cat man page but I did not see nay option to do this. I am looking for a command to do this or a simple bash program. I know how to do it using a program in C but I want to do it using the common commands. Any way to do it? Thanks a lot in advance..

+16  A: 

you need tail.

$ tail great-big-file.log
< Last 10 lines of great-big-file.log >

if you really need to SKIP a particular number of lines, use

$ tail -n+<Lines to skip> <filename>
< filename, excluding first so many lines. >

If you want to just see the last so many lines, omit the "+":

$ tail -n<Lines to show> <filename>
< last so many lines of file. >
TokenMacGuy
This will print the LAST 1000000 lines, which isn't what you're asking about.
Eddie
Or "tail --lines=+<LinesToSkip> ..." for the readable-commands crowd :-)
paxdiablo
@Eddie: Saw that almost as soon as i clicked the button. Had to use man to sort it out.
TokenMacGuy
@tokenmacguy: Yup, after I made my post and comment, I saw that you updated your post.
Eddie
A: 

You can do this using the head and tail commands:

head -n <num> | tail -n <lines to print>

where num is 1e6 + the number of lines you want to print.

Dana the Sane
Not the most efficient answer since you'd need to do a "wc -l" on the file to get a line count, followed by an addition to add the million :-). You can do it with just "tail".
paxdiablo
I'm not sure, my understanding was that 1e6 would be known at the time of calling. Counting backwards isn't the fastest though.
Dana the Sane
+4  A: 

If you have GNU tail available on your system, you can do the following:

$ tail -n +1000000 huge-file.log

It's the + character that does what you want.

Eddie
+3  A: 

Hi,

This shell script works fine for me:

#!/bin/bash
awk -v initial_line=$1 -v end_line=$2 '{
    if (NR >= initial_line && NR <= end_line) 
    print $0
}' $3

Used with this sample file (file.txt):

one
two
three
four
five
six

The command (it will extract from second to fourth line in the file):

edu@debian5:~$./script.sh 2 4 file.txt

Output of this command:

two
three
four

Of course, you can improve it, for example by testing that all argument values are the expected :-)

SourceRebels
++ for using awk, which is oh so marginally more portable than tail
guns
A: 

I needed to do the same and found this thread.

I tried "tail -n +, but it just printed everything.

The more +lines worked nicely on the prompt, but it turned out it behaved totally different when run in headless mode (cronjob).

I finally wrote this myself:

skip=5
FILE="/tmp/filetoprint"
tail -n$((`cat "${FILE}" | wc -l` - skip)) "${FILE}"