sed -n '10000000,10000020p' filename
You might be able to speed that up a little like this:
sed -n '10000000,10000020p; 10000021q' filename
In those commands, the option -n
causes sed
to "suppress automatic printing of pattern space". The p
command "print[s] the current pattern space" and the q
command "Immediately quit[s] the sed script without processing any more input..." The quotes are from the sed
man
page.
By the way, your command
tail -n 10000000 filename | head 10
starts at the ten millionth line from the end of the file, while your "middle" command would seem to start at the ten millionth from the beginning which would be equivalent to:
head -n 10000010 filename | tail 10
The problem is that for unsorted files with variable length lines any process is going to have to go through the file counting newlines. There's no way to shortcut that.
If, however, the file is sorted (a log file with timestamps, for example) or has fixed length lines, then you can seek into the file based on a byte position. In the log file example, you could do a binary search for a range of times as my Python script here* does. In the case of the fixed record length file, it's really easy. You just seek linelength * linecount
characters into the file.
* I keep meaning to post yet another update to that script. Maybe I'll get around to it one of these days.
The shell/OS imposed limit is usually one or two hundred thousand characters.
getconf ARG_MAX
will give you the maximum input limit for a command. On the Debian system I currently have a terminal open on this returns 131072
which is 128*1024
. The limit is reduced by your environment variables and if my memory serves me correctly these are passed in the same structure by the shell, though that will only take off a few hundred characters in most cases. To find an approximation of this value run env | wc -c
- this suggests 325 characters at the current time on this login on this machine.
Scripts are likely to permit this full length, but it is not unlikely that other utilities will impose their own limits either intentionally or through design issues. There may also be artificial limits to how long an individual argument on a long command line can be, and/or how long a path to a file can be.
Best Answer
A lot of *nix commands have a '--' option, which means "this is where the options end, anything from here onwards that looks like an option, isn't".
rm -- -filename
Not 100% sure if rm supports that, I'm a bit rusty.