Skip to content
Laviyx Learn

Try

grep, sed and awk Practice

Practise text processing in Linux with grep, sed and awk: search logs with regular expressions, edit files with sed, and build reports with awk. Every solution has been run on Ubuntu and shows its real output.

Intermediate13 problemsUpdated

Review first: Searching Text with grep, Editing Text with sed, Working with Columns in awk, sort, uniq, cut, tr and wc, Pipes and Redirection, Project: Web Log Report

  1. 1.List the visitors

    Beginner

    Print each IP address in access.log once, sorted.

    Starter code (Terminal)
    cat > access.log <<'EOF'
    192.168.1.10 - - [12/Nov/2026:09:00:01] "GET /index.html HTTP/1.1" 200 512
    192.168.1.11 - - [12/Nov/2026:09:00:05] "GET /about.html HTTP/1.1" 200 1024
    192.168.1.10 - - [12/Nov/2026:09:01:10] "POST /login HTTP/1.1" 302 0
    10.0.0.5 - - [12/Nov/2026:09:02:00] "GET /admin HTTP/1.1" 403 128
    192.168.1.12 - - [12/Nov/2026:09:03:30] "GET /missing.html HTTP/1.1" 404 256
    192.168.1.10 - - [12/Nov/2026:09:04:45] "GET /index.html HTTP/1.1" 200 512
    10.0.0.5 - - [12/Nov/2026:09:05:12] "GET /admin HTTP/1.1" 403 128
    192.168.1.11 - - [12/Nov/2026:09:06:00] "GET /api/orders HTTP/1.1" 500 64
    192.168.1.13 - - [12/Nov/2026:09:07:20] "GET /index.html HTTP/1.1" 200 512
    192.168.1.12 - - [12/Nov/2026:09:08:05] "GET /contact.html HTTP/1.1" 200 768
    EOF
    Show hint

    grep -o prints only the matching part. An IP address here is digits and dots at the start of the line.

    Show solution
    Terminal
    grep -oE '^[0-9.]+' access.log | sort -u
    Output
    10.0.0.5
    192.168.1.10
    192.168.1.11
    192.168.1.12
    192.168.1.13
  2. 2.Find failed requests

    Beginner

    Print the lines of access.log whose status code is 4xx or 5xx.

    Starter code (Terminal)
    cat > access.log <<'EOF'
    192.168.1.10 - - [12/Nov/2026:09:00:01] "GET /index.html HTTP/1.1" 200 512
    192.168.1.11 - - [12/Nov/2026:09:00:05] "GET /about.html HTTP/1.1" 200 1024
    192.168.1.10 - - [12/Nov/2026:09:01:10] "POST /login HTTP/1.1" 302 0
    10.0.0.5 - - [12/Nov/2026:09:02:00] "GET /admin HTTP/1.1" 403 128
    192.168.1.12 - - [12/Nov/2026:09:03:30] "GET /missing.html HTTP/1.1" 404 256
    192.168.1.10 - - [12/Nov/2026:09:04:45] "GET /index.html HTTP/1.1" 200 512
    10.0.0.5 - - [12/Nov/2026:09:05:12] "GET /admin HTTP/1.1" 403 128
    192.168.1.11 - - [12/Nov/2026:09:06:00] "GET /api/orders HTTP/1.1" 500 64
    192.168.1.13 - - [12/Nov/2026:09:07:20] "GET /index.html HTTP/1.1" 200 512
    192.168.1.12 - - [12/Nov/2026:09:08:05] "GET /contact.html HTTP/1.1" 200 768
    EOF
    Show hint

    The status code comes right after the closing quote: grep -E '" [45][0-9]{2} '.

    Show solution
    Terminal
    grep -E '" [45][0-9]{2} ' access.log
    Output
    10.0.0.5 - - [12/Nov/2026:09:02:00] "GET /admin HTTP/1.1" 403 128
    192.168.1.12 - - [12/Nov/2026:09:03:30] "GET /missing.html HTTP/1.1" 404 256
    10.0.0.5 - - [12/Nov/2026:09:05:12] "GET /admin HTTP/1.1" 403 128
    192.168.1.11 - - [12/Nov/2026:09:06:00] "GET /api/orders HTTP/1.1" 500 64
  3. 3.Show the lines around an error

    Intermediate

    Print the line with the 500 error, with one line before it and one line after it.

    Starter code (Terminal)
    cat > access.log <<'EOF'
    192.168.1.10 - - [12/Nov/2026:09:00:01] "GET /index.html HTTP/1.1" 200 512
    192.168.1.11 - - [12/Nov/2026:09:00:05] "GET /about.html HTTP/1.1" 200 1024
    192.168.1.10 - - [12/Nov/2026:09:01:10] "POST /login HTTP/1.1" 302 0
    10.0.0.5 - - [12/Nov/2026:09:02:00] "GET /admin HTTP/1.1" 403 128
    192.168.1.12 - - [12/Nov/2026:09:03:30] "GET /missing.html HTTP/1.1" 404 256
    192.168.1.10 - - [12/Nov/2026:09:04:45] "GET /index.html HTTP/1.1" 200 512
    10.0.0.5 - - [12/Nov/2026:09:05:12] "GET /admin HTTP/1.1" 403 128
    192.168.1.11 - - [12/Nov/2026:09:06:00] "GET /api/orders HTTP/1.1" 500 64
    192.168.1.13 - - [12/Nov/2026:09:07:20] "GET /index.html HTTP/1.1" 200 512
    192.168.1.12 - - [12/Nov/2026:09:08:05] "GET /contact.html HTTP/1.1" 200 768
    EOF
    Show hint

    -B shows lines before a match, and -A lines after.

    Show solution
    Terminal
    grep -B1 -A1 '" 500 ' access.log
    Output
    10.0.0.5 - - [12/Nov/2026:09:05:12] "GET /admin HTTP/1.1" 403 128
    192.168.1.11 - - [12/Nov/2026:09:06:00] "GET /api/orders HTTP/1.1" 500 64
    192.168.1.13 - - [12/Nov/2026:09:07:20] "GET /index.html HTTP/1.1" 200 512
  4. 4.Reorder names

    Intermediate

    names.txt has lines like Rao, Asha. Print them as Asha Rao.

    Starter code (Terminal)
    cat > names.txt <<'EOF'
    Rao, Asha
    Kumar, Ravi
    Iyer, Meena
    EOF
    Show hint

    Capture both parts with sed -E and groups, then write them as \2 \1.

    Show solution
    Terminal
    sed -E 's/^([^,]+), (.+)$/\2 \1/' names.txt
    Output
    Asha Rao
    Ravi Kumar
    Meena Iyer
  5. 5.Remove comments and blank lines

    Beginner

    Print app.conf without its comment lines and blank lines.

    Starter code (Terminal)
    cat > app.conf <<'EOF'
    # App settings
    host=localhost
    
    # Port for the web server
    port=8080
    debug=false
    EOF
    Show hint

    Two sed delete commands: /^#/d and /^$/d.

    Show solution
    Terminal
    sed -e '/^#/d' -e '/^$/d' app.conf
    Output
    host=localhost
    port=8080
    debug=false
  6. 6.Count requests by status code

    Beginner

    Print how many requests had each status code, sorted by code.

    Starter code (Terminal)
    cat > access.log <<'EOF'
    192.168.1.10 - - [12/Nov/2026:09:00:01] "GET /index.html HTTP/1.1" 200 512
    192.168.1.11 - - [12/Nov/2026:09:00:05] "GET /about.html HTTP/1.1" 200 1024
    192.168.1.10 - - [12/Nov/2026:09:01:10] "POST /login HTTP/1.1" 302 0
    10.0.0.5 - - [12/Nov/2026:09:02:00] "GET /admin HTTP/1.1" 403 128
    192.168.1.12 - - [12/Nov/2026:09:03:30] "GET /missing.html HTTP/1.1" 404 256
    192.168.1.10 - - [12/Nov/2026:09:04:45] "GET /index.html HTTP/1.1" 200 512
    10.0.0.5 - - [12/Nov/2026:09:05:12] "GET /admin HTTP/1.1" 403 128
    192.168.1.11 - - [12/Nov/2026:09:06:00] "GET /api/orders HTTP/1.1" 500 64
    192.168.1.13 - - [12/Nov/2026:09:07:20] "GET /index.html HTTP/1.1" 200 512
    192.168.1.12 - - [12/Nov/2026:09:08:05] "GET /contact.html HTTP/1.1" 200 768
    EOF
    Show hint

    Count into an awk array, then print it in the END block.

    Show solution
    Terminal
    awk '{count[$8]++} END {for (s in count) print s, count[s]}' access.log | sort
    Output
    200 5
    302 1
    403 2
    404 1
    500 1
  7. 7.Total bytes for successful requests

    Beginner

    Add up the bytes (the last field) for the requests with status 200.

    Starter code (Terminal)
    cat > access.log <<'EOF'
    192.168.1.10 - - [12/Nov/2026:09:00:01] "GET /index.html HTTP/1.1" 200 512
    192.168.1.11 - - [12/Nov/2026:09:00:05] "GET /about.html HTTP/1.1" 200 1024
    192.168.1.10 - - [12/Nov/2026:09:01:10] "POST /login HTTP/1.1" 302 0
    10.0.0.5 - - [12/Nov/2026:09:02:00] "GET /admin HTTP/1.1" 403 128
    192.168.1.12 - - [12/Nov/2026:09:03:30] "GET /missing.html HTTP/1.1" 404 256
    192.168.1.10 - - [12/Nov/2026:09:04:45] "GET /index.html HTTP/1.1" 200 512
    10.0.0.5 - - [12/Nov/2026:09:05:12] "GET /admin HTTP/1.1" 403 128
    192.168.1.11 - - [12/Nov/2026:09:06:00] "GET /api/orders HTTP/1.1" 500 64
    192.168.1.13 - - [12/Nov/2026:09:07:20] "GET /index.html HTTP/1.1" 200 512
    192.168.1.12 - - [12/Nov/2026:09:08:05] "GET /contact.html HTTP/1.1" 200 768
    EOF
    Show hint

    A condition before the braces filters the lines: $8 == 200 {...}. The status code is field 8, because the date in brackets counts as one field.

    Show solution
    Terminal
    awk '$8 == 200 {total += $9} END {print total}' access.log
    Output
    3328
  8. 8.Most requested pages

    Intermediate

    Print the three most requested paths with their counts, most requested first.

    Starter code (Terminal)
    cat > access.log <<'EOF'
    192.168.1.10 - - [12/Nov/2026:09:00:01] "GET /index.html HTTP/1.1" 200 512
    192.168.1.11 - - [12/Nov/2026:09:00:05] "GET /about.html HTTP/1.1" 200 1024
    192.168.1.10 - - [12/Nov/2026:09:01:10] "POST /login HTTP/1.1" 302 0
    10.0.0.5 - - [12/Nov/2026:09:02:00] "GET /admin HTTP/1.1" 403 128
    192.168.1.12 - - [12/Nov/2026:09:03:30] "GET /missing.html HTTP/1.1" 404 256
    192.168.1.10 - - [12/Nov/2026:09:04:45] "GET /index.html HTTP/1.1" 200 512
    10.0.0.5 - - [12/Nov/2026:09:05:12] "GET /admin HTTP/1.1" 403 128
    192.168.1.11 - - [12/Nov/2026:09:06:00] "GET /api/orders HTTP/1.1" 500 64
    192.168.1.13 - - [12/Nov/2026:09:07:20] "GET /index.html HTTP/1.1" 200 512
    192.168.1.12 - - [12/Nov/2026:09:08:05] "GET /contact.html HTTP/1.1" 200 768
    EOF
    Show hint

    The path is field 6. Use sort | uniq -c, then sort by the count.

    Show solution
    Terminal
    awk '{print $6}' access.log | sort | uniq -c | sort -k1,1nr -k2 | head -n 3
    Output
          3 /index.html
          2 /admin
          1 /about.html
  9. 9.Revenue by city

    Intermediate

    From orders.csv, print the revenue (qty times price) for each city in aligned columns, sorted by city.

    Starter code (Terminal)
    cat > orders.csv <<'EOF'
    order_id,customer,city,product,qty,price
    1001,Asha,Hyderabad,Notebook,3,40
    1002,Ravi,Pune,Pen,10,10
    1003,Meena,Hyderabad,Bag,1,850
    1004,Karan,Chennai,Notebook,5,40
    1005,Asha,Hyderabad,Pen,4,10
    1006,Divya,Pune,Bag,2,850
    1007,Ravi,Pune,Notebook,2,40
    EOF
    Show hint

    -F, splits on commas, NR > 1 skips the header, and printf "%-10s %6d\n" aligns the columns.

    Show solution
    Terminal
    awk -F, 'NR > 1 {total[$3] += $5 * $6} END {for (c in total) printf "%-10s %6d\n", c, total[c]}' orders.csv | sort
    Output
    Chennai       200
    Hyderabad    1010
    Pune         1880
  10. 10.Orders above a limit

    Intermediate

    Print the orders worth more than 100, showing the order id, the customer and the value. Pass the limit to awk as a variable.

    Starter code (Terminal)
    cat > orders.csv <<'EOF'
    order_id,customer,city,product,qty,price
    1001,Asha,Hyderabad,Notebook,3,40
    1002,Ravi,Pune,Pen,10,10
    1003,Meena,Hyderabad,Bag,1,850
    1004,Karan,Chennai,Notebook,5,40
    1005,Asha,Hyderabad,Pen,4,10
    1006,Divya,Pune,Bag,2,850
    1007,Ravi,Pune,Notebook,2,40
    EOF
    Show hint

    awk -v min=100 makes min available in the program.

    Show solution
    Terminal
    awk -F, -v min=100 'NR > 1 && $5 * $6 > min {print $1, $2, $5 * $6}' orders.csv
    Output
    1001 Asha 120
    1003 Meena 850
    1004 Karan 200
    1006 Divya 1700
  11. 11.Average quantity per product

    Intermediate

    Print the average quantity ordered for each product, with one decimal place, sorted by product.

    Starter code (Terminal)
    cat > orders.csv <<'EOF'
    order_id,customer,city,product,qty,price
    1001,Asha,Hyderabad,Notebook,3,40
    1002,Ravi,Pune,Pen,10,10
    1003,Meena,Hyderabad,Bag,1,850
    1004,Karan,Chennai,Notebook,5,40
    1005,Asha,Hyderabad,Pen,4,10
    1006,Divya,Pune,Bag,2,850
    1007,Ravi,Pune,Notebook,2,40
    EOF
    Show hint

    Keep two arrays, a sum and a count, then divide in END.

    Show solution
    Terminal
    awk -F, 'NR > 1 {sum[$4] += $5; n[$4]++} END {for (p in sum) printf "%s %.1f\n", p, sum[p] / n[p]}' orders.csv | sort
    Output
    Bag 1.5
    Notebook 3.3
    Pen 7.0
  12. 12.Read one section of a config file

    Advanced

    Print only the settings in the [database] section of settings.ini.

    Starter code (Terminal)
    cat > settings.ini <<'EOF'
    [server]
    host=0.0.0.0
    port=80
    [database]
    host=db.local
    port=5432
    user=admin
    [cache]
    ttl=300
    EOF
    Show hint

    When a line starts with [, remember whether it is [database]. A pattern without an action prints the line.

    Show solution
    Terminal
    awk '/^\[/ {inside = ($0 == "[database]"); next} inside' settings.ini
    Output
    host=db.local
    port=5432
    user=admin
  13. 13.A one-command log report

    Advanced

    With a single awk command, print the number of requests, the number of different visitors, and the number of errors (status 400 or above) with the percentage.

    Starter code (Terminal)
    cat > access.log <<'EOF'
    192.168.1.10 - - [12/Nov/2026:09:00:01] "GET /index.html HTTP/1.1" 200 512
    192.168.1.11 - - [12/Nov/2026:09:00:05] "GET /about.html HTTP/1.1" 200 1024
    192.168.1.10 - - [12/Nov/2026:09:01:10] "POST /login HTTP/1.1" 302 0
    10.0.0.5 - - [12/Nov/2026:09:02:00] "GET /admin HTTP/1.1" 403 128
    192.168.1.12 - - [12/Nov/2026:09:03:30] "GET /missing.html HTTP/1.1" 404 256
    192.168.1.10 - - [12/Nov/2026:09:04:45] "GET /index.html HTTP/1.1" 200 512
    10.0.0.5 - - [12/Nov/2026:09:05:12] "GET /admin HTTP/1.1" 403 128
    192.168.1.11 - - [12/Nov/2026:09:06:00] "GET /api/orders HTTP/1.1" 500 64
    192.168.1.13 - - [12/Nov/2026:09:07:20] "GET /index.html HTTP/1.1" 200 512
    192.168.1.12 - - [12/Nov/2026:09:08:05] "GET /contact.html HTTP/1.1" 200 768
    EOF
    Show hint

    Count visitors the first time each IP appears: if (!($1 in seen)).

    Show solution
    Terminal
    awk '{
      n++
      if (!($1 in seen)) { seen[$1] = 1; visitors++ }
      if ($8 >= 400) errors++
    } END {
      printf "Requests: %d\nVisitors: %d\nErrors: %d (%.0f%%)\n", n, visitors, errors, errors * 100 / n
    }' access.log
    Output
    Requests: 10
    Visitors: 5
    Errors: 4 (40%)