Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
How do I read certain set of characters in a file?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo
View previous topic :: View next topic  
Author Message
NewBlackDak
Guru
Guru


Joined: 02 Nov 2003
Posts: 512
Location: Utah County, UT

PostPosted: Tue Mar 15, 2005 9:48 pm    Post subject: How do I read certain set of characters in a file? Reply with quote

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
View user's profile Send private message
yaneurabeya
Veteran
Veteran


Joined: 13 May 2004
Posts: 1754
Location: Seattle

PostPosted: Tue Mar 15, 2005 9:54 pm    Post subject: Reply with quote

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 :D.
Back to top
View user's profile Send private message
NewBlackDak
Guru
Guru


Joined: 02 Nov 2003
Posts: 512
Location: Utah County, UT

PostPosted: Tue Mar 15, 2005 10:00 pm    Post subject: Reply with quote

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 :D.


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
View user's profile Send private message
adsmith
Veteran
Veteran


Joined: 26 Sep 2004
Posts: 1386
Location: NC, USA

PostPosted: Tue Mar 15, 2005 10:11 pm    Post subject: Reply with quote

sed, grep, and awk are your friends. In this case, particularly awk and sed.
some links:
http://www.gnu.org/software/gawk/manual/gawk.html
http://www.gnu.org/software/sed/manual/sed.html
Back to top
View user's profile Send private message
NewBlackDak
Guru
Guru


Joined: 02 Nov 2003
Posts: 512
Location: Utah County, UT

PostPosted: Wed Mar 16, 2005 12:43 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum