PROGRAMMING THE BASH SHELL PART IV by İlker Korkmaz and Kaya Oğuz

Slides:



Advertisements
Similar presentations
CST8177 awk. The awk program is not named after the sea-bird (that's auk), nor is it a cry from a parrot (awwwk!). It's the initials of the authors, Aho,
Advertisements

1 Unix Talk #2 AWK overview Patterns and actions Records and fields Print vs. printf.
2000 Copyrights, Danielle S. Lahmani UNIX Tools G , Fall 2000 Danielle S. Lahmani Lecture 6.
Linux+ Guide to Linux Certification, Second Edition
Chapter 5 Editing Text Files
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Lecture 02CS311 – Operating Systems 1 1 CS311 – Lecture 02 Outline UNIX/Linux features – Redirection – pipes – Terminating a command – Running program.
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
Advanced File Processing
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Agenda Sed Utility - Advanced –Using Script-files / Example Awk Utility - Advanced –Using Script-files –Math calculations / Operators / Functions –Floating.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
Chapter Four UNIX File Processing. 2 Lesson A Extracting Information from Files.
Guide To UNIX Using Linux Fourth Edition
Unix Talk #2 (sed). 2 You have learned…  Regular expressions, grep, & egrep  grep & egrep are tools used to search for text in a file  AWK -- powerful.
A Guide to Unix Using Linux Fourth Edition
Introduction to Unix (CA263) File Processing. Guide to UNIX Using Linux, Third Edition 2 Objectives Explain UNIX and Linux file processing Use basic file.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
CS 403: Programming Languages Fall 2004 Department of Computer Science University of Alabama Joel Jones.
Advanced File Processing. 2 Objectives Use the pipe operator to redirect the output of one command to another command Use the grep command to search for.
Linux+ Guide to Linux Certification, Third Edition
Chapter Five Advanced File Processing Guide To UNIX Using Linux Fourth Edition Chapter 5 Unix (34 slides)1 CTEC 110.
Chapter Five Advanced File Processing. 2 Objectives Use the pipe operator to redirect the output of one command to another command Use the grep command.
(Stream Editor) By: Ross Mills.  Sed is an acronym for stream editor  Instead of altering the original file, sed is used to scan the input file line.
Introduction to Awk Awk is a convenient and expressive programming language that can be applied to a wide variety of computing and data manipulation tasks.
Agenda Regular Expressions (Appendix A in Text) –Definition / Purpose –Commands that Use Regular Expressions –Using Regular Expressions –Using the Replacement.
Chapter 13: sed Say what?. In this chapter … Basics Programs Addresses Instructions Control Spaces Examples.
Awk Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Introduction to Unix – CS 21 Lecture 12. Lecture Overview A few more bash programming tricks The here document Trapping signals in bash cut and tr sed.
Chapter 12: gawk Yes it sounds funny. In this chapter … Intro Patterns Actions Control Structures Putting it all together.
A talk about AWK Don Newcomb 18 Jan What is AWK? AWK is an interpreted computer language It is primarily used for text processing and data formatting.
Revision Lecture Mauro Jaskelioff. AWK Program Structure AWK programs consists of patterns and procedures Pattern_1 { Procedure_1} Pattern_2 { Procedure_2}
Chapter Five Advanced File Processing. 2 Lesson A Selecting, Manipulating, and Formatting Information.
LIN Unix Lecture 7 Hana Filip. LIN Text Processing Command Line Utility Programs (cont.) sed LAST WEEK wc sort tr uniq awk TODAY join paste.
Lesson 4-Mastering the Visual Editor. Overview Introducing the visual editor. Working in an existing file with vi. Understanding the visual editor. Navigating.
© 2006 KDnuggets [16/Nov/2005:16:32: ] "GET /jobs/ HTTP/1.1" "
CSCI 330 UNIX and Network Programming
Awk- An Advanced Filter by Prof. Shylaja S S Head of the Dept. Dept. of Information Science & Engineering, P.E.S Institute of Technology, Bangalore
1 P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Awk Programming Ruibin Bai (Room AB326) Division of Computer Science The University.
The awk command. Introduction Awk is a programming language used for manipulating data and generating reports. The data may come from standard input,
Sed. Class Issues vSphere Issues – root only until lab 3.
1 Lecture 10 Introduction to AWK COP 3344 Introduction to UNIX.
Linux+ Guide to Linux Certification, Second Edition
FILTERS USING REGULAR EXPRESSIONS – grep and sed.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Awk 2 – more awk. AWK INVOCATION AND OPERATION the "-F" option allows changing Awk's "field separator" character. Awk regards each line of input data.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/9/2006 Lecture 6 – String Processing.
CS 350 Lecture 3 UNIX/Linux Shells by İlker Korkmaz and Kaya Oğuz.
PROGRAMMING THE BASH SHELL PART III by İlker Korkmaz and Kaya Oğuz
Tutorial of Unix Command & shell scriptS 5027
Arun Vishwanathan Nevis Networks Pvt. Ltd.
Lesson 5-Exploring Utilities
CSE 374 Programming Concepts & Tools
CSCI The UNIX System sed - Stream Editor
LINUX LANGUAGE MULTIPLE CHOICE QUESTION SET-5
Guide To UNIX Using Linux Third Edition
Vi Editor.
CS 403: Programming Languages
Tutorial of Unix Command & shell scriptS 5027
Tutorial of Unix Command & shell scriptS 5027
John Carelli, Instructor Kutztown University
Guide To UNIX Using Linux Third Edition
Tutorial of Unix Command & shell scriptS 5027
Unix Talk #2 (sed).
Chapter Four UNIX File Processing.
Linux Shell Script Programming
Awk.
Introduction to Bash Programming, part 3
Presentation transcript:

PROGRAMMING THE BASH SHELL PART IV by İlker Korkmaz and Kaya Oğuz CS350 Lecture8 PROGRAMMING THE BASH SHELL PART IV by İlker Korkmaz and Kaya Oğuz

Programming the bash As pointed in syllabus document, there will be 5 lecture weeks to cover the programming with bash scripts. This lecture is the 4th part on scripting in bash.

References The contents of this lecture are prepared with the help of and based on: the textbook, UNIX Shells by Example (Chapter5, and Chapter6) the tutorial, Advanced Bash-Scripting Guide at http://tldp.org/LDP/abs/html/

Contents of Lecture8 based on the regular expressions sed awk

A note: Midterm will be held on April 28th

Just a review example on the previous lecture concepts How can you print the lines, where the first word begins with K or k, of a given file? If you do not achieve to handle the above operation, it is advised that you should dissect the previous lectures.

Regular Expression concept A Regular Expression (RE) is a pattern of characters used to be matched in a search. The RE may directly be used via the bash operator =~ (RE-match operator). grep is one of the popular tools for RE. sed is another tool, which uses the RE syntax similar as in vi editor. Due to its noninteractive structure, sed lets you type your editing commands at the command line. awk is yet another tool for RE.

sed sed is the streamlined editor sed may be used with RE

How sed works sed processes a file or an input one line at a time and sends its output to the screen. sed stores the processing line in a buffer called pattern space or temporary buffer. sed, like grep, can be used for searching patterns. Moreover, sed has the ability to replace the patterns found. sed has the ability to modify (delete, insert, append) the input file. sed returns exit status of 0 whether or not the pattern is found. It returns nonzero value if sed contains a syntax error.

RE with sed Use forward slashes, / / to search and print: sed -n '/RE/p' fileName example: sed -n '/love/p' myFile.in without -n, all lines will be printed and also the lines containing the pattern will be reprinted. to substitute: sed -n 's/RE/replacementString/' fileName example: sed -n 's/love/like/' myFile.in

sed addressing and commands The addressing may be used to decide which lines to edit. The addresses may be the decimal numbers, or RE (also called context address), or both. Without specifying an address, all lines are issued by sed. Table 5.1 gives the sed commands: d, s, !, r, ... $ sed -n '/[Ll]ove/d' myfile Lines containing the pattern Love or love are deleted. $ sed -n '/Love/!d' myfile Lines not containing the pattern Love are deleted. $ sed '1,3d' myFile Lines 1 through 3 are deleted.

How to modify a file with sed sed is a nondestructive editor. It will display the modifications on the screen, but it will not change the original file. To really effect the modifications in the file, redirection may be used. example: $ sed '2,4d' myFile > tempFile $ mv tempFile myFile

Some examples with sed As an introduction to sed to be used with RE, try to understand Examples 5.8, 5.9, 5.13, 5.16, and 5.18. sed has more abilities according to its commands ( r, w, a, i, c, n, y, q, ...), which may be studied by examples available in Chapter 5 of the textbook.

awk awk is a UNIX programming language used for manipulating data and generating reports. nawk is a newer version of awk and its first initials stand for the authors of the language. Alfred Aho, Brian Kernighan, and Peter Weinberger could have been called as wak, kaw, …; however it is awk. gawk is the GNU version used in Linux. awk scans a file or an input line by line, searching for lines that match a pattern and performing the given actions on those matched lines.

awk with RE formats to be used are: examples: This presentation includes “nawk” command, however you may use “gawk” on Linux. formats to be used are: nawk 'pattern' fileName nawk {action} fileName nawk 'pattern {action}' fileName examples: $ nawk '/Love/' myFile prints all lines containing Love $ nawk '{print $1}' myFile prints the first fields of the lines in myFile The first field starts at the left margin of the line and is delimited by a whitespace. $ nawk '/Love/{print $1, $2}' myFile What gets printed?

awk examples awk may also be used to manipulate the command outputs. $ command | nawk 'pattern {action}' $ df | nawk '$4 > 75000' df reports the free disk space on file systems. If the fourth field of any line of the output of the command df is greater than 75000 (blocks), that line is printed.

How awk works Let's dissect the following command: $ awk '{print $1, $3}' myFile.in awk takes a line of input from the file myFile.in and puts the line into an internal variable called $0 (zero). Each line is also called a record. Next, the line is broken into fields or words separated by a whitespace character. Each field is stored in a numbered variable, starting with $1. awk knows about the whitespace characters according to FS, field separator, which may be changed. awk uses print function with comma, which provides a space in the output. The comma is specially mapped to the internal variable OFS, output field separator. The comma generates whatever assigned to OFS. The above processes continue until all lines in the file have been handled.

Records and Fields for awk Each line is a record and is terminated with a newline. The output record separator, ORS, and the input record separator, RS, can be changed. The number of each record is stored in awk's built-in variable, NR. After a record has been processed, NR is incremented. $0 variable refers to the entire record. Each record consists of fields separated by whitespace (blank spaces or tabs). NF variable stores the number of fields within the current processing record. Fields are stored in the variables $1, $2, … Field separator, FS, holds the value of the input field separator. It may be changed using the -F option.

Using field separator to parse a record To parse a line, use nawk -F example: $ nawk -F: '/CS 350/{print $1, $2}' file.dat The colon character “:” separates the fields. $ nawk -F'[:\t ]' '{print $1, $2}' file.dat Either the colon, the tab, or the space can separate the fields.

awk scripting -f option may be used to include an input file and also a script file within the command awk. format: $ nawk -f myNawkScript myFile You shall dissect Example 6.53 on page 187 of the textbook.

awk programming awk is actually a programming language. The syntax of awk is similar to C programming language. The awk utility provides the UNIX/Linux shell programmers with many features, which are mainly explained in Chapter 6 of the textbook.