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

Practical 3 OS

The document outlines practical exercises in shell script programming using grep, sed, and awk commands. It explains how to use grep for searching text patterns in log files, sed for searching and replacing strings in files, and awk for processing field-oriented data and performing calculations. Each command is accompanied by examples demonstrating its syntax and functionality.

Uploaded by

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

Practical 3 OS

The document outlines practical exercises in shell script programming using grep, sed, and awk commands. It explains how to use grep for searching text patterns in log files, sed for searching and replacing strings in files, and awk for processing field-oriented data and performing calculations. Each command is accompanied by examples demonstrating its syntax and functionality.

Uploaded by

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

Name:- Shivam Kapate

PRN:- 24025081245513

Practical No. 03
Aim:- Shell Script programming using the commands grep,awk,and sed

 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.
Greps syntax :
grep [OPTIONS] PATTERN [FILES...]
For example, to find the exact location of your systems Apache error log file, use grep to search
for ErrorLog in the Apache configuration file.
grep "ErrorLog" /etc/apache2/apache2.conf

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

 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, its syntax can may seem difficult to understand. Once youre used to seds 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:
 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.
AWKs primary use cases are the following:
Processing field-oriented data
Numeric comparisons and calculations
Modifying data based on calculations
awk '($3 == "Toyota") {print} names.txt

awk '($5 < 1945) {print $2} names.txt

You might also like