Text processing in Linux involves using various commands to manipulate and analyze text data efficiently. Commonly used commands include grep for searching through text, sed for stream editing, and awk for pattern scanning and processing. These commands allow users to perform tasks such as filtering, extracting, and transforming information from text files. The benefits of text processing include the automation of data handling, increased productivity, and greater precision in data analysis.
Text Processing Tools
Introduction to grep
The grep command in Unix/Linux is a powerful tool used for searching and manipulating text patterns within files. Its name is derived from the ed (editor) command g/re/p (globally search for a regular expression and print matching lines), which reflects its core functionality. grep is widely used by programmers, system administrators, and users alike for its efficiency and versatility in handling text data. In this article, we will explore the various aspects of the grep command.
grep searches the named input FILEs (or standard input if no files are named, or if a single hyphen-minus (-) is given as file name) for lines containing a match to the given PATTERN. By default, grep prints the matching lines.
How to install grep in Linux?
Grep comes pre-installed in almost every distribution of Linux. However, in case, we can install it with the below command in the terminal window if it is missing from our system:
$ sudo apt-get install grep
Syntax of grep Command in Unix/Linux grep [options] pattern [files]

or
command | grep <searchWord>

Let’s start with some basic grep operations:-

This creates a sample file with names and marks.
Verify the content of marks.txt:

Use grep with a pipe
Execute the command to filter data containing ‘9’

Use grep without a pipe
Run the grep command directly:

Advanced Grep Usage
The -i option enables to search for a string case insensitively in the given file. It matches the words like “UNIX”, “Unix”, “unix”.

2. Count the Number of Matches
Displaying the Count of Number of Matches Using grep
We can find the number of lines that matches the given string/pattern

3. Display the File Names that Matches the Pattern Using grep
We can just display the files that contains the given string/pattern.

4. Checking for the Whole Words in a File Using grep
By default, grep matches the given string/pattern even if it is found as a substring in a file. The -w option to grep makes it match only the whole words.

Some More Practical Examples of grep Command in Linux
To Search for the given string in a single file test.sh
$ cat test.sh #!/bin/bash fun()
echo “This is a test.”
# Terminate our shell script with success message exit 1
fun()
from above file grep exit:
$ grep “exit” demo_file
output:
exit 1
To Checking for the given string in multiple files: in this case test.sh and test1.sh
$ cat test.sh #!/bin/bash fun()
echo “This is a test.”
# Terminate our shell script with success message exit 1
fun()
$ cat test1.sh #!/bin/bash fun()
echo “This is a test1.”
# Terminate our shell script with success message exit 0
fun()
grep exit in both files test.sh and test1.sh:
$ grep exit test*
output:
test1.sh: exit 0
test.sh: exit 1
To Case insensitive search using grep -i, added EXIT in test1.sh
$ cat test1.sh #!/bin/bash fun()
echo “This is a test1.”
# Terminate our shell script with success message, EXIT with 0 exit 0
fun()
$ grep exit test1.sh test1.sh:
exit 0
$ grep -i exit test*
output:
test1.sh:
# Terminate our shell script with success message, EXIT with 0 test1.sh:
exit 0
two lines with -i option, as its case insensitive.
To Match regular expression in files
$ grep “This.*test” test1.sh
output:
echo “This is a test1.”
Three commonly used commands
Let’s start with some basic sed operations:-
First, create a new file to work with:

This replaces the first occurrence of “Hello” with “Hi” on each line. The s command in sed stands for “substitute”.
To replace all occurrences on each line, use the global flag:

This command modifies the file directly, replacing “world” with “sed”.
Let’s check the contents of the file to see the changes:

Explanation:
Now that we understand the basics of sed, let’s explore some more advanced features that make it a powerful tool for text manipulation.
1.Deleting lines:

This deletes the second line of the file. The d command in sed stands for “delete”.
2. Inserting text:

This inserts “First line” before the first line of the file. The i command stands for “insert”.
3. Appending text:

This appends “Last line” at the end of the file. The a command stands for “append”, and $ represents the last line.
4. Multiple commands:

This applies multiple substitutions in one command. The -e option allows you to specify multiple sed commands.
Explanation:
Important note:
AWK Operations:
Useful For:
Programming Constructs:
Syntax of grep Command in Unix/Linux

Options:
Let’s start with some basic awk operations:
First, create a new file with some structured data:

This creates a file named awk_test.txt with a header row and four data rows.
Now, let’s use awk to print specific fields:

This prints the first and second fields of each line.
We can also use conditions:

This prints names of people over 28 years old.
calculates and prints the average age:

This calculates and prints the average age, skipping the header row.
Explanation:
Three powerful text processing commands in Linux:
1.Grep-Pattern Searching:
Features:
Example Uses:
2. sed – Stream Editor:
Features:
Example Uses:
3. awk – Text Processing and Data Extraction:
Features:
Example Uses:
These tools are essential for Linux users and admins, streamlining file searches, text modifications, and data extraction. With practice, they simplify many text processing tasks in daily Linux work.
