I have a file called
file.txt. How can I print the first line only using the
I have a file called
Although it's an unconventional application of grep, you can do it using
grep -m1 "" file.txt
It works because the empty expression matches anything, while
-m1 causes grep to exit after the first match
-m NUM, --max-count=NUM Stop reading a file after NUM matching lines.
This is not something
grep does. The name "grep" itself is an acronym for "globally search a regular expression and print", which is what the
g/re/p does (for a given regular expression
ed is an interactive line editor from 1969, but it's most likely installed on your system today nonetheless. We got
ed, and it can be seen as a shortcut or alias for a specific functionality of
sed, which is "stream-
ed", i.e. a (non-interactive) stream editor.
$ sed -n '1p' file.txt
1p string is a tiny
sed "script" that prints (
p) the line corresponding to the given address (
1, the first line).
The editing command
1p would (no surprise) do the same thing in the
ed editor by the way.
-n suppresses the output of anything not explicitly printed by the script, so all we get is the first line of the file
$ sed '1q' file.txt
This prints all lines of the file, but quits (
q) at line 1 (after printing it). This is exactly equivalent to
head -n 1 file.txt.
In the POSIX standard it says (generalizing) that
head -n N is much the same as
sed 'Nq', i.e. "print every line, but quit at line
N". The reason
head was included in the standard at all was due to symmetry with
tail (and backwards compatibility with existing Unix implementations).
Unless the first line has a unique string you cannot do this using only grep.
head -n 1 file.txt would work in its place.
If you want to only print out the first line if it matches a pattern then pipe head into grep
head -n 1 * | grep [pattern]
Yet Another Unconventional Use of Grep -- a Schwartzian Transform that goes through several gyrations to number the lines, then uses grep to look for the line number, then strip the line number back off:
function grep1() ( nlines=$(wc -l < "$1") nlw=$(printf "%d" "$nlines" | wc -c) nl -d '\n' -ba -n ln -w "$nlw" -s ' ' "$1" | grep '^1 ' | sed 's/^1 *//' ) function greplast() ( nlines=$(wc -l < "$1") nlines=$((nlines + 0)) nlw=$(printf "%d" "$nlines" | wc -c) nl -d '\n' -ba -n ln -w "$nlw" -s ' ' "$1" | grep "^$nlines " | sed "s/^$nlines *//" )
I'm putting this Answer here as an example of the idea that just because you can do something in (grep or bash or ... etc), doesn't mean that you should -- there's probably a better tool for the job. sed (
sed 1q or
sed -n 1p) and head (
head -n 1) are the more appropriate tools for printing the first line from a file. For printing the last line of a file, you have
tail -n 1 or
sed -n '$p'. Not only are these tools a single command (instead of 3+ in the above functions), they are also much clearer for future readers -- perhaps yourself! -- of the scripts they're in. While I am not one of the (currently 3) down-voters of your question, it's likely that your insistence on an arbitrary tool for the job (without any supporting reasons) is the reason for the downvotes. It's extremely unlikely that a system that has
grep does not also have