Unit 6 subjective questions Filters and Regular Expressions
CSE105 — Creative Engineering Workshop 20 questions with model answers. Write yours first, then compare.
-
Question 1 of 20
Define a filter in the context of Unix/Linux commands. Explain how filters process data and give three examples of commonly used filters.
Model answer
A filter is a program that takes its input from the standard input (stdin), performs some transformation or operation on the data, and sends the result to the standard output (stdout).
How filters process data:
- They read data line by line (or stream) from stdin.
- They process/transform the data according to their function.
- They write the processed output to stdout, which can be redirected or piped.
Key characteristics:
- Filters can be connected using pipes (
|) to build powerful command chains. - They follow the Unix philosophy: do one thing and do it well.
Three common examples:
grep— filters lines matching a pattern.sort— arranges lines in order.wc— counts lines, words, and characters.
Example:
bash
cat file.txt | grep "error" | sortHere
grepandsortact as filters in a pipeline. -
Question 2 of 20
Explain the
grepcommand with its syntax. Describe at least five important options ofgrepwith examples.Model answer
grep(Global Regular Expression Print) searches for a pattern in one or more files and prints the matching lines.Syntax:
bash
grep [options] pattern [file...]Important options:
-
-i: Ignore case while matching.
bash
grep -i "hello" file.txt -
-c: Count the number of matching lines.
bash
grep -c "error" log.txt -
-n: Display matching lines with their line numbers.
bash
grep -n "main" program.c -
-v: Invert match; display lines that do not match.
bash
grep -v "debug" log.txt -
-l: List only the names of files containing the match.
bash
grep -l "TODO" *.txt -
-w: Match whole words only.
bash
grep -w "is" file.txt
Note:
grepreturns matching lines to stdout, making it ideal for use in pipelines. -
-
Question 3 of 20
Distinguish between
grep,egrep, andfgrep. When would you use Extendedgrep?Model answer
These three variants differ in the type of pattern matching they support:
Feature grepegrep(grep -E)fgrep(grep -F)Pattern type Basic Regular Expression (BRE) Extended Regular Expression (ERE) Fixed strings (no regex) Metacharacters like +,?,|,()Need escaping ( \+,\?)Used directly Treated as literals Speed Normal Normal Fastest (no regex parsing) Extended grep (
egreporgrep -E):- Supports metacharacters
+,?,|,{},()without escaping. - Use it when you need complex patterns such as alternation or grouping.
Example:
bashExtended grep — match lines containing 'cat' or 'dog'
egrep "cat|dog" animals.txt
Equivalent basic grep needs escaping
grep "cat|dog" animals.txt
When to use Extended grep: Whenever the search requires alternation (
|), one-or-more (+), optional (?), or grouping()— Extended grep makes patterns cleaner and more readable. - Supports metacharacters
-
Question 4 of 20
Explain the
cutcommand in detail. How can it be used to extract columns and character ranges from a file?Model answer
The
cutcommand extracts selected portions (columns or characters) from each line of input.Syntax:
bash
cut [options] [file]Important options:
-
-c: Cut by character position.
bash
cut -c1-5 file.txt # first 5 characters of each line -
-f: Cut by field number. -
-d: Specify the field delimiter (default is TAB).
bash
cut -d":" -f1 /etc/passwd # extract usernames
Extracting character ranges:
cut -c1-5→ characters 1 to 5cut -c3-→ from character 3 to endcut -c-4→ from start to character 4
Extracting columns (fields):
bash
cut -d"," -f2,4 data.csv # extract 2nd and 4th fieldsExample: To get the shell of each user:
bash
cut -d":" -f1,7 /etc/passwdNote:
cutworks well when data is neatly delimited but cannot handle multiple/variable spaces as a single delimiter (useawkfor that). -
-
Question 5 of 20
Describe the
headandtailcommands. How cantailbe used to monitor a growing log file in real time?Model answer
headdisplays the beginning portion of a file, whiletaildisplays the ending portion.headcommand:
bash
head [options] [file]-
By default shows the first 10 lines.
-
-n: specify number of lines.
bash
head -n 5 file.txt # first 5 lines -
-c: specify number of bytes.
tailcommand:
bash
tail [options] [file]-
By default shows the last 10 lines.
-
-n: specify number of lines.
bash
tail -n 15 file.txt # last 15 lines -
+n: start from line n to end.
bash
tail -n +5 file.txt # from line 5 onward
Monitoring a growing log file:
The-f(follow) option keeps the file open and displays new lines as they are appended:
bash
tail -f /var/log/syslogThis is extremely useful for real-time log monitoring. Combine with
grep:
bash
tail -f access.log | grep "404" -
-
-
Question 6 of 20
Explain the
sortcommand with its important options. Illustrate numeric, reverse, and field-based sorting with examples.Model answer
The
sortcommand arranges the lines of text in a specified order (default is ASCII/alphabetical ascending).Syntax:
bash
sort [options] [file]Important options:
-
-r: Reverse (descending) order.
bash
sort -r names.txt -
-n: Numeric sort (treats values as numbers).
bash
sort -n marks.txt -
-k: Sort by a specific key/field.
bash
sort -k2 data.txt # sort by 2nd field -
-t: Specify field separator.
bash
sort -t":" -k3 -n /etc/passwd # sort by UID numerically -
-u: Remove duplicate lines after sorting. -
-f: Case-insensitive sort.
Examples:
bashNumeric sort
sort -n numbers.txt
Reverse numeric sort
sort -nr numbers.txt
Sort CSV by 3rd column numerically
sort -t"," -k3 -n students.csv
Note: Without
-n,sortcompares numbers as strings, so10would come before2. -
-
Question 7 of 20
Explain the
wccommand. How does it count lines, words, and characters? Provide examples of its usage in pipelines.Model answer
The
wc(word count) command counts the number of lines, words, and characters/bytes in its input.Syntax:
bash
wc [options] [file]Default output (three numbers):
lines words characters filename
bash
wc file.txt12 45 310 file.txt
Important options:
-
-l: Count only lines.
bash
wc -l file.txt -
-w: Count only words.
bash
wc -w file.txt -
-c: Count bytes. -
-m: Count characters. -
-L: Length of the longest line.
Usage in pipelines:
bashCount number of users on the system
cat /etc/passwd | wc -l
Count files in a directory
ls | wc -l
Count matching lines
grep "error" log.txt | wc -l
Note: When multiple files are given,
wcprints counts per file and a total line at the end. -
-
Question 8 of 20
Explain the
trcommand. Describe how it is used for translating, deleting, and squeezing characters with suitable examples.Model answer
The
tr(translate) command translates, deletes, or squeezes characters read from standard input. It works only on stdin (does not accept a filename directly).Syntax:
bash
tr [options] SET1 [SET2]1. Translating characters:
bashConvert lowercase to uppercase
tr 'a-z' 'A-Z' < file.txt
echo "hello" | tr 'a-z' 'A-Z' # HELLO
2. Deleting characters (
-d):
bashRemove all digits
echo "abc123" | tr -d '0-9' # abc
3. Squeezing repeated characters (
-s):
bashCompress multiple spaces into one
echo "hello world" | tr -s ' ' # hello world
4. Complement set (
-c):
bashKeep only alphabets, replace rest with newline
tr -cs 'a-zA-Z' '\n' < file.txt
Common uses:
- Case conversion.
- Removing unwanted characters (e.g., carriage returns
tr -d '\r'). - Squeezing whitespace.
Note:
troperates on characters, not words or patterns. -
Question 9 of 20
Explain the
uniqcommand. Why issortusually used beforeuniq? Describe its important options with examples.Model answer
The
uniqcommand filters out adjacent duplicate lines from sorted input.Syntax:
bash
uniq [options] [file]Why
sortbeforeuniq:uniqonly removes consecutive duplicate lines.- If duplicates are scattered throughout the file, they will not be detected.
- Therefore, we
sortthe data first so identical lines become adjacent.
bash
sort file.txt | uniq
Important options:
-
-c: Prefix each line with its count of occurrences.
bash
sort file.txt | uniq -c -
-d: Display only duplicate lines.
bash
sort file.txt | uniq -d -
-u: Display only unique (non-repeated) lines.
bash
sort file.txt | uniq -u -
-i: Case-insensitive comparison.
Example — frequency count of words:
bash
cat words.txt | sort | uniq -c | sort -nrThis lists each word with its count, sorted by most frequent.
-
Question 10 of 20
What is a pipe in Unix/Linux? Explain how filters can be combined using pipes to build powerful command pipelines with examples.
Model answer
A pipe (
|) is a mechanism that connects the standard output of one command to the standard input of another command, allowing data to flow through a chain of commands.Concept:
bash
command1 | command2 | command3- Output of
command1becomes input ofcommand2, and so on. - No temporary files are needed; data flows in memory.
Why pipes are powerful:
- Each filter does one job well; combining them solves complex problems.
- Follows the Unix philosophy of building small, composable tools.
Examples:
1. Count logged-in users:
bash
who | wc -l2. Find the top 5 largest files:
bash
ls -l | sort -k5 -nr | head -53. Find most frequent words:
bash
cat file.txt | tr ' ' '\n' | sort | uniq -c | sort -nr | head4. Unique error messages:
bash
grep "error" log.txt | sort | uniqNote: Pipes greatly reduce the need for intermediate files and enable one-line solutions to complex text-processing tasks.
- Output of
-
Question 11 of 20
Define a Regular Expression (regex). Explain the fundamental building blocks of regular expressions with examples.
Model answer
A Regular Expression (regex) is a sequence of characters that defines a search pattern used for matching, searching, and manipulating text.
Fundamental building blocks:
-
Literals: Ordinary characters that match themselves. e.g.,
catmatches the stringcat. -
Metacharacters: Special characters with meaning:
.— matches any single character.c.tmatchescat,cot,cut.*— matches zero or more of the preceding character.ab*matchesa,ab,abb.^— anchors match to the start of a line.^Thematches lines starting withThe.$— anchors match to the end of a line.end$matches lines ending withend.
-
Character classes
[ ]: Match any one character inside brackets.[aeiou]matches any vowel.[0-9]matches any digit.[^0-9]matches any non-digit.
Example:
bash
grep "^[A-Z].*.$" file.txtMatches lines that start with a capital letter and end with a period.
Uses: Text searching (
grep), stream editing (sed), and pattern-based programming. -
-
Question 12 of 20
Explain the different anchors and quantifiers used in regular expressions with suitable examples.
Model answer
Anchors and quantifiers are key components of regular expressions that control where and how many times a pattern matches.
Anchors (position matching):
^— Start of line.^abcmatches lines beginning withabc.$— End of line.xyz$matches lines ending withxyz.\b— Word boundary (in ERE/PCRE).\bcat\bmatches the whole wordcat.
Quantifiers (repetition):
*— Zero or more of the preceding element.bo*matchesb,bo,boo.+— One or more (ERE).bo+matchesbo,boobut notb.?— Zero or one (optional).colou?rmatchescolorandcolour.{n}— Exactly n times.a{3}matchesaaa.{n,}— At least n times.a{2,}matchesaa,aaa, ...{n,m}— Between n and m times.a{2,4}matchesaatoaaaa.
Examples:
bashLines that are empty
grep "^$" file.txt
Match a word with 5 to 8 letters (ERE)
egrep "[a-z]{5,8}" file.txt
Note:
+,?, and{}require escaping in Basic Regular Expressions but work directly in Extended (egrep/grep -E). -
Question 13 of 20
Compare Basic Regular Expressions (BRE) and Extended Regular Expressions (ERE). List the metacharacters that differ between them.
Model answer
Basic Regular Expressions (BRE) and Extended Regular Expressions (ERE) are two standards for defining patterns; they differ mainly in how metacharacters are treated.
Metacharacter BRE ( grep)ERE ( grep -E/egrep).*[]^$Work directly Work directly +(one or more)Must escape: \+Used directly: +?(optional)Must escape: \?Used directly: ?|(alternation)Must escape: \|Used directly: |()(grouping)Must escape: \(\)Used directly: (){}(intervals)Must escape: \{\}Used directly: {}Key differences:
- In BRE, characters like
+,?,|,(),{}are treated as literals unless escaped with a backslash. - In ERE, these are treated as metacharacters by default.
Examples:
bashBRE — match 'cat' or 'dog'
grep "cat|dog" file.txt
ERE — same match
grep -E "cat|dog" file.txt
Conclusion: ERE makes complex patterns more readable, which is why
egrep/grep -Eis preferred for advanced matching. - In BRE, characters like
-
Question 14 of 20
Explain character classes in regular expressions. Describe POSIX character classes with examples.
Model answer
A character class matches any one character from a defined set. It is enclosed in square brackets
[ ].Basic character classes:
[abc]— matchesa,b, orc.[a-z]— matches any lowercase letter (range).[A-Za-z0-9]— matches any alphanumeric character.[^0-9]— negated class; matches any character that is NOT a digit.
POSIX character classes (portable, locale-aware named classes used inside
[[ ]]):POSIX Class Meaning Equivalent [[:alpha:]]Alphabetic characters [A-Za-z][[:digit:]]Digits [0-9][[:alnum:]]Alphanumeric [A-Za-z0-9][[:space:]]Whitespace (space, tab, newline) [[:upper:]]Uppercase letters [A-Z][[:lower:]]Lowercase letters [a-z][[:punct:]]Punctuation characters Examples:
bashMatch lines containing a digit
grep "[[:digit:]]" file.txt
Match words with only alphabets
egrep "^[[:alpha:]]+$" file.txt
Advantage of POSIX classes: They are locale-independent and more readable than raw ranges.
-
Question 15 of 20
With the help of examples, explain how
grepis used with regular expressions to solve real-world text searching problems.Model answer
grepcombined with regular expressions is a powerful tool for pattern-based text searching. Below are practical real-world examples.1. Find lines starting with a specific word:
bash
grep "^Error" logfile.txt2. Find blank lines:
bash
grep "^$" file.txt3. Match email-like patterns (ERE):
bash
grep -E "[a-zA-Z0-9._]+@[a-zA-Z]+.[a-z]+" contacts.txt4. Find IP-address-like patterns:
bash
grep -E "[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}" access.log5. Search for words with exactly 3 letters:
bash
grep -E "\b[a-zA-Z]{3}\b" file.txt6. Find lines containing digits:
bash
grep "[0-9]" data.txt7. Case-insensitive whole-word search:
bash
grep -iw "linux" notes.txtExplanation: Regular expressions let
grepgo beyond fixed strings, enabling matching of structured data like emails, IPs, dates, and codes. Combining with options (-i,-w,-n,-E) increases flexibility. -
Question 16 of 20
Write and explain shell command pipelines to perform the following tasks: (a) Display the top 3 users consuming the most disk space, (b) Count the number of unique words in a file, (c) List the 5 most frequently occurring lines in a log file.
Model answer
These tasks demonstrate the power of combining filters using pipes.
(a) Top 3 users consuming the most disk space:
bash
du -sh /home/* | sort -rh | head -3du -sh /home/*→ shows size of each user's directory.sort -rh→ sorts human-readable sizes in reverse (largest first).head -3→ takes the top 3 entries.
(b) Count the number of unique words in a file:
bash
tr ' ' '\n' < file.txt | sort | uniq | wc -ltr ' ' '\n'→ converts each word onto a separate line.sort→ makes duplicate words adjacent.uniq→ removes duplicates.wc -l→ counts the unique words.
(c) 5 most frequently occurring lines in a log file:
bash
sort log.txt | uniq -c | sort -nr | head -5sort→ groups identical lines together.uniq -c→ counts occurrences of each line.sort -nr→ sorts by count (descending).head -5→ shows the top 5.
Conclusion: Each pipeline chains simple filters to solve a non-trivial task efficiently without writing a program.
-
Question 17 of 20
Explain the concept of standard input, standard output, and standard error streams. How do filters and pipes make use of these streams?
Model answer
In Unix/Linux, every process is associated with three default I/O streams, each identified by a file descriptor:
Stream Name File Descriptor Default Device stdin Standard Input 0 Keyboard stdout Standard Output 1 Terminal (screen) stderr Standard Error 2 Terminal (screen) 1. Standard Input (stdin, 0): The source of data for a command; by default the keyboard.
2. Standard Output (stdout, 1): Where normal output is written; by default the screen.
3. Standard Error (stderr, 2): Where error messages are written; kept separate from stdout so errors don't mix with valid data.
How filters use these streams:
- A filter reads from stdin, processes data, and writes to stdout.
- Example:
sortreads lines from stdin and outputs sorted lines to stdout.
How pipes use these streams:
- A pipe
|connects stdout of one command to stdin of the next.
bash
grep "error" log.txt | sort | uniq
Redirection examples:
bash
command > out.txt # redirect stdout
command 2> err.txt # redirect stderr
command < in.txt # redirect stdin
command > all.txt 2>&1 # merge stderr into stdoutNote: Separating stderr from stdout allows error messages to be handled independently in pipelines.
-
Question 18 of 20
Describe how the
sort,uniq, andwccommands can be combined to generate a word frequency report from a text file. Explain each stage of the pipeline.Model answer
A word frequency report lists each distinct word along with the number of times it appears, usually sorted by frequency. This can be built entirely with filters and pipes.
Complete pipeline:
bash
cat file.txt | tr -s ' ' '\n' | tr 'A-Z' 'a-z' | sort | uniq -c | sort -nrStage-by-stage explanation:
-
cat file.txt— Sends the file contents to stdout. -
tr -s ' ' '\n'— Translates spaces into newlines and squeezes multiple spaces, placing each word on its own line. -
tr 'A-Z' 'a-z'— Converts everything to lowercase soTheandtheare treated as the same word. -
sort— Sorts the words alphabetically so identical words become adjacent (required foruniq). -
uniq -c— Removes duplicates and prefixes each word with its count. -
sort -nr— Sorts numerically in reverse, placing the most frequent words at the top.
Sample output:
15 the 9 and 7 isCounting total unique words:
bash
tr -s ' ' '\n' < file.txt | sort | uniq | wc -lHere
wc -lcounts how many unique words exist.Conclusion: This demonstrates how small filters, chained together, solve a real text-analysis problem elegantly.
-
-
Question 19 of 20
Explain the role of escaping and the backslash (
\) in regular expressions. Why is escaping important, and what happens when metacharacters are used as literals?Model answer
In regular expressions, some characters have special meaning (metacharacters). To match them as literal characters, they must be escaped using a backslash (
\).Common metacharacters that need escaping to be literal:
.*^$[]\+?(){}|Why escaping is important:
- Without escaping, a metacharacter is interpreted by its special function, not as itself.
- Example:
.means any character. To match an actual dot (period), you must write\..
Examples:
bashMatch a literal dot (e.g., a file extension)
grep "file.txt" list.txt
Without escaping, this matches 'fileXtxt', 'file1txt', etc.
grep "file.txt" list.txt
Matching a literal
$or*:
bash
grep "$100" prices.txt # matches the string $100
grep "5 * 3" math.txt # matches 5 * 3Escaping in BRE vs ERE (reversed meaning):
- In BRE,
\+means one or more (metacharacter), while+is literal. - In ERE,
+means one or more, while\+is literal.
Conclusion: Escaping controls whether a character behaves as a metacharacter or a literal, and misusing it is a common source of pattern-matching errors.
-
Question 20 of 20
A file
students.txtcontains records in the formatRollNo:Name:Marks:Branch. Write and explain commands to: (a) Extract only names, (b) Display records sorted by marks in descending order, (c) Count students in the CSE branch.Model answer
Given a colon-delimited file
students.txtwith fieldsRollNo:Name:Marks:Branch, we usecut,sort, andgrepwith pipes.Sample data:
101:Aman:88:CSE
102:Bhavna:76:ECE
103:Chetan:91:CSE(a) Extract only names (field 2):
bash
cut -d":" -f2 students.txt-d":"sets the delimiter to colon.-f2selects the 2nd field (Name).
(b) Display records sorted by marks (field 3) in descending order:
bash
sort -t":" -k3 -nr students.txt-t":"sets colon as the field separator.-k3sorts on the 3rd field (Marks).-nnumeric sort,-rreverse (descending).
(c) Count students in the CSE branch:
bash
grep -c ":CSE$" students.txt-ccounts matching lines.:CSE$matches records ending with:CSE(branch field), avoiding false matches.
Alternative using pipes:
bash
cut -d":" -f4 students.txt | grep -c "CSE"Conclusion: By combining field extraction (
cut), sorting (sortwith keys), and pattern matching (grep), we can perform database-like queries directly on text files.
That's unit 6
Long answers stick best when they're tested again soon. Run the MCQs on this unit, then come back to the questions you found hardest.
Nearly through. Did unit 6 of CSE105 save you a night before the exam?
LPU Notes is free. A student pays for the servers.
Did unit 6 of CSE105 save you a night before the exam?
Then you can help keep it free for the next batch. Ads cover part of the server bill; the rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks.