0
(0)

There are multiple ways to get the Nth line of a file but below are the most used commands.

  1. Sed

Let's consider the below input file

$ cat file1

mango
apple
grapes
banana
strawberry
papaya
orange
cherry

#to print 4th line from the file

sed -n 4p file1

banana

#to print data within specific range(from 4th line to 6th)

sed -n 4,6p file1

banana
strawberry
papaya

#to print specific lines( 4th and 6th only)

sed -n -e 4p -e 6p file1

banana
papaya

#to delete specific line (4th line)

sed '4d' file1

mango
apple
grapes
strawberry
papaya
orange
cherry

# to delete data within specific range(4th line to 6th line)

sed '4,6d' file1

mango
apple
grapes
cherry

#to delete specific lines( 4th and 6th only)

sed -e '4d' -e '6d' file1

mango
apple
grapes
strawberry
orange
cherry

2. awk

#to print 5th line from the file1

awk 'NR==5' file1

strawberry

#to print specific lines( 3rd and 5th only)

awk 'NR==3' || 'NR==5' file1

grapes
strawberry

#to print data within specific range(from 4th line to 6th)

awk 'NR>=4&&NR<=6' file1

banana
strawberry
papaya

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.