Searching within files: Which command displays every line containing the literal string 'sales' from the file named empl.lst?

Difficulty: Easy

Correct Answer: grep sales empl.lst

Explanation:


Introduction / Context:
Searching text is a core Unix capability. grep scans files for patterns and prints lines that match. This forms the basis of countless data-processing pipelines, log reviews, and audit scripts. Recognizing correct grep usage is essential for effective command-line work.



Given Data / Assumptions:

  • The target file is empl.lst.
  • The search pattern is the literal string sales.
  • No regular expression anchors or case-insensitive flags are specified.


Concept / Approach:

The basic grep syntax is grep pattern file. By default, grep prints each matching line to standard output. You can add options like -i for case-insensitive search, -n for line numbers, or -w to match whole words only, but none are required here.



Step-by-Step Solution:

Select the search tool: grep.Apply the pattern and file: grep sales empl.lst.Output: All lines containing “sales”.Optionally refine: grep -i sales empl.lst for case-insensitive matches.


Verification / Alternative check:

Run grep sales empl.lst and compare output against manual inspection with less or more. Line count can be confirmed with grep -c sales empl.lst to ensure completeness.



Why Other Options Are Wrong:

  • cut sales empl.lst: cut selects columns; it does not search for strings.
  • /sales > empl.lst: Looks like a vi search or sed address, not a shell command.
  • cat | /sales > empl.lst: Invalid pipeline; /sales is not a command.
  • None of the above: Incorrect because grep is correct.


Common Pitfalls:

Forgetting to quote patterns containing shell metacharacters; using grep without -F when the pattern includes regex special characters that should be treated literally; writing results back into the same source file inadvertently.



Final Answer:

grep sales empl.lst

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion