在 Linux 如果需要印出檔案第一行及最後一行的資料, 可以用幾個方法實現。
檔案 testing.txt 內容:
|
1 2 3 4 5 6 7 |
header line 1 line 2 line 3 line 4 line 5 footer |
以下幾個指令也可以印出檔案的第一行及最後一行, 即得出 header 及 footer:
sed:
|
1 2 3 |
sed -e 1b -e '$!d' testing.txt header footer |
awk:
|
1 2 3 |
awk 'NR==1; END{print}' testing.txt header footer |
head 及 tail:
|
1 2 3 |
(head -n1 && tail -n1) < testing.txt header footer |