Skip to content
Tutorials Center
My learning

Topics

Lesson 38 of 38 ยท Projects and Practice

AWK and sed Practice Exercises with Solutions

Twenty graded AWK and sed exercises with runnable solutions: line selection, substitution, columns, grouping, joins, debugging broken commands, plus a quick cheat sheet.

You learn AWK and sed by solving small problems, not by reading about them. This lesson has twenty exercises in four groups, from easy to challenging. Try each one yourself first: the problem is stated in the text, the data is created in the editor, and the solution sits below it. Run the editor, then change the data and see whether your own command still works.

A good routine: say the task in words, decide the tool (see Combining sed and AWK), write the command, run it on the sample, then try an edge case (empty file, extra spaces, no match).

Group 1: sed warm-ups

1. Print only lines 2 to 4. 2. Delete every line that starts with #. 3. Replace the second a on every line with A. 4. Print the lines between BEGIN and END, excluding those two lines. 5. Join every two lines into one, separated by a space.

Bash Solutions 1-5

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Hints if you got stuck: for 1, -n plus p; for 3, a number after the s command's closing slash picks the occurrence; for 4, the empty regex // repeats the last one used, so //!p skips the range's boundary lines; for 5, N appends the next line to the pattern space.

Group 2: AWK warm-ups

6. Print the third column of sales.txt. 7. Print the total of column 2. 8. Print lines where column 2 is greater than 10 and column 1 does not start with x. 9. Print the name of the row with the largest column 2. 10. Print each line prefixed by its number and the number of fields.

Bash Solutions 6-10

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Group 3: realistic tasks

11. Count how many times each word occurs in a text (ignore case, ignore punctuation), highest first. 12. From grades.csv, print each student's average rounded to one decimal. 13. Print lines that appear more than once, once each. 14. In emails.txt, print only the domain of each address, then count the domains. 15. Convert YYYY-MM-DD dates to DD/MM/YYYY in a file with other text around them.

Bash Solutions 11-15

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

For 11 the tr stages lower-case the text, replace everything that is not a letter or newline with a space, squeeze spaces, and put each word on its own line. A pure AWK version would use tolower and gsub(/[^a-z ]/, ""). Which one reads better to you?

Group 4: challenges

16. Print a file in reverse order, but keep the header line first. 17. Given orders.csv and prices.csv, print each order with its total price (use a join). 18. Print each paragraph (blank-line separated) of notes.txt that contains the word urgent. 19. Transpose a table of numbers. 20. Print the longest line and its length.

Bash Solutions 16-20

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Debugging: what is wrong here?

Two commands that look right and are not. Predict what happens, then run them.

Bash Find the bugs

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The first bug is the most common AWK surprise: when one side of a comparison is a string constant ("6"), AWK compares as text, so "12" > "6" is false. The second shows why n + 0 prints 0 for a never-set counter. The third is sed's greedy matching.

Cheat sheet

Task sed AWK
Print line 5 sed -n 5p awk 'NR==5'
Lines 3 to 6 sed -n 3,6p awk 'NR>=3 && NR<=6'
Delete matching lines sed '/re/d' awk '!/re/'
Replace all sed 's/a/b/g' awk '{gsub(/a/,"b")}1'
Last field (awkward) awk '{print $NF}'
Sum column 2 (not suited) awk '{s+=$2} END{print s}'
Remove duplicate lines (only with cryptic hold-space tricks) awk '!seen[$0]++'
In-place edit sed -i.bak ... awk ... f > t && mv t f
Count lines sed -n '$=' awk 'END{print NR}'

The cheat sheet shows where each tool is natural. The 1 in {gsub(/a/,"b")}1 is a pattern that is always true, so the line is printed after the change.

What to do next

  • Redo the exercises with different data and with edge cases.
  • Rewrite each sed solution in AWK and each AWK solution as a pipeline, then compare readability.
  • Pick a real file from your own work (a log, a CSV export, a config) and ask three questions of it.
  • Read the manual pages (man awk, man sed) once the basics feel comfortable: you will now understand them.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does awk '$2 > "6"' compare?

    When one operand is a string constant the comparison is a string comparison. Use $2 + 0 > 6 for numbers.
  2. 2. What does awk '/a/ { n++ } END { print n + 0 }' print when nothing matches?

    An unset n is an empty string; adding 0 makes it the number 0.
  3. 3. Which command removes HTML tags correctly?

    [^>]* stops at the first >, while .* is greedy and removes everything up to the last >.
  4. 4. How do you keep the header line when reversing a file?

    Handle NR == 1 separately, store the other lines in an array and print them in reverse in END.

Summary

  • Practise by writing the task in words, choosing the tool, testing on small data and trying edge cases.
  • sed excels at line selection and substitution, AWK at columns, arithmetic and grouping.
  • Most mistakes come from string-vs-number comparison, unset variables and greedy regexes.
  • The cheat sheet maps common tasks to the natural tool.
  • Keep solving real problems with your own files: that is where the skill grows.

Next: back to the AWK and sed track page