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

CServer Side ProgrammingProgramming

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.


$> 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
Published on 17-Jul-2020 14:19:39