To display specific lines from a file in Linux, you can use a combination of the sed, awk, or head and tail commands. Here are a few methods:
Using sed
To display lines 3 to 4 of a file named example.txt:
sed -n '3,4p' example.txt
Using awk
To display lines 3 to 4:
awk 'NR>=3 && NR<=4' example.txt
Using head and tail
To display lines 3 to 4:
head -n 4 example.txt | tail -n 2
Explanation:
sed -n '3,4p': The-noption suppresses automatic printing, and3,4ptellssedto print lines 3 to 4.awk 'NR>=3 && NR<=4':NRis the current line number, and this command prints lines where the line number is between 3 and 4.head -n 4 | tail -n 2: This first gets the first 4 lines and then pipes that output totail, which retrieves the last 2 lines from that output.
Choose the method that best fits your needs!
