Write a bash script to print a particular line from a file in C


In this program, we are given a file name text.txt. Our task is to print a particular line from the file.

For this there are multiple methods in bash script, they are awk, sed, head.

Syntax

$> awk ‘{if(NR==LINE_NUMBER) print $0}’ filename
$> sed -n LINE_NUMBERp filename
$head -n LineNumber filename | tail - n + LINE_NUMBER

Code to print a specific line in bash programming from file text.txt.

Using awk

$> awk ‘{if(NR==5) print $0}’ text.txt

Using sed

$>sed -n 5p text.txt

Using head

$head -n 5 filename | tail - n + 5

Updated on: 17-Jul-2020

215 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements