Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

file-io

cat

People also ask

How do I show the first line of a text file?

To look at the first few lines of a file, type head filename, where filename is the name of the file you want to look at, and then press <Enter>. By default, head shows you the first 10 lines of a file.

How do I read the first line of a file in bash?

Just echo the first list of your source file into your target file. For which head -n 1 source. txt > target.

Which command will print the first line of a file?

The default command which comes to our mind is the head command. head with the option "-1" displays the first line. 2.


You don't need cat.

head -1 file

will work fine.


You don't, use head instead.

head -n 1 file.txt

There are many different ways:

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

You could use cat file.txt | head -1, but it would probably be better to use head directly, as in head -1 file.txt.


This may not be possible with cat. Is there a reason you have to use cat?

If you simply need to do it with a bash command, this should work for you:

head -n 1 file.txt

cat alone may not be possible, but if you don't want to use head this works:

 cat <file> | awk 'NR == 1'