View previous topic :: View next topic |
Author |
Message |
NewBlackDak Guru
Joined: 02 Nov 2003 Posts: 512 Location: Utah County, UT
|
Posted: Tue Mar 15, 2005 9:48 pm Post subject: How do I read certain set of characters in a file? |
|
|
We have some files that I need to manipulate, and depending on what's in the file they're treated differently.
What I need is the last 20 characters in the file. Can I do this from a simple bash script, or do I need something more advanced?
Any help is much appreciated!! _________________ Gentoo systems.
X2 4200+@2.6 - Athy
X2 3600+ - Myth
UltraSparc5 440 - sparcy |
|
Back to top |
|
|
yaneurabeya Veteran
Joined: 13 May 2004 Posts: 1754 Location: Seattle
|
Posted: Tue Mar 15, 2005 9:54 pm Post subject: |
|
|
You can get something close to that using tail... but that counts lines. Maybe there's something in the man page about characters?
Or if you're brave ... there's always C . |
|
Back to top |
|
|
NewBlackDak Guru
Joined: 02 Nov 2003 Posts: 512 Location: Utah County, UT
|
Posted: Tue Mar 15, 2005 10:00 pm Post subject: |
|
|
yaneurabeya wrote: | You can get something close to that using tail... but that counts lines. Maybe there's something in the man page about characters?
Or if you're brave ... there's always C . |
Yeh I didn't want to have to go full out c or c++, but I just might have to. The problems is there are no LF or CR in the file for like the last 100 lines, so tail, more, and grep are out of the question. Anybody know if perl can do it? _________________ Gentoo systems.
X2 4200+@2.6 - Athy
X2 3600+ - Myth
UltraSparc5 440 - sparcy |
|
Back to top |
|
|
adsmith Veteran
Joined: 26 Sep 2004 Posts: 1386 Location: NC, USA
|
|
Back to top |
|
|
NewBlackDak Guru
Joined: 02 Nov 2003 Posts: 512 Location: Utah County, UT
|
Posted: Wed Mar 16, 2005 12:43 am Post subject: |
|
|
Maybe I should have been more clear. If you grep you get that 100 lines worth of stuff from the file. I'm unsure how I could use sed, because the only thing that will ever be the same in the files is that last few characters that I need. As for awk. I haven't time to read through 100 pages worth of docs to see if this is even possible. I just used a php script, and here are the important parts
Code: |
$file = $argv[1];
$size = filesize($file);
$size2 = $size - 19;
$handle = fopen($file, "r") or die ("error opening file " . $file);
fseek($handle,$size2);
$contents = fread($handle,$size);
fclose($handle);
return $contents;
|
The best part is it's very fast compared to grepping and awking even with some of the 100-200MB files I'm dealing with. Get the filesize fastforward to 19 b before the end of the file, and read the rest.
I'll go back and read the other stuff tomorrow when I have time to revisit it. _________________ Gentoo systems.
X2 4200+@2.6 - Athy
X2 3600+ - Myth
UltraSparc5 440 - sparcy |
|
Back to top |
|
|
|