Fundamentals 7 min read

Mastering Linux’s Three Musketeers: grep, sed, and awk

This article introduces Linux’s three essential text‑processing tools—grep for searching, sed for stream editing, and awk for column‑based reporting—explaining their key options, demonstrating practical command‑line examples, and showing how each excels in specific data‑handling scenarios.

Linyb Geek Road
Linyb Geek Road
Linyb Geek Road
Mastering Linux’s Three Musketeers: grep, sed, and awk

In Linux, grep , sed and awk are collectively known as the “three musketeers” of text processing, tightly coupled with regular expressions to provide powerful data‑handling capabilities.

grep

grep (Global Regular Expression Print) searches files for lines matching a pattern. Common options include: -i: ignore case -v: invert match -n: show line numbers -r: recursive search -o: output only matching parts -E or --extended-regexp: use extended regular expressions

Example file test.txt:

a b c d e
1 2 3 4 5
G E d C T G B
l k j h g f

Typical commands and results:

grep c test.txt
ab c d e
grep -i c test.txt
ab c d e
G E d C T G B
grep -io c test.txt
c
C

Matching multiple patterns:

grep -E 'c|h' test.txt
ab c d e
l k j h g f
grep 'c\|h' test.txt
ab c d e
l k j h g f

sed

sed (stream editor) filters and transforms text streams. The substitution command s/old/new/g replaces all occurrences of old with new. Adding -i edits files in place. sed 's/old/new/g' file In‑place editing example:

sed -i 's/原字符串/替换字符串/g' filename

Counting lines with wc -l counts newline characters, which may differ from the actual number of logical lines. Using sed: sed -n "$=" filename returns the line number of the last line, effectively counting lines even when the final newline is missing. The -n flag suppresses default output, and $= prints the line number of the last line.

awk

awk excels at column‑oriented processing. The -F option sets the field separator; in the examples the separator is |@| (escaped as \|\@\|).

Count fields in the last record: awk -F '\|\@\|' 'END{print NF}' filename Print the first column: awk -F '\|\@\|' '{print $1}' filename Print the first and third columns: awk -F '\|\@\|' '{print $1,$3}' filename Filter rows where the sixth column contains the word “money”:

awk -F '\|\@\|' '{print $6}' filename | grep money

Conclusion

These three tools are indispensable for Linux text processing. grep shines at searching, sed at editing streams, and awk at generating reports from columnar data. Mastering the specific strengths of each dramatically improves data‑handling efficiency.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

linuxcommand linetext processinggrepawksed
Linyb Geek Road
Written by

Linyb Geek Road

Tech notes

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.