How to "grep" out specific line ranges of a file

how to s
how to b
how to make
how to draw
how to get
how to do
how to cook
how to t

There are often times I'll grep -n whatev file to find what I'm looking for. Say the output is

1234: whatev 1
5555: whatev 2
6643: whatev 3

If I want to then just extract the lines between 1234 and 5555, is there a tool to do that? For static files I have a script that does wc -l of the file and then does the math to split it out with tail & head but that doesn't work out so well with log files that are constantly being written to.

Try using sed as mentioned on For example use

sed '2,4!d' somefile.txt

to print from the second line to the fourth line of somefile.txt. (And don't forget to check, sed is a wonderful tool.)

How-to, Get more from your technology and gadgets with TechRadar's expert tips, tricks, hacks and advice. Learn how to do just about everything at eHow. Find expert advice along with How To videos and articles, including instructions on how to make, cook, grow, or do almost anything.

The following command will do what you asked for "extract the lines between 1234 and 5555" in someFile.

sed -n '1234,5555p' someFile

Technology how to guides, tips and tricks, How To - Tips, Tricks and Hacks for Doing Everything Better | Lifehacker. CNET editors and users share the top tech 'how to' tips and tricks with advice for getting the most out of all your gadgets.

If I understand correctly, you want to find a pattern between two line numbers. The awk one-liner could be

awk '/whatev/ && NR >= 1234 && NR <= 5555' file

You don't need to run grep followed by sed.

Perl one-liner:

perl -ne 'if (/whatev/ && $. >= 1234 && $. <= 5555') {print}' file

How To, We all need help now and then with real-life challenges. The How To channel offers easy-to-navigate playlists of short videos that will give you immediate li How To is your one-stop channel for navigating the rough patches in life. From relationships to finances, here is where you’ll find simple, practical help from those who have been there.

Line numbers are OK if you can guarantee the position of what you want. Over the years, my favorite flavor of this has been something like this:

sed "/First Line of Text/,/Last Line of Text/d" filename

which deletes all lines from the first matched line to the last match, including those lines.

Use sed -n with "p" instead of "d" to print those lines instead. Way more useful for me, as I usually don't know where those lines are.

wikiHow, Hey guys! Welcome back to another video. Make sure you subscribe and turn your notifications on right away so you don't miss a upload from me. Hope you  Samsung Redesigned Its TV Boxes to be Easily Converted Into Cat Houses and Entertainment Centers

If you want lines instead of line ranges, you can do it with perl: eg. if you want to get line 1, 3 and 5 from a file, say /etc/passwd:

perl -e 'while(<>){if(++$l~~[1,3,5]){print}}' < /etc/passwd

The How To Channel—Simple, Practical Help., CNET editors and users share the top tech 'how to' tips and tricks with advice for getting the most out of all your gadgets. Find DIY projects and craft ideas perfect for inside or outside your home that can be done in a weekend or on a budget with step-by-step instructions at

Youtube Channel, The Best Tech Newsletter Anywhere. Join 250,000 subscribers and get a daily digest of news, geek trivia, and our feature articles. Skip to content  Learn how to do (almost) anything at HowToDoThings! Explore instructive how-to articles and videos – make things, solve problems, be happy!

How To, giving practical instruction and advice (as on a craft) how-to books on all sorts of hobbies— Harry Milt. how-to. noun. Definition of how-to (Entry 2 of 2). I make How To videos! Have a video Suggestion? Post it in the Comments section, contact me through my Facebook page or Tweet me! Business enquiries - HowToBa

How-To Geek, Create a new survey and edit it with others at the same time. Choose from a variety of pre-made themes or create your own. Free with a Google account.

  • See…
  • One useful follow up bit of information is how to prepend the line numbers onto the sed result... pipe it into nl like so: sed ''"$start"','"$end"'!d' somefile.txt | nl -ba -v$start
  • I had to add / delimiters to make it work: sed -n '/1234/,/5555/p' someFile
  • Quite handy :) +1
  • small thing, but you don't need the quotes
  • FYI, That $l is "dollar el" not "dollar one". A more perlish (i.e. shorter) command is perl -ne 'if($.~~[1,3,5]){print}' /etc/passwd.