0% found this document useful (0 votes)
249 views2 pages

AWK and SED Command Examples in Linux

This document provides examples of using the AWK and SED command line tools in Linux. It describes key features of AWK such as processing fields/columns, supporting regular expressions, and running in three steps of BEGIN, body, and END. Usage examples shown include printing specific fields, matching patterns, and passing output between AWK and SED. The document also covers features of SED such as text editing with regular expressions, input/output options, and instructions to select, delete, substitute and print lines. Usage examples demonstrate printing/deleting lines by number, pattern matching, and modifying files.

Uploaded by

imranpathan22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
249 views2 pages

AWK and SED Command Examples in Linux

This document provides examples of using the AWK and SED command line tools in Linux. It describes key features of AWK such as processing fields/columns, supporting regular expressions, and running in three steps of BEGIN, body, and END. Usage examples shown include printing specific fields, matching patterns, and passing output between AWK and SED. The document also covers features of SED such as text editing with regular expressions, input/output options, and instructions to select, delete, substitute and print lines. Usage examples demonstrate printing/deleting lines by number, pattern matching, and modifying files.

Uploaded by

imranpathan22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

AWK and SED Command Examples in Linux Quick HOWTO

AWK Features:
1. Field/Column processor
2. Supports egrep-compatible (POSIX) RegExes
3. Can return full lines like grep
4. Awk runs 3 steps:
a. BEGIN - optional
b. Body, where the main action(s) take place
c. END - optional
5. Multiple body actions can be executed by separating them using semicolons. e.g.
'{ print $1; print $2 }'
6. Awk, auto-loops through input stream, regardless of the source of the stream. e.g.
STDIN, Pipe, File
Usage:
1. awk '/optional_match/ { action }' file_name | Pipe
2. awk '{ print $1 }' grep1.txt
Note: Use single quotes with awk, to avoid shell interpolation of awk's variables
3. awk '{ print $1,$2 }' grep1.txt
Note: Default input and output field separators is whitespace
4. awk '/linux/ { print } ' grep1.txt - this will print ALL lines containing 'linux'
5. awk '{ if ($2 ~ /Linux/) print}' grep1.txt
6. awk '{ if ($2 ~ /8/) print }' /var/log/messages - this will print the entire line
for log items for the 8th
7. awk '{ print $3 }' /var/log/messages | awk -F: '{ print $1}'
Sed - Stream Editor:
Features:
1. Faciliates automated text editing
2. Supports RegExes (POSIX)
3. Like Awk, supports scripting using '-F' option
4. Supports input via: STDIN, pipe, file
Usage:
1. sed [options] 'instruction[s]' file[s]
2. sed -n '1p' grep1.txt - prints the first line of the file

3. sed -n '1,5p' grep1.txt - prints the first 5 lines of the file


4. sed -n '$p' grep1.txt - prints the last line of the file
5. sed -n '1,3!p' grep1.txt - prints ALL but lines 1-3
6. sed -n '/linux/p' grep1.txt - prints lines with 'linux'
7. sed -e '/^$/d' grep1.txt - deletes blank lines from the document
8. sed -e '/^$/d' grep1.txt > sed1.txt - deletes blank lines from the document
'grep1.txt' and creates 'sed1.txt'
9. sed -ne 's/search/replace/p' sed1.txt
10. sed -ne 's/linux/unix/p' sed1.txt
11. sed -i.bak -e 's/3/4' sed1.txt - this backs up the original file and creates a
new 'sed1.txt' with the modifications indicated in the command
Note: Generally, to create new files, use output redirection, instead of allowing sed
to write to STDOUT
Note: Sed applies each instruction to each line

You might also like