String at the beginning of a line can be grep-ed by prefixing a caret(^) symbol to the pattern.
grep ^pattern My-Filename
# (^), caret symbol
Here is an example
# Example output:
ubuntu@codetryout:~$ grep ^ubuntu /etc/passwd
ubuntu:x:1001:1001::/home/ubuntu:/bin/bash
Grep pattern at the beginning of the line, case insensitive
ubuntu@codetryout:~$ grep -i ^UBUNTU /etc/passwd
ubuntu:x:1001:1001::/home/ubuntu:/bin/bash
Grep output to display the line number in result
ubuntu@codetryout:~$ grep -in ^UBUNTU /etc/passwd
47:ubuntu:x:1001:1001::/home/ubuntu:/bin/bash
Here is another example with multiple output lines, grep-ing for lines starting with the character “a“
ubuntu@codetryout:~$ grep -in ^a /etc/passwd
28:avahi-autoipd:x:109:116:Avahi autoip daemon,,,:/var/lib/avahi-autoipd:/usr/sbin/nologin
34:avahi:x:115:121:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/usr/sbin/nologin
We have just explored several ways to grep pattern at the beginning of a text file