
Text processing in Bash using the grep, sed, and awk commands lets you filter and manipulate input to perform specific tasks, such as file content processing. You can combine the text processing commands with loops, conditional statements, and expressions for advanced processing.
This article explains how to process text in Bash using the grep, sed, and awk commands.
Follow the steps below to create a new Bash script that loads an input file with multiple lines of content to a variable for further text processing.
Create a new log.txt file to store multiple lines of text.
Add the following contents to the log.txt file.
Save and close the file.
Create a new processing.sh Bash script that inputs the log.txt file as a variable.
Add the following contents to the processing.sh file.
Save and close the file.
In the above script, the LOG variable takes the log.txt as an input. You'll process the variable value using the grep, sed, and awk commands in the next sections.
grep Command to Process Text FileThe grep command searches specific text patterns in files, such as the log.txt file you created, and outputs the results for additional processing in a Bash script. Follow the steps below to use the grep command and display the results.
Open the processing.sh script you created earlier.
Add the following command at the end of the file.
Save and close the file.
The above grep command searches for the term ERROR in the log.txt file and displays all matching results.
Run the script using Bash.
Output:
awk Command to Process Text FileThe awk command analyzes structured data and searches for specific patterns in a file. You can use the awk command to customize file contents, split lines into fields or columns for processing, and match specific patterns. Follow the steps below to process the log.txt file columns using the awk command.
Open the processing.sh Bash script.
Add the following contents to the file.
Save and close the file.
The above awk command prints the first and third columns in the log.txt file to display the date and error type.
Run the script using Bash.
Output:
sed Command to Process Text FileStream Editor (sed) is a text processing command that processes and transforms text into a file. The sed command searches, modifies, and replaces contents in standard input or a specific file using Bash. Follow the steps below to use the sed command to search and replace contents in the log.txt file.
Open the processing.sh Bash script.
Add the following command at the end of the file.
Save and close the file.
The above sed command replaces ERROR with New ERROR and WARNING with New WARNING values in the log.txt file.
Run the script using Bash.
Output:
grep, sed, and awk CommandsYou can combine grep, sed, and awk commands to analyze and manipulate large inputs and perform advanced text processing tasks using Bash. Follow the steps below to combine the grep, sed, and awk commands and process the log.txt file.
Create a new combine.sh Bash script.
Add the following contents to the file.
Save and close the file.
In the above Bash script, the combined grep, sed, and awk commands use pipes | to redirect the output of each command and display the modified contents of the log.txt file in a well-structured format that includes all log entries.
Run the script using Bash.
Output:
grep, sed, and awk with Conditional Bash StatementsYou can use grep, sed, and awk commands to process text further with if-else, case, and if-then conditional statements to enhance output readability and handle errors. Follow the steps below to use the conditional statement to process the log.txt file using the search commands in a Bash script.
Create a new conditional-processing.sh Bash script.
Add the following contents to the conditional-processing.sh file.
Save and close the file.
In the above file:
if-else conditional statement validates the input variable.if-else conditional statement runs a grep condition to find the ERROR or WARNING fields in the file. The combined grep, sed, and awk commands run when the condition evaluates to true and perform additional processing to display the modified output.Run the script using Bash.
Output:
grep, sed, and awk with Bash LoopsLoops such as while, for, and until let you run advanced text processing tasks when using the grep,sed, and awk commands. Loops run continuous tasks when a condition returns true or false and are useful when reading multiple lines in files. Follow the steps below to use a while loop to display the contents of the log.txt file using the output from the grep,sed, and awk commands as the input.
Create a new loop-combined.sh script.
Add the following contents to the loop-combined.sh file.
Save and close the file.
The above Bash script prompts a user to enter a command option to run the while loop that displays the contents of the log.txt file using the piped output from the grep,sed, and awk commands on each new line.
Run the script using Bash and set ERROR as a command option to test the while loop.
Output:
Run the script again and set WARNING as the command option.
Output:
You have used the grep, sed, and awk commands in Bash to process text. Combining the commands enables advanced text processing and filtering when handling large data from inputs such as log files. Combine the search commands with loops, functions, and advanced conditional expressions in Bash to create efficient scripts that perform specific tasks such as log monitoring.
0 Comments
Be the first to comment and share your perspective with the community.