Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

What's a good way of extracting say, lines 20 -45 out of a huge text file. Non-interactively of course!

share|improve this question
    
Why is this tagged "gnu"? This is just a question about UNIX shell scripting. –  dkagedal Sep 15 '10 at 23:22
    
I feel that it should be removed aswell, but like xenoterracide once said, I don't feel strongly enough about it to do it myself –  Stefan Sep 16 '10 at 8:53
    
Can't remember if tagged it as 'gnu' myself or not :-) Anyway this question could be answered with or without GNU, so the tag adds nothing, so I changed it. –  Chris Huang-Leaver Oct 31 '10 at 7:43

5 Answers 5

up vote 8 down vote accepted

you could try:

cat textfile | head -n 45 | tail -n 26

or

cat textfile | awk "20 <= NR && NR <= 45" 

update:

As Mahomedalid pointed out, cat is not necessary and a bit redundant, but it does make for a clean, readable command.

If cat does bother you, a better sollution would be:

<textfile awk "20 <= NR && NR <= 45"
share|improve this answer
    
You get +1 for being first, but Mahomedalid gets +1 for pointing out the useless use of cat. :) –  Annika Backstrom Sep 15 '10 at 16:05
1  
awk NR==20,NR==45 textfile works too, and reads easily. –  ephemient Sep 16 '10 at 1:47
    
I like the the use of stdin more, it has some global consistancy with the rest of nix –  Stefan Sep 16 '10 at 8:52
    
Reading from command line arguments has consistency with other UNIX utilities too, and my main point was to demonstrate awk's , range operator. –  ephemient Sep 17 '10 at 18:10
    
lol, i meant @adam. but yes, I like your suggestion –  Stefan Nov 3 '10 at 4:03

Even simpler:

sed -n 20,45p < textfile

The -n flag disables the default output. The "20,45" addresses lines 20 to 45, inclusive. And the "p" command prints the current line.

share|improve this answer
1  
+1 nice, i Like, but its line 20 to 45 :) –  Stefan Sep 15 '10 at 18:53
1  
ok ok, I edited it to say 20,45 :-) –  dkagedal Sep 15 '10 at 23:20

Actually, you don't need the cat, with:

head textfile -n 45 | tail -n 25

or

awk "NR > 19 && NR < 46" texfile

it's enough

share|improve this answer
1  
yes, I know ppl always talk about UUOC (en.wikipedia.org/wiki/Cat_(Unix)%23Useless_use_of_cat) and how it is more costly, but still cat is way cleaner and more readable IMO. –  Lazer Sep 15 '10 at 18:46
    
i know im beeing a stickler for detail here, but the first head command won't give lines 20-45, but 45-65.... –  Stefan Sep 15 '10 at 19:27
    
actually... it gives the lines 41 to 65 –  Stefan Sep 15 '10 at 22:06
ruby -ne 'print if 20 .. 45' file
share|improve this answer
1  
a fellow rubyist, you get my vote sir –  Stefan Sep 16 '10 at 16:36
1  
While we're at it, why not python -c 'import fileinput, sys; [sys.stdout.write(line) for nr, line in enumerate(fileinput.input()) if 19 <= nr <= 44]' too? :-P This is something that Ruby, modeled after Perl, inspired by awk/sed, can do easily. –  ephemient Sep 17 '10 at 18:21

Since sed and awk were already taken, here is a perl solution:

perl -nle "print if ($. > 19 && $. < 46)" < textfile

Or, as pointed out in the comments:

perl -ne 'print if 20..45' textfile
share|improve this answer
2  
What's with all those extra characters? No need to strip and re-add newlines, flip-flop assumes comparison to line number, and diamond operator runs through arguments if provided. perl -ne'print if 20..45' textfile –  ephemient Sep 15 '10 at 21:09
1  
Nice. -nle is a bit of a reflex I suppose, as for the rest, I have no excuse save ignorance. –  Steven D Sep 15 '10 at 21:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.