下面哪些命令可以查看file1文件的第300-500行的內容?


下面哪些命令可以查看file1文件的第300-500行的內容?

cat file1 | tail -n +300 | head -n 200
cat file1| head -n 500 | tail -n +300
sed -n '300,500p' file1

答案:BC

解釋:

>head --help
# head -n, --lines=[-]NUM       
# print the first NUM lines instead of the first 10;
# with the leading '-', print all but the last NUM lines of each file
意思就是
head -n k          # 打印前k行
head -n -k         # 打印除最后k行外的所有內容

>tail --help
# tail -n, --lines=[+]NUM       
# output the last NUM lines, instead of the last 10;
# or use -n +NUM to output starting with line NUM
意思就是
tail -n k          # 打印最后k行
tail -n +k         # 從第k行開始打印

回到這道題,輸出300行-500行的內容。

A選項:從第300行開始,接着輸出前200行的內容,但這里的200行包括了第300行,不包括第500行。所以應該改為cat file1 | tail -n +300 | head -n 201

B選項:先取出前500行,再從300行開始。 cat file1 | head -n 500 | tail -n + 300 ,正確。

C選項:sed命令 p :列印,將某個選擇的數據印出。通常 p 會與參數 sed -n 一起運行

sed -n '300-500p' file1 打印300-500行,正確。

親自測試,A輸出300-499;BC輸出300-500


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM