Bash scripting - How to filter lines of a file?



In this tutorial, we play with filtering some lines of a file using Bash.

Content
  • Create a simple file for the tutorial
  • How to filter the first/last n lines?
  • How to remove n lines?
  • How to filter specific lines?

Create a simple file for the tutorial. 


First, let's create a simple file for this tutorial using the following command with seq and tee.

$seq 6 | tee filtering.txt

1
2
3
4
5
6

How to filter the first n lines?


Given the dummy file - filtering.txt -we've just created, we now move on to several ways of filtering n=3 lines as an example.

Using head

$< filtering.txt head -n 3 
Using sed

$< filtering.txt sed -n '1,3p'
Using awk (NR refers to Number of Records)

$< filtering.txt awk 'NR<=3'
Filtering the last 3 lines is straighforward with tail

$< filtering.txt tail -n 3
How to filter the last n lines?

$< lines tail -n 3

How to remove n lines? (e.g., n=3)


Using tail

$< filtering.txt tail -n +4
Using sed

$< filtering.txt sed ‘1,3d’
How to remove the last n lines?

$< lines head -n -3

How to filter specific lines? For example, 4-6 lines


Using sed

$ < filtering.txt sed -n '4,6p'
Using awk

$ < filtering.txt awk '(NR>=4)&&(NR<=6)'
Using head and tail

$ < filtering.txt head -n 6 | tail -n 3  

How to filter odd or even lines?

 
Using sed

$< filtering.txt sed -n '1~2p'	# '0~2p' for even 
Using awk

$< filtering.txt awk 'NR%2'	# '(NR+1)%2' for even

No comments:

Post a Comment