WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Linux · Intermediate · question 28 of 100

What are the differences between the grep, sed, and awk commands in Linux?

📕 Buy this interview preparation book: 100 Linux questions & answers — PDF + EPUB for $5

In Linux, the grep, sed, and awk commands are often used for manipulating text files and processing text data. Here are some of the key differences between these commands:

grep:

The grep command is used to search for specific patterns of text within a file or set of files. It can be used to search for text using regular expressions, and can be used to match patterns in files of any type. Here’s an example of using grep to search for a pattern within a file:

    $ grep "example" file.txt

In this example, grep is used to search for the word "example" within the file.txt file.

sed:

The sed command is used to perform text transformations on files or streams of text data. It can be used to perform substitutions, deletions, and other types of text manipulations. Here’s an example of using sed to perform a substitution within a file:

    $ sed 's/example/test/g' file.txt

In this example, sed is used to replace all occurrences of "example" with "test" within the file.txt file.

awk:

The awk command is used to process and manipulate text data in files or streams. It is particularly useful for working with structured data, such as data that is organized into columns or fields. awk uses a pattern-action model, where patterns are used to identify specific lines or fields, and actions are used to perform operations on those lines or fields. Here’s an example of using awk to print the first field of a comma-separated value (CSV) file:

    $ awk -F ',' '{print \$1}' file.csv

In this example, awk is used to print the first field of the file.csv file, which is delimited by commas.

Overall, grep, sed, and awk are powerful and flexible tools that are commonly used in Linux for manipulating text data. By understanding the key differences between these commands, users can choose the appropriate tool for their text processing needs.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Linux interview — then scores it.
📞 Practice Linux — free 15 min
📕 Buy this interview preparation book: 100 Linux questions & answers — PDF + EPUB for $5

All 100 Linux questions · All topics