LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-30-2004, 02:26 PM   #1
keyops
LQ Newbie
 
Registered: Apr 2004
Posts: 4

Rep: Reputation: 0
extracting x bytes


Can anyone tell me if there is a way of greppin through a large file and - if grep finds the search term - it then extracts only x chars back and y chars forward?

The file I am searching is one large mass with no \n's, alike to a disk partition

ta

k
 
Old 05-30-2004, 03:01 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
I don't know of a way to do it with grep - I think it can only do line offsets. You could do something with perl:
perl -ne 's/(.....)(WORD)(...)/(print "$1$2$3\n")/meg;' SOMEFILE

Replace "SOMEFILE" with the name of your file and "WORD" with the word to search for. You can adjust the number of "."s to say how many characters before or after you want displayed. The example above will show 5 before and 3 after.
 
Old 05-30-2004, 03:07 PM   #3
keyops
LQ Newbie
 
Registered: Apr 2004
Posts: 4

Original Poster
Rep: Reputation: 0
thanks

cheers for that - will give it a go

does anyone know how to do this at command line, rather than employing perl?

thanx again

Jc
 
Old 05-30-2004, 03:08 PM   #4
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
That command will work on the command line.
 
Old 05-30-2004, 04:30 PM   #5
keyops
LQ Newbie
 
Registered: Apr 2004
Posts: 4

Original Poster
Rep: Reputation: 0
Agreed, but it is still employing perl

What I am wondering is whether or not this can be done with a combination of seeking through the file, using cut /sed/awk etc

if not, not a problem as your solution does work, but just interested in alternative shell examples

many thanks again

k
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
synaptic bytes mercutio Linux - Software 9 02-10-2006 04:51 PM
TX bytes vs. httpd bytes ovrload Linux - Networking 3 10-12-2005 04:19 PM
How to retrieve bytes sent AzidStar Linux - Networking 5 03-01-2005 03:25 AM
How many bytes per inode? theonebeyond Linux - Newbie 2 09-24-2004 02:36 AM
1 block = ? bytes captainstorm Linux - Newbie 2 07-08-2003 10:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration