如何使用cat读取文件的第一行?


当前回答

清单上又多了一个讨厌的选择:

perl -pe'$.<=1||last' file
# or 
perl -pe'$.<=1||last' < file
# or
cat file | perl -pe'$.<=1||last'

其他回答

你不需要猫。

head -1 file

会很好。

有很多不同的方法:

sed -n 1p file
head -n 1 file
awk 'NR==1' file

清单上又多了一个讨厌的选择:

perl -pe'$.<=1||last' file
# or 
perl -pe'$.<=1||last' < file
# or
cat file | perl -pe'$.<=1||last'

你可以使用cat file.txt | head -1,但是直接使用head可能更好,就像在head -1 file.txt中那样。

这对猫来说可能是不可能的。你非得用cat这个词吗?

如果你只是需要用bash命令来做,这应该为你工作:

head -n 1 file.txt