LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-20-2007, 04:28 AM   #1
secretlydead
Member
 
Registered: Sep 2003
Location: Qingdao, China
Distribution: mandriva, slack, red flag
Posts: 249

Rep: Reputation: 31
start grep at line number


How do I start grep at line, say, 200?

(My output is too large and produces a segmentation fault, so i'm going to have to grep each 100 or so lines at a time.)


Particularly, I need to convert strings of chinese code to UTF-8, so I'm making a database. I need to find all of

grep -o "&#[[:digit:]]\{5\};"

those in a file...
 
Old 09-20-2007, 04:57 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
I don't know of an option in grep itself for this, but you can use sed to extract the lines, and then pass them through your grep command (since you already have the command you want), or try to do the whole thing in sed. If you're running the command a large number of times in a tight loop, the all sed version will probably run faster as ou will have half the number of process invocations, but if you're not doing it more than a few dozen times or the efficiency isn't an issue, don't worry about that.

For example, sed will output lines 200 to the end of the file if you do this command:
Code:
$ sed -n '200,$ p' input_file
The -n option says, "don't print each line unless it is explicitly printed in the program".

The sed program, provided in 'single quotes' says: for lines 200 to the end of the file, do command p (print line). The first part, 200,$ is the address specification. You can use lines numbers, special addresses like $ to mean the end of the file, or patterns, which can be used to look in the contents of the line, and start or end when a certain pattern is encountered.

Just pipe the output into your grep statement and you're laughing, i.e.
Code:
$ sed -n '200,$ p' input_file | grep -o "&#[[:digit:]]\{5\};"
 
Old 09-20-2007, 06:03 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,130

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
You can use sed (in a loop) to do it in blocks of 100 if you like.
Try it like this "sed -n '200,300p;300q' file" - that prints out lines 200-300, and quit at line 300 (saves reading the entire file). Stick it in a loop and adjust the bounds.

Personally I'd do it in perl for a bit more control, but each to their own.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script find a line and the next line (grep?) metalx1000 Programming 5 07-24-2007 08:41 PM
showing grep result without the whole line Dr_Death_UAE Linux - General 12 09-07-2006 06:12 AM
Grep pattern first line of a file ericcarlson Linux - Newbie 11 07-20-2004 10:51 AM
Blank line - grep. liguorir Linux - Software 1 04-24-2004 09:03 AM
grep a line in a file from dir bkeating Linux - Newbie 4 07-24-2003 02:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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

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