Home > Backend Development > C++ > 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
Release: 2023-08-26 22:13:15
forward
1352 people have browsed it

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
Copy after login

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

Using awk

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

使用sed命令

$>sed -n 5p text.txt
Copy after login

使用头部

$head -n 5 filename | tail - n + 5
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template