在这个程序中,我们被给定一个文件名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