Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Using perl:
$ perl -ne 'print if $.>=3 and $.<=5;' /etc/passwd
$ perl -ne 'print if $.>=3; last if $.>5' /etc/passwd
(The second variant is, again, more efficient.)
Using sed:
$ sed -n '3,5p' /etc/passwd
$ sed -n '3,5p;6q' /etc/passwd
(The second version would quit upon encountering line 6 so it would be efficient for huge files.)
Using awk:
$ awk 'NR==3,NR==5' /etc/passwd
$ awk 'NR>=3{print}NR==5{exit}' /etc/passwd
(The second variant quit after printing line 5 so it's more efficient.)
Using python:
build script:
with open('yourfile.txt') as f:
for line in itertools.islice(f, num+1, num+2):
print line
Using head:
$ head -5 /etc/passwd | tail -3
(Tail returns the last lines of a file together with a pipe you can use both features.)
$ head -n 5 /etc/passwd
(The second variant print first five lines.)
Using bash:
$ find / -type f -name awk.txt & awk 'NR==3' awk.txt | less
(This is the command to find the file with name "awk.txt" and print the third line from this file.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.