Last active
December 1, 2016 18:41
-
-
Save nu11secur1ty/8ed049e18478331a5581c8365e294f23 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Using perl: | |
$ perl -ne 'print if $.>=3 and $.<=5;' /etc/passwd | |
or | |
$ 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 | |
or | |
$ 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 | |
or | |
$ 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.) | |
or | |
$ 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