How do i read the first line of a file using cat
?
相关问题
- JQ: Select when attribute value exists in a bash a
- bash print whole line after splitting line with if
- “command not found” errors in expect script execut
- grep using grep results
- UNIX Bash - Removing double quotes from specific s
相关文章
- Check if directory exists on remote machine with s
- Reverse four length of letters with sed in unix
- Launch interactive SSH bash session from PHP
- BASH: Basic if then and variable assignment
- Bash script that creates a directory structure
- Test if File/Dir exists over SSH/Sudo in Python/Ba
- How can I create a “tmp” directory with Elastic Be
- Spread 'sed' command over multiple lines
You don't need
cat
.head -1 file
will work fine.You could use
cat file.txt | head -1
, but it would probably be better to use head directly, as inhead -1 file.txt
.cat
alone may not be possible, but if you don't want to usehead
this works:I'm surprised that this question has been around as long as it has, and nobody has provided the pre-mapfile built-in approach yet.
...puts the first line of the file in the variable expanded by
"$first_line"
, easy as that.Moreover, because
read
is built into bash and this usage requires no subshell, it's significantly more efficient than approaches involving subprocesses such ashead
orawk
.This may not be possible with
cat
. Is there a reason you have to usecat
?If you simply need to do it with a bash command, this should work for you:
use the below command to get the first row from a CSVfile