Bird
0
0

You want to print lines where the first field contains the substring "cat" anywhere inside it. Which awk command achieves this?

hard📝 Application Q8 of 15
Linux CLI - Text Processing
You want to print lines where the first field contains the substring "cat" anywhere inside it. Which awk command achieves this?
Aawk 'index($1, "cat") == 0 {print $0}' filename
Bawk '$1 == "cat" {print $0}' filename
Cawk '$1 !~ /cat/ {print $0}' filename
Dawk '$1 ~ /cat/ {print $0}' filename
Step-by-Step Solution
Solution:
  1. Step 1: Understand substring matching in awk

    The operator '~' matches regex pattern anywhere in the field.
  2. Step 2: Identify correct pattern for substring

    '$1 ~ /cat/' matches lines where first field contains 'cat' anywhere.
  3. Final Answer:

    awk '$1 ~ /cat/ {print $0}' filename -> Option D
  4. Quick Check:

    Use '~' for regex substring match [OK]
Quick Trick: Use '~ /pattern/' to match substring in awk [OK]
Common Mistakes:
  • Using '==' for substring instead of exact match
  • Using '!~' to exclude instead of include
  • Misusing index function with wrong condition

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Linux CLI Quizzes