Database Reference
In-Depth Information
Line 8
Line 9
Line 10
Notice that with tail you have to add one. Removing the last three lines can be done
with head :
$ < lines head -n -3
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
You can print (or extract) specific lines (4, 5, and 6 in this case) using either sed , awk ,
or a combination of head and tail :
$ < lines sed -n '4,6p'
$ < lines awk '(NR>=4)&&(NR<=6)'
$ < lines head -n 6 | tail -n 3
Line 4
Line 5
Line 6
Print odd numbered lines with sed by specifying a start and a step, or with awk by
using the modulo operator:
$ < lines sed -n '1~2p'
$ < lines awk 'NR%2'
Line 1
Line 3
Line 5
Line 7
Line 9
Printing even numbered lines works in a similar manner:
$ < lines sed -n '0~2p'
$ < lines awk '(NR+1)%2'
Line 2
Line 4
Line 6
Line 8
Line 10
Based on pattern
Sometimes you want to extract or remove lines based on their contents. Using grep ,
the canonical command-line tool for filtering lines, we can print every line that
matches a certain pattern or regular expression. For example, to extract all the chap‐
ter headings from Alice's Adventures in Wonderland :
Search WWH ::




Custom Search