Once you can search by start of line conditions with grep, you can quickly extract the lines you need from configuration files or logs.
However, many beginners struggle with the meaning of regular expressions and how to use the symbols.
This article explains everything in detail, from the basics to more advanced usage
Reference: GNU grep
Basic syntax for specifying start of line with grep
Create the file
cat << 'EOF' > input.txt
apple
banana
apricot
grape
Apple
avocado
EOF
Command to run
grep '^a' input.txt
Result
apple
apricot
avocado
Command to run
grep '^A' input.txt
Result
Apple
How it works
| Item | Description |
|---|---|
| grep | Command that searches for lines matching a condition |
| ^ | Regular expression representing the start of line (beginning of each line) |
| ^a | Matches lines whose start of line is a lowercase a |
| ^A | Matches lines whose start of line is an uppercase A |
| grep '^a' input.txt | Displays only the lines in input.txt that start with a |
Explanation
^ is a regular expression meaning "start of line."
By using grep '^string', you can efficiently extract only the lines that begin with a specified string.
Searching for lines whose start of line matches a specific string with grep
Create the file
cat << 'EOF' > input.txt
apple red
banana yellow
apple green
orange orange
apple pie
grape purple
EOF
Command to run
grep '^apple' input.txt
Result
apple red
apple green
apple pie
How it works
| How it works | Description |
|---|---|
| grep | Command that searches for lines matching a specified pattern |
| ^ | Regular expression representing the start of line (the first character) |
| ^apple | Matches only lines whose start of line is apple |
| input.txt | The file being searched |
Explanation
grep '^apple' input.txt uses ^ to extract only "lines whose start of line is apple."
Lines that merely contain apple somewhere in the middle will not match; only lines that begin with it are displayed.
Searching for lines whose start of line matches multiple strings with grep
Create the file
cat << 'EOF' > input.txt
apple red
banana yellow
cherry red
apple green
banana brown
grape purple
orange orange
EOF
Command to run
grep -E '^(apple|banana)' input.txt
Result
apple red
banana yellow
apple green
banana brown
How it works
| Item | Description |
|---|---|
| grep | Command that searches text for lines matching a condition |
| -E | Enables extended regular expressions, allowing use of | and () |
| ^ | Anchor representing the start of line |
| (apple|banana) | Matches either apple or banana |
| ^(apple|banana) | Searches only for lines whose start of line is apple or banana |
Explanation
By specifying start of line with ^ and combining -E with |, you can search for multiple strings using an OR condition.
If you want to add more start of line conditions, simply add them like (apple|banana|orange).
Searching for lines whose start of line begins with a specific single character with grep
Create the file
cat << 'EOF' > input.txt
apple
banana
apricot
cherry
avocado
blueberry
EOF
Command to run
grep '^a' input.txt
Result
apple
apricot
avocado
How it works
| Item | Description |
|---|---|
| grep | Command that searches for lines matching a condition |
| ^ | Regular expression representing the start of line (beginning of each line) |
| a | The character used to search for lines whose start of line is a |
| '^a' | The search pattern "lines whose start of line is a" |
| input.txt | The file being searched |
Explanation
^ is a regular expression meaning "start of line."
Running grep '^a' input.txt extracts only the lines whose start of line is a.
Searching for lines whose start of line begins with a digit with grep
Create the file
cat << 'EOF' > input.txt
123 apple
abc orange
456 banana
789 grape
xyz lemon
10 melon
EOF
Command to run
grep '^[0-9]' input.txt
Result
123 apple
456 banana
789 grape
10 melon
How it works
| Item | Description |
|---|---|
| grep | Command that searches for lines matching a specified pattern |
| ^ | Metacharacter representing the start of line |
| [0-9] | Character class representing a single digit from 0 to 9 |
| ^[0-9] | Regular expression representing the condition "start of line is a digit" |
Explanation
grep '^[0-9]' extracts only lines whose start of line (^) is a digit ([0-9]).
This makes it easy to search for data lines that begin with a number, such as from logs or CSV files.
Searching for lines whose start of line begins with whitespace with grep
Create the file
cat << 'EOF' > input.txt
apple
banana
orange
grape
melon
peach
EOF
Command to run
grep '^[[:space:]]' input.txt
Result
banana
grape
peach
How it works
| Item | Description |
|---|---|
| grep | Command that searches text for lines matching a condition |
| ^ | Anchor representing the start of line |
| [[:space:]] | POSIX character class representing whitespace characters such as half-width spaces and tabs |
| ^[[:space:]] | Regular expression matching lines whose start of line is a whitespace character |
Explanation
^ specifies the start of line, and [[:space:]] represents whitespace characters (spaces, tabs, etc.).
This lets you extract only lines whose start of line is whitespace.
Searching for comment lines at the start of line with grep
Create the file
cat << 'EOF' > input.txt
# comment1
name=alice
# comment2
age=20
# comment3
city=Tokyo
## comment4
email=alice@example.com
EOF
Command to run
grep '^#' input.txt
Result
# comment1
# comment2
# comment3
## comment4
How it works
| Item | Description |
|---|---|
| grep | Command that searches text for lines matching a condition |
| ^ | Metacharacter representing the start of line |
| # | The character treated as the start of a comment line to search for |
| '^#' | Regular expression matching only lines whose start of line is # |
| grep '^#' input.txt | Displays only the comment lines in input.txt whose start of line is # |
Explanation
Using ^ lets you search only for lines where # appears at the start of line.
This is often used to extract only comment lines from configuration files or shell scripts.
How to search for blank lines at the start of line with grep
Create the file
cat << 'EOF' > input.txt
apple
banana
orange
grape
melon
EOF
Command to run
grep -n '^$' input.txt
Result
1:
4:
7:
How it works
| Item | Description |
|---|---|
| grep | Command that searches for lines matching a specified pattern |
| ^ | Metacharacter representing the start of line |
| $ | Metacharacter representing the end of line |
| ^$ | Matches lines with no characters between the start of line and end of line (blank lines) |
| -n | Option that also displays the line number of matches |
Explanation
A blank line is a state where the end of line immediately follows the start of line, so specifying ^$ lets you search only for blank lines.
Adding -n also lets you confirm the line numbers of the blank lines.
Combining start of line and end of line in a grep search
Create the file
cat << 'EOF' > input.txt
error
error.log
access.log
error.txt
system error
error.log.old
debug.log
error.log
EOF
Command to run
grep '^error' input.txt
Result
error
error.log
error.txt
error.log.old
error.log
Command to run
grep 'log$' input.txt
Result
error.log
access.log
debug.log
error.log
Command to run
grep '^error.*log$' input.txt
Result
error.log
error.log
How it works
| Item | Description |
|---|---|
| ^ | Represents the start of line and matches lines beginning with the specified string |
| $ | Represents the end of line and matches lines ending with the specified string |
| .* | Matches any string of zero or more characters |
| ^error.*log$ | Searches only for lines that start with error and end with log |
Explanation
By combining ^ and $ in grep, you can perform flexible searches based on both the start of line and end of line.
This is often used to extract lines matching a specific naming convention, such as log file names.
How to search for lines that do NOT match at the start of line with grep
Create the file
cat << 'EOF' > input.txt
apple
banana
orange
grape
#comment
123apple
EOF
Command to run
grep -v '^[[:space:]]' input.txt
Result
apple
banana
grape
#comment
123apple
How it works
| Item | Description |
|---|---|
| grep | Command that searches for lines matching a pattern |
| ^ | Anchor representing the start of line |
| [[:space:]] | Whitespace characters such as half-width spaces and tabs |
| ^[[:space:]] | Matches lines whose start of line is a whitespace character |
| -v | Excludes matching lines and displays only the non-matching lines |
Explanation
grep -v '^[[:space:]]' excludes lines whose start of line is whitespace and displays all other lines.
This is a convenient way to exclude only lines that have whitespace at the start of line.
When to use plain start of line search versus -E (extended regular expressions) with grep
Create the file
cat << 'EOF' > input.txt
apple
banana
apricot
grape
application
pineapple
EOF
Command to run
grep '^app' input.txt
Result
apple
application
Command to run
grep -E '^apple|grape$' input.txt
Result
apple
grape
How it works
| Command | How it works | Characteristics |
|---|---|---|
| grep '^app' input.txt | ^ represents the start of line and searches only for lines beginning with "app." | Plain grep is sufficient for a simple start of line match search. |
| grep -E '^apple|grape$' input.txt | -E enables extended regular expressions, and | specifies an OR condition. ^apple means "lines starting with apple," and grape$ means "lines ending with grape." | Lets you write OR searches with multiple conditions concisely. |
Explanation
For a simple start of line match, plain grep using ^ is sufficient.
When you need extended regular expressions, such as for an OR search across multiple conditions, using grep -E lets you write it concisely.
How to run a recursive start of line match search with grep
Create the file
mkdir -p sample/dir1 sample/dir2
Create the file
cat << 'EOF' > sample/dir1/file1.txt
error: failed
warning: disk
error: timeout
info: ok
EOF
Create the file
cat << 'EOF' > sample/dir2/file2.txt
error: network
info: connected
warning: memory
EOF
Command to run
grep -r '^error' sample
Result
sample/dir2/file2.txt:error: network
sample/dir1/file1.txt:error: failed
sample/dir1/file1.txt:error: timeout
How it works
| Item | Description |
|---|---|
| grep | Command that searches text |
| -r | Searches directories recursively |
| ^ | Represents the start of line in a regular expression |
| ^error | Matches only lines whose start of line is error |
| sample | The directory being searched |
Explanation
Using grep -r '^error' lets you recursively search all files under a specified directory for lines whose start of line is error.
^ is the start of line anchor in a regular expression and is used for prefix matching.
How to combine a start of line match search in grep with the find command
Create the file
cat << 'EOF' > input.txt
error: Disk full
warning: Low memory
success: Backup completed
error: Permission denied
info: Service started
EOF
Command to run
find . -name "input.txt" -exec grep '^error' {} \;
Result
error: Disk full
error: Permission denied
How it works
| Item | Description |
|---|---|
| find . | Searches under the current directory |
| -name "input.txt" | Searches for files named input.txt |
| -exec ... {} \; | Runs a command for each file found |
| grep '^error' | ^ represents the start of line and extracts only lines beginning with error |
| {} | Replaced with the file name found by find |
Explanation
Using ^ in grep lets you search only for lines beginning with a specified string.
Combined with find, you can run a start of line match search across multiple files at once.
Causes and fixes when a start of line search in grep isn’t working
Create the file
cat << 'EOF' > input.txt
apple
banana
apple pie
Apple
grape
EOF
Command to run
grep "^apple" input.txt
Result
apple
apple pie
Command to run
grep "^Apple" input.txt
Result
Apple
Command to run
grep "^ banana" input.txt
Result
banana
Command to run
grep "^apple" input.txt | cat -n
Result
1 apple
2 apple pie
How it works
| Item | Description |
|---|---|
| ^ | Anchor representing the start of line; searches only for strings matching from the beginning of the line. |
| ^apple | Extracts only lines whose start of line is apple. |
| Uppercase/lowercase | apple and Apple are treated as different characters, so they do not match each other. |
| Whitespace at start of line | If there is a space at the start of line, the search condition must include that space as well. |
| Reasons a start of line search may not work | Causes include whitespace or tabs at the start of line, mismatched uppercase/lowercase in the search string, and cases where ^ is affected by shell expansion because it wasn't quoted. |
Explanation
The ^ in grep is an anchor meaning "start of line," and it searches only for strings that match from the beginning. If you're not getting the results you expect, checking for whitespace at the start of line or differences in uppercase/lowercase makes it easier to identify the cause.
Summary: understanding and mastering start of line searches with grep
Understanding start of line searches with grep lets you efficiently extract the data you need.
The quickest path to understanding is to first learn the basic syntax and then practice running it yourself.
