How would you print just the 10th line of a file?
For example, assume that
file.txt
has the following content:Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 Line 7 Line 8 Line 9 Line 10Your script should output the tenth line, which is:
Line 10
三种方法。
1.AWK,我也只会这一种,后边俩是谷歌来的
awk "NR == 10" file.txt
这里的NR就是number of rows的意思,即第几行
2.SED
sed -n '10p' file.txt
3.Tail and Head
NR = ' cat file.txt | wc -1 '
if [ $NR -ge 10 ] ; then
tail -n +10 file.txt | head -1
fi
加入if...fi条件语句,先判断文件是否超出10行
tail从第十行开始执行,输出重定向为head,head提取此时的第一行,正好是文件的第十行。
LOL
没有评论:
发表评论