https://www.linode.com/docs/guides/differences-between-grep-sed-awk/ * Search * Partners * Docs * Support * Sales * Careers * Log In Linode - Akamai Cloud Computing Logo * Why Linode * Products * Solutions * Marketplace * Pricing * Community * Sign Up * Mobile * Why Linode + Choosing Linode + Support Experience + Predictable Pricing + Cloud Simplified + Free Bundled Services + Global Infrastructure + Customer Stories + Linode for Business + Linode vs. Competitors + Our Approach + What is Cloud Computing? * Products + Products Overview + Compute o Dedicated CPU o Shared CPU o High Memory o GPU o Kubernetes o EdgeWorkers + Storage o Block Storage o Object Storage o Backups + Databases o Managed Databases o MongoDB o MySQL o PostgreSQL + Services o Managed o Professional Services + Networking o Cloud Firewall o DDoS Protection o DNS Manager o NodeBalancers o VLAN + Developer Tools o Cloud Manager o API o CLI o Terraform Provider o Ansible Collection o Images o Integrations o StackScripts o Monitoring o Users & Permissions + Delivery o Adaptive Media o Download Delivery o Ion o Global Traffic + Security o Guardicore o Kona Site Defender o App & API Protector o Bot Manager o Account Protector o EAA * Solutions + Solutions Overview + Digital Agencies + eLearning + Higher Education + Machine Learning + Managed Hosting + Multicloud + SaaS + Security Solutions + Video Streaming + Website Hosting * Marketplace + Browse Marketplace + Submit Marketplace App * Pricing + Pricing List + Cloud Estimator + Cloud Pricing Calculator * Community + Community Overview + Q&A + Developer Portal + Blog + Events + Hackathons + Newsletter + Content Resources + Affiliate Program + Beta Program + Customer Referral Program + Startups Solutions + Swag Store + Documentation + Distributions + Cloud Computing Terms + Promotional Offers * Company + About Linode + Partners + Press Center + Careers + Legal + Sales + System Status * Search * Docs * Blog * Resources * Pricing * Partners * Support * Sales Search [ ] [Search] * Log In * Sign Up Why Linode * Choosing Linode * Support Experience * Predictable Pricing * Cloud Simplified * Free Bundled Services * Global Infrastructure Get to Know Us Better * About Linode * Customer Stories * Linode for Business * Linode vs. Competitors * Our Approach * What is Cloud Computing? View All Products Compute * Dedicated CPU * Shared CPU * High Memory * GPU * Kubernetes * EdgeWorkers Storage * Block Storage * Object Storage * Backups Databases * Managed Databases * MongoDB * MySQL * PostgreSQL Services * Managed * Professional Services Networking * Cloud Firewall * DDoS Protection * DNS Manager * NodeBalancers * VLAN Developer Tools * Cloud Manager * API * CLI * Terraform Provider * Ansible Collection * Images * Integrations * StackScripts * Monitoring * Users & Permissions Delivery * Adaptive Media * Download Delivery * Ion * Global Traffic Security * Guardicore * Kona Site Defender * App & API Protector * Bot Manager * Account Protector * EAA View All Solutions Solutions * Digital Agencies * eLearning * Higher Education * Machine Learning * Managed Hosting * Multicloud * SaaS * Security Solutions * Video Streaming * Website Hosting Pricing * Pricing List * Cloud Estimator * Cloud Pricing Calculator Community * Overview * Q&A * Developer Portal * Blog * Beta Program Engage With Us * Content Resources * Events * Hackathons * Newsletter * Press Center * Affiliate Program * Customer Referral Program * Startup Program * Swag Store * Promotional Offers Explore docs [ ] 1. Docs Home 2. Guides 3. Development 4. Awk 5. * Contents CONTENTS Filtered by search term 1. Docs Home 2. List Sections Tiles Search Results  results matching  results No Results Previous Next Filters Filters ( ) [ ] All [ ] [ ] Add tags [ ] All [ ] 1. Docs Home 2. Guides 3. Development 4. Awk 5. Learn to Process Text in Linux using Grep, sed, and AWK Updated Friday, January 6, 2023, by Andy Lester * Report an Issue * View File * Edit File * Share on Twitter * Share on Facebook * Share on Hacker News Traducciones al Espanol Estamos traduciendo nuestros guias y tutoriales al Espanol. Es posible que usted este viendo una traduccion generada automaticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso. Create a Linode account to try this guide with a $ credit. This credit will be applied to any valid services used during your first days. Sign Up The Differences Between Grep, sed, and AWK Grep, sed, and AWK are all standard Linux tools that are able to process text. Each of these tools can read text files line-by-line and use regular expressions to perform operations on specific parts of the file. However, each tool differs in complexity and what can be accomplished. Grep is used for finding text patterns in a file and is the simplest of the three. Sed can find and modify data, however, its syntax is a bit more complex than grep. AWK is a full-fledged programming language that can process text and perform comparison and arithmetic operations on the extracted text. This guide provides an overview of each tool with examples and includes links to guides in our library that go deeper into each tool. The Grep Command-Line Utility Grep is a Linux utility used to find lines of text in files or input streams using regular expressions. It's name is short for Global Regular Expression Pattern Grep: Search a Log File for Errors Grep is a good tool to use when you need to search for a text pattern in a file. For example, you may need to search for specific text in a system log file. Grep's syntax uses the following format: grep [OPTIONS] PATTERN [FILES...] For example, to find the exact location of your system's Apache error log file, use grep to search for ErrorLog in the Apache configuration file. grep "ErrorLog" /etc/apache2/apache2.conf # ErrorLog: The location of the error log file. # If you do not specify an ErrorLog directive within a ErrorLog ${APACHE_LOG_DIR}/error.log Grep is able to find the configuration named ErrorLog in your Apache configuration file and returns the text as output. The output returned by most Linux distributions highlights the search term in red. To search multiple files using grep, separate each filename with a whitespace character. When grep searches multiple files, its output displays the location of each file where the search term is found. grep "rollover" /var/log/fail2ban.log /var/log/fail2ban.log.1 /var/log/fail2ban.log:2021-08-22 00:00:17,281 fail2ban.server [2467]: INFO rollover performed on /var/log/fail2ban.log /var/log/fail2ban.log.1:2021-08-15 00:00:10,103 fail2ban.server [2467]: INFO rollover performed on /var/log/fail2ban.log Grep: Viewing a Search Term's Context Grep provides several command-line options to control the amount of text surrounding your search term results. The list below contains the available options: * -B num: displays the lines before the search term match. Replace num with the number of lines to display. * -A num: displays the lines after the search term match. Replace num with the number of lines to display * -C num: displays the lines before and after the search term match. Replace num with the number of lines to display. The default value for num is 2. For example, the command will display the 4 lines "before" the pattern match. grep -B 4 "fonts" /var/log/apache2/access.log The output returns the 4 lines before the search term match: 192.0.2.0 - - [17/May/2015:10:05:47 +0000] "GET /presentations/logstash-monitorama-2013/plugin/highlight/highlight.js HTTP/1.1" 200 26185 "http://semicomplete.com/presentations/logstash-monitorama-2013/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/198.51.100.0 Safari/537.36" 192.0.2.0 - - [17/May/2015:10:05:12 +0000] "GET /presentations/logstash-monitorama-2013/plugin/zoom-js/zoom.js HTTP/1.1" 200 7697 "http://semicomplete.com/presentations/logstash-monitorama-2013/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/198.51.100.0 Safari/537.36" 192.0.2.0 - - [17/May/2015:10:05:07 +0000] "GET /presentations/logstash-monitorama-2013/plugin/notes/notes.js HTTP/1.1" 200 2892 "http://semicomplete.com/presentations/logstash-monitorama-2013/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/198.51.100.0 Safari/537.36" 192.0.2.0 - - [17/May/2015:10:05:34 +0000] "GET /presentations/logstash-monitorama-2013/images/sad-medic.png HTTP/1.1" 200 430406 "http://semicomplete.com/presentations/logstash-monitorama-2013/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/198.51.100.0 Safari/537.36" 192.0.2.0 - - [17/May/2015:10:05:57 +0000] "GET /presentations/logstash-monitorama-2013/css/fonts/Roboto-Bold.ttf HTTP/1.1" 200 38720 "http://semicomplete.com/presentations/logstash-monitorama-2013/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/198.51.100.0 Safari/537.36" Note To highlight your search term, add the --color option to your grep command. Grep: List Files that Match a Pattern To find a list of files that match a given pattern, use the -l option. The example below finds all HTML files in a directory that contain the string "copyright". The output returns the list of filenames that contain the matched search term. grep -l -i copyright *.html about.html index.html products.html Grep can also find files that do not match a pattern. To find all the files that don't contain the string "copyright", use the -L option. grep -L -i copyright *.html contact.html how-to-buy.html You can use grep's -l option to perform more complex searches. For example, you can use it to find all files in a directory that contain the pattern "dogs" and "cats". To do so, get a list of the HTML files that contain the word "dogs": grep -l "dogs" *.html Then, search for "cats" in the existing list of files containing the word "dogs": grep -l cats $(grep -l dogs *.html) The output displays a list of files that contain both. To learn more about grep and its command-line options, see our How to Grep for Text in Files guide. The guide also shows you other useful operations, like piping command outputs to grep and how to recursively search through a directory tree. Sed Command Sed is short for Stream Editor and has much of the same functionality as grep. However, sed is much more flexible in how it can select lines of input. It also lets you modify the data it finds. Sed opens a file, reads it line by line, and acts on each line according to its instructions. If you are new to sed, it's syntax can may seem difficult to understand. Once you're used to sed's syntax, you can accomplish powerful operations on your text files and data. Create a Search and Replace sed Script The most common usage of sed is to search for strings or patterns throughout a file and replace them with different strings. For instance, to replace every instance of "Copyright 2020" with "Copyright 2021" in a group of HTML files, use the following command: sed -e' s/Copyright 2020/Copyright 2021/g' index.html > modified.html When your sed programs become too big to easily fit on the command line, sed can accept sets of instructions from a program file. For example, you might have many replacements you want to make. Place them in a file named replacements.sed, as follows: File: replacements.sed 1 s/I should of/I should have/; 2 s/supposably/supposedly/; 3 s/mute point/moot point/; 4 s/one in the same/one and the same/; Apply the changes to all your text files with the following command: sed -f replacements.sed -i.bak *.txt sed: Prepend and Append Lines The i option in sed inserts text before a given line. The a option places text after a given line. For example, the command below inserts a line before line number 4. sed '4 i #This is the extra line' sedtest.txt You should see a similar output after the command executes: This is line #1 This is line #2 This is line #3 #This is the extra line This is line #4 This is line #5 This is line #6 This is line #7 This is line #8 This is line #9 This is line #10 To save the output to a new file named newsedtest.txt, use a redirection operator, as follows: sed '4 i #This is the extra line' sedtest.txt > newsedtest.txt To insert the line before every line where a pattern match is found, use the following command: sed '/8/ i #This line is inserted using sed' sedtest.txt To learn more about sed, see our Manipulate Text from the Command Line with sed guide. The guide shows you how to change file extensions with sed, delete lines from files using sed, and more. AWK Command AWK is the most powerful of the three tools explored in this guide. It is a full-featured programming language with functions, variables, flow control, and support for different data types. AWK's primary use cases are the following: * Processing field-oriented data * Numeric comparisons and calculations * Modifying data based on calculations AWK: Process Data in Multiple Fields The examples in this section rely on the data in the names.txt file below. The data contains information about cars owned by a group of different people. File: names.txt 1 Vince Lombardi Toyota Fordham 1913 2 Betty Ford Chevrolet Bennington 1918 3 Harrison Ford Toyota Ripon 1942 4 Mike Rowe Ford Towson 1962 You can use AWK to find all the people that drive a Toyota. AWK automatically breaks up each line into fields and columns using whitespaces as the delimiter. The first field is stored in variable $1, the second in $2, and so on. To search for the people who own a "Toyota", use the field stored in the $3 variable, as follows: awk '($3 == "Toyota") {print}' names.txt You should see a similar output: Vince Lombardi Toyota Fordham 1913 Harrison Ford Toyota Ripon 1942 The part between the single quotes, ($3 == "Toyota") {print} is a very simple program in the AWK language. It consists of a condition to test against, and an action to take. This is similar to how sed programs match each line against a pattern. AWK: Numeric Comparisons and Calculations Using AWK You can accomplish much more with AWK when searching for text patterns, because it supports comparison and arithmetic operators. For example, to print the last name of people who were born before 1945 (using the data in names.txt) use the following command: awk '($5 < 1945) {print $2}' names.txt You should see a similar output: Lombardi Ford Ford AWK can also perform arithmetic on the fields it works with. To find the birth year average for all people in the names.txt file, use the following AWK command: awk '{total += $5} END {print total/NR}' names.txt The output returns the following average: 1933.75 On each line, AWK adds the value of the fifth column to the variable total. At the end of the file, it prints total divided by NR, a special variable where AWK keeps the number of records it has read. To take a deep dive into the AWK programming language, refer to our Learn the AWK Programming Language guide. Conclusion Grep, sed, and AWK can each be used to search for and process text on a Linux system. However, each tool provides its own strengths. For simple text pattern searches within a directory, grep can complete the task. It's syntax is uncomplicated and it also provides many useful command-line options to enhance its search capabilities. Sed can search for text in files and can also replace the text. This provides you with more out-of-the-box functionality than grep. Finally, AWK is a full-fledged programming language, so you can accomplish a lot more with it. However, it does require a higher learning curve. This page was originally published on Friday, August 27, 2021. linux * Report an Issue * View File * Edit File * Share on Twitter * Share on Facebook * Share on Hacker News --------------------------------------------------------------------- Your Feedback Is Important Let us know if this guide was helpful to you. Provide Feedback --------------------------------------------------------------------- Join the conversation. Read other comments or post your own below. Comments must be respectful, constructive, and relevant to the topic of the guide. Do not post external links or advertisements. Before posting, consider if your comment would be better addressed by contacting our Support team or asking on our Community Site. The Disqus commenting system for Linode Docs requires the acceptance of Functional Cookies, which allow us to analyze site usage so we can measure and improve performance. To view and create comments for this article, please update your Cookie Preferences on this website and refresh this web page. Please note: You must have JavaScript enabled in your browser. Please enable JavaScript to view the comments powered by Disqus. comments powered by Disqus Linode - Akamai Cloud Computing Logo (c) 2003-2022 Linode LLC. All rights reserved. * Site Map * Support * Partners * Careers * Press Center * Legal Center * System Status Why Linode * Choosing Linode * Support Experience * Predictable Pricing * Cloud Simplified * Free Bundled Services * Global Infrastructure * Customer Stories * Linode for Business * Linode vs. Competitors * Our Approach * What is Cloud Computing? Company * About * Partners * Press Center * Careers * Legal Products * Products Overview * Dedicated CPU * Shared CPU * High Memory * GPU * Kubernetes * Block Storage * Object Storage * Backups * Managed Databases * Cloud Firewall * DDoS Protection * DNS Manager * NodeBalancers * VLAN * Managed * Professional Services * Cloud Manager * API * CLI * Terraform Provider * Ansible Collection Solutions * Solutions Overview * Digital Agency * eLearning * Higher Education * Machine Learning * Managed Hosting * Multicloud * SaaS * Security Solutions * Video Streaming * Website Hosting Marketplace * Browse Marketplace * Submit Marketplace App Pricing * Pricing List * Cloud Estimator * Cloud Pricing Calculator Community * Community Overview * Q&A * Developer Portal * Blog * Events * Hackathons * Content Resources * Affiliate Program * Beta Program * Customer Referral Program * Startup Program * Swag Store * Documentation * Distributions * Cloud Computing Terms * Promotional Offers Contact * Support * System Status * Log in Follow Us * Twitter * Instagram * YouTube * LinkedIn * GitHub *