Linux course index · Linux Basics category · Lesson 5 of 8
AI disclosure — AI-generated content, posted as is. AI can make mistakes. This lesson draws on the linked Open Education posts and adds original explanations or lab examples where useful. The newly written explanations and examples may be freely reused. Linked source material retains its own terms.
Learning goal
Choose find for names, grep for lines, cut for fields, and sed for transforming text.
Concepts and setup
The site filters signed integers, tests Canadian postal-code shape, and selects account fields with cut. In the lab we use synthetic data. A postal-code shape check does not prove that a location or postal code exists. LC_ALL=C makes the ASCII-oriented regex and sort behave consistently. Quote patterns so the shell does not expand them.
Filter lines and fields without touching system files
lab=$(mktemp -d)
cd "$lab" || exit
printf '%s\n' '-7' '+12' '3.5' 'abc' > data.txt
printf '%s\n' 'M4C 5L7' 'm4c 5l7' 'wrong' > postal.txt
printf '%s\n' 'learner:x:1000' 'helper:x:1001' > accounts.txt
LC_ALL=C grep -E '^[+-]?[0-9]+$' data.txt
LC_ALL=C grep -E '^[A-Z][0-9][A-Z] ?[0-9][A-Z][0-9]$' postal.txt
cut -d ':' -f 1,3 accounts.txt | sed 's/^helper:/trainee:/'
Possible output
-7
+12
M4C 5L7
learner:1000
trainee:1001
The source uses egrep. grep -E is the straightforward modern spelling. sed writes the replacement to stdout and leaves accounts.txt unchanged.
Filter by file name
mkdir reports
cp data.txt reports/raw.txt
find reports -type f -name '*.txt' -print
grep -n '3[.]5' reports/raw.txt
Possible output
reports/raw.txt
3:3.5
Quoted *.txt reaches find as a pattern. In contrast, grep -n displays the line number of a content match.
Questions only
Quick-check questions
- What do ^ and $ mean in this regex?
- Why did the plain sed command leave the file unchanged?
Assessment questions
- Change the signed-integer pattern to accept only unsigned integers.
- Use cut to print the username and ID from colon-delimited accounts.txt.
- Distinguish the three exit statuses of GNU grep in ordinary use.
Questions and answers
Quick-check answers
- What do ^ and $ mean in this regex?
Answer — They anchor the start and end of a line. - Why did the plain sed command leave the file unchanged?
Answer — It prints transformed text to standard output without in-place editing.
Assessment answer key
- Change the signed-integer pattern to accept only unsigned integers.
Answer — Use LC_ALL=C grep -E ‘^[0-9]+$’ data.txt. None of the four starter lines match. - Use cut to print the username and ID from colon-delimited accounts.txt.
Answer — cut -d ‘:’ -f 1,3 accounts.txt. - Distinguish the three exit statuses of GNU grep in ordinary use.
Answer — 0 means match, 1 means no match, and 2 indicates an error in ordinary use.
Source articles on this site
- Linux: Regular Expression: grep
- Linux sed, cat, cut examples
- Linux: sed: search, replace, delete, print
- ইউনিক্সঃ পাইপ এবং ফিল্টার (Unix – Pipes and Filters)
Continue the course
Previous lesson 04 — Pipes, Redirection, and Exit Status. Next lesson 06 — File Permissions and umask.
Linux course index · Linux Basics category · Lesson 5 of 8
