How do I read first line using cat

2019-03-07 20:40发布

How do i read the first line of a file using cat?

标签: bash cat
9条回答
我只想做你的唯一
2楼-- · 2019-03-07 21:21

You don't, use head instead.

head -n 1 file.txt
查看更多
Anthone
3楼-- · 2019-03-07 21:25

You dont need any external command if you have bash v4+

< file.txt mapfile -n1 && echo ${MAPFILE[0]}

or if you really want cat

cat file.txt | mapfile -n1 && echo ${MAPFILE[0]}

:)

查看更多
ゆ 、 Hurt°
4楼-- · 2019-03-07 21:26

There are many different ways:

sed -n 1p file
head -n 1 file
awk 'NR==1' file
查看更多
登录 后发表回答