編寫 Bash 指令碼以列印 C 中檔案的特定行
在此程式中,我們給定一個檔名 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在 Bash 程式設計中從檔案 text.txt 列印特定行的程式碼。
使用 awk
$> awk ‘{if(NR==5) print $0}’ text.txt使用 sed
$>sed -n 5p text.txt
使用 head
$head -n 5 filename | tail - n + 5
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP