1

how to seek to a line and display it in a file on linux without opening the file? are there any commands that can be useful? The file is rather large and can't be opened in memory.

user881480
  • 5,005
  • 6
  • 32
  • 31
  • add more details, any programming language? what shell? what do you mean by 'without opening the file'? http://stackoverflow.com/questions/1429556/shell-bash-command-to-get-nth-line-of-stdout – Facundo Casco Oct 13 '11 at 23:01
  • There is no reason an entire file has to memory resident to search through it. Normally just little chunks are read at a time until the search is satisfied or eof is reached. Opening the file is pretty much non-negotiable. – Duck Oct 13 '11 at 23:09
  • If you expect to get at the contents without opening a file, I certainly hope you’re chummy with the superblock. – tchrist Oct 14 '11 at 00:36
  • "lines" are defined by the locations of "line separator" characters (in UN*X: CR, aka `\n`), which can occur anywhere in the file at any frequency. Unless your specific file has a far more rigid structure than that (like, all lines having the same number of characters), to make the position of the N-th line predictable somehow, there's no way to determine in advance where in the file "line `N`" starts without reading the entire file to that point and counting line separators as you go. You don't need to keep the entire file in memory while doing so - discard the already-processed parts. – FrankH. Oct 14 '11 at 09:18

2 Answers2

2

http://sed.sourceforge.net/sed1line.txt shows an example of showing a single line from a file. It must open the file to read it but should stream through the contents without loading the entire thing into memory.

sed '52q;d' # method 3, efficient on large files
Guerrero
  • 534
  • 4
  • 11
0

Do you have enough room on your file system to split the file up? If so, try this:

$ split -b 1024m <filename>  

This will split your file into as many parts as needed, in 1024MB chunks, typically with the name starting as xaa, xab, xac, and so on.

cat xa* | grep yourpattern

And when you're done:

rm xa*
andronikus
  • 4,125
  • 2
  • 29
  • 46