Home  >  Article  >  Backend Development  >  Write a bash script in C language that prints specific lines in a file

Write a bash script in C language that prints specific lines in a file

WBOY
WBOYforward
2023-08-26 22:13:151271browse

Write a bash script in C language that prints specific lines in a file

在这个程序中,我们被给定一个文件名text.txt。我们的任务是从文件中打印出特定的一行。

为了实现这个目标,bash脚本中有多种方法,它们是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

使用sed命令

$>sed -n 5p text.txt

使用头部

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

The above is the detailed content of Write a bash script in C language that prints specific lines in a file. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete