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.

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

CST8177 sed The Stream Editor. The original editor for Unix was called ed, short for editor. By today's standards, ed was very primitive. Soon, sed was.
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.
CS 497C – Introduction to UNIX Lecture 29: - Filters Using Regular Expressions – grep and sed Chin-Chih Chang
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Linux+ Guide to Linux Certification, Second Edition
CS Lecture 04 Outline Change your shell Sed and awk Tricks with Bash scripts Assignment 1 discussion 1CS 311 Operating SystemsLecture 04.
Introduction to Unix – CS 21 Lecture 13. Lecture Overview Finding files and programs which whereis find xargs Putting it all together for some complex.
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.
Introduction to Unix – CS 21 Lecture 5. Lecture Overview Lab Review Useful commands that will illustrate today’s lecture Streams of input and output File.
Unix Filters Text processing utilities. Filters Filter commands – Unix commands that serve dual purposes: –standalone –used with other commands and pipes.
UNIX Filters.
Filters using Regular Expressions grep: Searching a Pattern.
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
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.
LIN 6932 Unix Lecture 6 Hana Filip. LIN 6932 HW6 - Part II solutions posted on my website see syllabus.
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.
Introduction to Unix (CA263) File Processing. Guide to UNIX Using Linux, Third Edition 2 Objectives Explain UNIX and Linux file processing Use basic file.
Unix programming Term: III B.Tech II semester Unit-II PPT Slides Text Books: (1)unix the ultimate guide by Sumitabha Das (2)Advanced programming.
Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.
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.
Chapter Five Advanced File Processing Guide To UNIX Using Linux Fourth Edition Chapter 5 Unix (34 slides)1 CTEC 110.
Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections.
(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.
Programmable Text Processing with awk Lecturer: Prof. Andrzej (AJ) Bieszczad Phone: “UNIX for Programmers and Users”
Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.
Agenda Regular Expressions (Appendix A in Text) –Definition / Purpose –Commands that Use Regular Expressions –Using Regular Expressions –Using the Replacement.
Revision Lecture Mauro Jaskelioff. AWK Program Structure AWK programs consists of patterns and procedures Pattern_1 { Procedure_1} Pattern_2 { Procedure_2}
Introduction to Unix – CS 21
BY A Mikati & M Shaito Awk Utility n Introduction n Some basics n Some samples n Patterns & Actions Regular Expressions n Boolean n start /end n.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Chapter Five Advanced File Processing. 2 Lesson A Selecting, Manipulating, and Formatting Information.
Introduction to sed. Sed : a “S tream ED itor ” What is Sed ?  A “non-interactive” text editor that is called from the unix command line.  Input text.
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.
Searching and Sorting. Why Use Data Files? There are many cases where the input to the program may come from a data file.Using data files in your programs.
Advanced Text Processing. 222 Lecture Overview  Character manipulation commands cut, paste, tr  Line manipulation commands sort, uniq, diff  Regular.
16-Dec-15Advanced Programming Spring 2002 sed and awk Henning Schulzrinne Dept. of Computer Science Columbia University.
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
ORAFACT Text Processing. ORAFACT Searching Inside Files grep - searches for patterns within files grep [options] [[-e] pattern] filename [...] -n shows.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
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
Programming Languages Meeting 12 November 18/19, 2014.
Filters and Utilities. Notes: This is a simple overview of the filtering capability Some of these commands are very powerful ▫Only showing some of the.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/9/2006 Lecture 6 – String Processing.
Arun Vishwanathan Nevis Networks Pvt. Ltd.
Lesson 5-Exploring Utilities
CST8177 sed The Stream Editor.
PROGRAMMING THE BASH SHELL PART IV by İlker Korkmaz and Kaya Oğuz
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).
Awk.
Introduction to Bash Programming, part 3
Software I: Utilities and Internals
Presentation transcript:

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 awk

The Here Document Redirects stdin to a specific set of text located inside the same file << COMMAND << MARKER Data MARKER

Example of Here

Why Is This Useful? Allows you to keep all relevant information in one file Example: Database you want to search Don’t need to clutter up your directory with unnecessary temporary files

Trapping Signals Catching a signal is also called trapping a signal You can tell bash programs what to do when they receive different signals Analogy: When a postcard arrives, what do I do?

The ‘trap’ Command Usage: trap ‘COMMAND’ Signals Example: trap ‘cat errorMsg’ 4 6 In order to prevent you from running a program forever, signal number 9 cannot be trapped

Example Of trap

Two Helper Filters cut Break individual lines apart tr Change characters into different characters

The ‘cut’ Command More precise control over individual lines Will cut out certain words from each individual line so they can be processed Usage: cut [FLAGS] FILE

Flags -d Delimiter -f Field number Example cut –d: -f3 myFile

Example Of Cut

The ‘tr’ Command Translate Change on a one to one basis characters from one thing to another Usage: tr ‘Set1’ ‘Set2’ Example: tr ‘abc’ ‘ABC’ < myFile

Example Of tr

Two More Powerful Tools sed Stream Editor awk Alfred Aho, Peter Weinberger, and Brian Kernighan

The ‘sed’ Command/Language Filter Like grep, sort, or uniq, it takes input and performs some operation on it to filter the output Usage: sed ‘Address Command’ Address specifies where to make changes Command specifies what change to make Example: sed ‘4d’ textFile

Address Specification Addresses could be line numbers or regular expressions No address – each line One address – only that line Two comma separated addresses – All lines in between ! – All other lines

Commands Available To sed a\ Append text c\ Replace text i\ Insert text before d Delete lines s Make substitutions

Examples

More Examples

Substitution Example Same syntax as vi

When Would You Want To Use sed? sed works on streams, so it is perfect to be placed in the middle of a pipe in order to change the output from one format to another Example: If a program always prints out 4 lines of junk for every good line, sed can be used to weed out the junk

Example

awk Answers the question: What do I do if I want to search for a pattern and actually use it? Combination of grep and commands Searches for some pattern or condition and performs some command on it Complete programming language Looks a lot like C syntactically Variables are declared bash style

Pattern And Command awk in its most basic form simply executes a command on all lines that match (or adhere to) a certain pattern Usage: awk ‘Pattern { Command }’ FILE Just like sed, if there is no pattern, then every line will be matched

Example

Different Ways To Run Awk awk ‘Pattern { Command }’ awk –f awkFile inputFile Since awk itself can be a complex language, you can store all the commands in a file and run it with the –f flag

Important awk Concepts Record Every line of an input file is a record The current record can be referenced with $0 Field Every word in a record is called a field Each field is numbered and can be referred to $1 is the first record, $2 is the second, etc.

Special Predefined awk Variables RS The character that acts as a record separator Default is the end of a line FS The character that acts as a field separator Default is whitespace (space, tab, etc) Can be redefined with the –F flag

Example

Other awk Variables NF = number of fields in the current record NR = Total number of records seen so far OFS = Output field separator ORS = Output record separator

BEGIN And END Blocks Two special patterns that can be matched BEGIN Commands are executed before any records are looked at END Commands are executed after all records are processed

Example

Awk Patterns /regular expression/ -> same as egrep Relational expression >, =, <=, == Pattern && pattern Pattern || pattern Pattern1 ? Pattern2 : pattern3 If Pattern1 is True, then Pattern2, else pattern 3 (pattern) ! Pattern Pattern1, pattern2

Example Patterns

Awk Actions Enclosed in { } () Grouping $ Field reference Increment, decrement ^ Exponentiation + - ! Plus, minus, not * / % Multiplication, division, and modulus

Control Flow Statements Inside of commands, you can have control flow if while for

If Syntax if (condition) { Statements } else { Statements }

While Syntax while (Condition) { Statements }

For Syntax for (Declaration ; Condition ; Increment ) { Statements } for ( j=0; j < 5; j++) { print “hello world” }

When Would You Want To Use awk? Whenever you want to search for some pattern and perform some action Example: I want to go through and calculate the average score on the Midterm

Example

Another Example Adding 12 points to everyone’s midterm score

Putting Them Together – awk and sed

awk Versus bash $ arguments Always enclose everything to awk in single quotes so they don’t get interpreted $1 to awk means something completely different than $1 to bash $1 in awk means first field $1 in bash means first command line argument

Next Time Looking at some of the string and mathematical awk functionality find Putting everything together The complete bash programming necessities Quiz 2 – Next Tuesday