wEEK 7

Advanced Text Editing Techniques

Why Text Editing Mastery Matters in Linux

In Week 6, we learned the essentials of working with text:

  • Creating and viewing files with echo and cat

  • Editing with nano and vim

  • Navigating and inspecting with less, wc, diff, and grep

  • Organizing with directories and mv

  • Searching with grep and in-editor tools

Those skills gave us the foundation to work with text files safely and effectively. Now, we are ready to move beyond basic viewing and editing. This week, we focus on advanced text editing techniques that let you transform, filter, and analyze text at scale.

P.A.G.E.S. Framework

P — Print with cat

The cat command is one of the most widely used text tools in Linux because it gives an immediate view of file contents. System administrators and developers rely on it constantly: to quickly check configuration files, confirm the results of scripts, preview logs, or even combine files together. It is also a common starting point in pipelines, where the output from cat is passed into other tools for filtering, searching, or counting. Its simplicity and speed make it the go-to choice when you want to see raw text without opening an editor. While cat is best for smaller files, it’s also used to glue files together, to append new data into logs, and to send file contents directly into processing commands. 

Use CaseCommandExampleNotes
Print filecat file.txtcat notes.txtQuick way to view small files.
Print multiplecat file1 file2cat intro.txt body.txtDisplays files in sequence.
Merge into new filecat file1 file2 > combined.txtcat jan.csv feb.csv > q1.csvCreates a single output file.
Append to filecat file1 >> output.txtcat update.log >> all.logAdds without erasing existing content.
Pipe into search`cat filegrep error``cat syslog
Pipe into count`cat filewc -l``cat data.csv
Page through file`cat fileless``cat large.txt
Better alternativegrep "error" fileinstead of `cat filegrep “error”`
Alternative tooltail -f logfile.txttail -f syslogLive log monitoring.

A — Analyze with wc

The wc command (“word count”) is widely used because it gives a quick numerical summary of a file: how many lines, words, and characters it contains. Administrators use it to check the size of configuration files, verify that a dataset has the expected number of rows, or confirm that a script output is complete. It is also a common step in pipelines, where wc is used to count filtered results from tools like grep. This makes it valuable not just for measuring file size, but also for validating that processing steps are working as intended.

Use CaseCommandExampleNotes
Count all (lines, words, chars)wc file.txtwc notes.txtShows full summary.
Count lineswc -l file.txtwc -l access.logCommon for counting records or log entries.
Count wordswc -w file.txtwc -w report.txtUseful for documentation or essays.
Count characterswc -c file.txtwc -c config.cfgMeasures raw size in bytes.
Pipe into wcgrep "error" log.txt | wc -lCounts how many lines matched a search.
Check dataset rowscat data.csv | wc -lConfirms number of records in a file.

G — Grep for Searching (with Regex)

The grep command is used constantly in Linux because it can scan through files and print only the lines that match what you’re looking for. This is especially valuable for system logs, error messages, and configuration files where scrolling line by line would take too long. grep works with simple keywords, but it becomes far more powerful when combined with regular expressions (regex), which let you search for patterns like numbers, dates, or strings at the start or end of a line. Administrators and developers use grep not only to troubleshoot (finding “error” in a log) but also to analyze datasets (locating all rows with certain fields), or even to confirm whether a configuration contains the right parameters.

Use CaseCommandExampleNotes
Simple searchgrep text filegrep "error" syslogFinds all lines containing “error.”
Show line numbersgrep -n text filegrep -n "server" config.txtHelps navigate to exact spots in configs.
Case-insensitivegrep -i text filegrep -i "warning" log.txtMatches regardless of letter case.
Regex searchgrep -E "[0-9]{3}" filegrep -E "[0-9]{3}" data.txtMatches three-digit numbers.
Search start of linegrep "^pattern" filegrep "^server" config.txtFinds lines beginning with “server.”
Search with contextgrep -C 2 text filegrep -C 2 "error" syslogShows matching lines with 2 lines above and below.
Pipe searchcat file | grep textcat syslog | grep "timeout"Works in pipelines, though direct file search is faster.

Navigate the tree​

E — Extract with cut

The cut command is used to pull out specific columns or character ranges from text files, which is especially useful for structured data like CSVs or log files. Instead of reading every field, you can extract just the pieces you need, such as names, scores, or dates. The tr command (translate) complements this by transforming or cleaning up characters — for example, converting lowercase to uppercase, deleting digits, or replacing spaces with newlines. Together, cut and tr allow quick parsing and cleanup of text without opening a full editor.

Use CaseCommandExampleNotes
Extract a columncut -d',' -f1 file.csvcut -d',' -f1 scores.csvGets the first column (e.g., names).
Extract multiple fieldscut -d',' -f1,3 file.csvcut -d',' -f1,3 records.csvGrabs name and department.
Extract characterscut -c1-5 file.txtcut -c1-5 ids.txtShows first 5 characters per line.
Combine with sortcut -d',' -f2 scores.csv | sort -nSorts scores in numeric order.
Delete characterstr -d '0-9' < file.txtRemoves all digits from text.
Convert casetr 'a-z' 'A-Z' < file.txtConverts text to uppercase.
Split into linestr ' ' '\n' < words.txtBreaks words into separate lines for counting or sorting.
 

S — Sort for Organizing

Sorting and processing text is essential when working with logs, reports, or datasets. The sort command organizes lines alphabetically or numerically and can remove duplicates when paired with uniq. For deeper analysis, awk treats each line as a record and works with fields (columns), letting you print, calculate, or filter specific data. The sed command is a stream editor that automates text substitutions, deletions, or pattern-based edits across entire files. Together, these three tools let you go from raw, unstructured text to clean, organized, and even summarized output.

Tool & Use CaseCommandExampleNotes
Sort alphabeticallysort file.txtsort names.txtOrganizes text into order.
Sort numericallysort -n file.txtsort -n ages.txtUseful for numbers like scores or IDs.
Unique with countssort file.txt | uniq -csort words.txt | uniq -cShows frequency of each line.
Select column (awk)awk '{print $1}' file.txtawk '{print $1}' records.txtPrints the first field (e.g., names).
Multiple fields (awk)awk -F',' '{print $1, $3}' file.csvawk -F',' '{print $1, $3}' employees.csvExtracts specific CSV columns.
Summation (awk)awk '{sum += $2} END {print sum}' sales.txtAdds up a numeric column.
Replace text (sed)sed 's/error/warning/' file.txtsed 's/error/warning/' log.txtReplaces first match on each line.
Replace all (sed)sed 's/error/warning/g' file.txtReplaces every occurrence on each line.
Delete line (sed)sed '2d' file.txtRemoves a specific line by number.

This concludes Lecture 7. Please return to Blackboard to access the Week 7 materials.

Scroll to Top