Filter text with grep and cut
grep selects matching lines. -F treats the pattern as literal text; -E enables extended regular expressions. Use -e before a pattern that may start with a hyphen, and -- before file operands where supported. GNU grep normally returns 0 for a match, 1 for no match, and 2 for an error; other implementations may use higher error codes. With -q a match can mask a later error, so the examples use ordinary grep. cut -d delimiter -f fields extracts delimited fields; it does not understand CSV quoting. These are separate external programs, not Bash syntax. Use simple text records with a known delimiter. For GNU cut, a line without the delimiter is printed unchanged unless -s is used. Status 1 from ordinary grep means no selected lines, not an input error; a later successful pipeline stage can hide either status without pipefail.
Practical examples
Run each example as a separate script in a fresh noninteractive Bash 5.2 process and empty practice directory. Tests use C locale; do not enter expected output as code.
Literal search
Expected stdout:
a.bThe dot is literal in fixed-string mode.
Filter and extract
printf '%s\n' 'INFO:ready' 'WARN:retry' 'ERROR:failed' | grep -E '^(WARN|ERROR):' | cut -d ':' -f 2Expected stdout:
retryfailedgrep selects records; cut extracts field two.
Distinguish no match from failure
printf 'INFO:ready\n' > log.txt || exit 1grep -F -e WARN log.txtstatus=$?printf 'status=%s\n' "$status"Expected stdout:
status=1No line matched, but grep successfully read the file.
Common mistakes
No match is not a missing file. Do not use a regular expression when a literal user search is intended, or cut as a full CSV parser.
Practice
Create a colon-separated log with three levels. Extract the messages of WARN records, then test a pattern absent from the log and explain the status. Keep fields free of embedded colons.
Next: Sort, count and aggregate text