1 P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Awk Programming Ruibin Bai (Room AB326) Division of Computer Science The University.

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

Lecture 7.2  awk. History of AWK The name AWK – Initials of designers: Alfred V. Alo, Peter J. Weinberger, and Brian W. Kernighan. Appear 1977, stable.
1 Unix Talk #2 AWK overview Patterns and actions Records and fields Print vs. printf.
ISBN Regular expressions Mastering Regular Expressions by Jeffrey E. F. Friedl –(on reserve.
2000 Copyrights, Danielle S. Lahmani UNIX Tools G , Fall 2000 Danielle S. Lahmani Lecture 6.
A Guide to Unix Using Linux Fourth Edition
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
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
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
Introduction to Shell Script Programming
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.
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.
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.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
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”
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.
P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Exam Revision Ruibin Bai (Room AB326) Division of Computer Science The University of Nottingham.
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.
13 More Advanced Awk Mauro Jaskelioff (originally by Gail Hopkins)
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}
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.
1 P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Awk Programming (2) Ruibin Bai (Room AB326) Division of Computer Science The University.
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.
1 P51UST: Unix and Software Tools Unix and Software Tools (P51UST) More Shell Programming Ruibin Bai (Room AB326) Division of Computer Science The University.
Chapter Six Introduction to Shell Script Programming.
© 2006 KDnuggets [16/Nov/2005:16:32: ] "GET /jobs/ HTTP/1.1" "
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
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
P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Awk Programming (3) Ruibin Bai (Room AB326) Division of Computer Science The University.
Alon Efrat Computer Science Department University of Arizona Unix Tools.
CISC 1480/KRF Copyright © 1999 by Kenneth R. Frazer 1 AWK q A programming language for handling common data manipulation tasks with only a few lines of.
Review of Awk Principles
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.
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
By Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
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.
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.
Arun Vishwanathan Nevis Networks Pvt. Ltd.
Lesson 5-Exploring Utilities
CSC 4630 Meeting 7 February 7, 2007.
PROGRAMMING THE BASH SHELL PART IV by İlker Korkmaz and Kaya Oğuz
CS 403: Programming Languages
What is Bash Shell Scripting?
John Carelli, Instructor Kutztown University
Guide To UNIX Using Linux Third Edition
Unix Talk #2 (sed).
Programming Languages
Awk.
Presentation transcript:

1 P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Awk Programming Ruibin Bai (Room AB326) Division of Computer Science The University of Nottingham Ningbo, China

What is awk? A pattern matching program for processing texts, initially implemented in The name AWK is derived from family names of its authors: Alfred Aho, Peter Weinberger and Brain Kerinighan There are different versions of awk: –awk - the original version, sometimes called old awk, or oawk –New awk - additional features added in Often called nawk –GNU awk (gawk)- has even more features

What does awk do? A text file is thought of as being made up of records and fields On this file you can: –Do arithmetic and string operations –Use loops and conditionals (if-then-else) –Produce formatted reports

What does awk do? (2) awk (new awk) also allows you to: –Execute UNIX commands from within a script –Process the output from UNIX commands –Work with multiple input streams –Define functions

What does awk do? (3) awk can also be combined with shell scripting! –Shell is very easy and quick to write, but it lacks functionality. –awk and shell are designed to be integrated Simply invoke the awk interpreter from within the shell script, rather than from the command line!

Awk Syntax The awk command has the following syntax awk [-F field_sep] ‘program’ target-files(s) Or awk [-F field_sep] –f program.file target-file(s) 6 P51UST: Unix and Software Tools

Awk Syntax awk [-F field_sep] ‘program’ target-files(s) The program is one or more awk programming commands, typed in at command-line, enclosed by single quotes. target-files is one or more of the input files the command is to process. Option –F : allows you to change awk’s field separator. Default field separator is white space (one or more spaces or tabs). 7 P51UST: Unix and Software Tools

Awk Syntax awk [-F field_sep] –f program.file target-file(s) Option –f specifies that the filename that follows contains the awk programming commands. Awk takes its instructions from that file rather than from the command line. target-files is one or more of the input files the command is to process. Using –f option is preferred –More efficient to debug, modify and enhance your awk programming commands. –You could use this awk script again in future –Easier to manage if the program grown over time. 8 P51UST: Unix and Software Tools

Example – First Taste of awk When type in the following command in a terminal $ cat fingr.txt zliyccj Cai Jiangliang pts/4 1d Apr 8 10:51 zliyccj Cai Jiangliang *pts/17 15:12 Apr 8 19:11 zliyccj2 Chen Jianjun *pts/6 13:48 Apr 8 21:29 zliyccj2 Chen Jianjun pts/30 14:47 Apr 8 20:31 zliyccl Cao Lizhou pts/98 20:09 Apr 8 15:08 zliychj2 He Jiansen pts/19 17:04 Apr 8 18:14 zliychl Huang Lun *pts/26 14:23 Apr 8 18:04... Task: extract the username field? 9 P51UST: Unix and Software Tools

Example – First Taste of awk Field separator: white space and tabs, default Target-file: fingr.txt Commands: awk ‘{print $1}’ fingr.txt Or awk -F “ ” '{print $1}' fingr.txt 10 P51UST: Unix and Software Tools

Awk Variables Some common awk variables 11 P51UST: Unix and Software Tools variablesContents FSField separator, usually white space OFSOutput field separator, usually white space RSRecord separator, default “\n” NRNumber of the current lines NFThe number of fields, based on FS $1-$(NF)Contains strings from the first field to the last field $0Whole current line FILENAMEThe name of the current target file ARGVContains an array of the command-line arguments (not supported by awk) ARGCThe number of command-line arguments (not supported by awk).

Awk Program Format General Format pattern {action} –A pattern selects which of the lines from the file or files, if any, are acted upon. –Patterns can be relational expressions or regular expressions, or others. –An action is defined as one or more awk commands enclosed in a pair of curly brackes ‘{ }’. 12 P51UST: Unix and Software Tools

Awk Program Format Program 1 Program 2 –The action associated with a particular pattern must begin on the same line as the pattern with which it is associated. –Program 1 and Program 2 performs very differently. 13 P51UST: Unix and Software Tools pattern { action 1 action 2 } pattern { action 1 action 2 }

Awk Program Format Patterns without associated actions will print the lines that are matched, while actions without patterns will be applied on every line. For program 1, the actions will be performed only on lines that match the pattern. In program 2, each line that matches the pattern would be displayed and the actions would be performed on every line. 14 P51UST: Unix and Software Tools pattern { action 1 action 2 } pattern { action 1 action 2 }

Patterns (1) Relational expressions $1 < 4 {action} $1 <= 4 {action} $1 == “bac” {action} ($1 $1) || $4 != “abc” {action} The tilde (~) $1 ~ /^z/ {action} Note: Regular expression must be enclosed in forward slashes ‘/’ 15 P51UST: Unix and Software Tools SymbolMeaning < (<=)Less than (Less than or equal to) > (>=)Greater than (Greater than or equal) !=Not equal to ==Equal ~Contains (or matches) the regular expression !~Does not contain (or match) the regular expression &&Logical AND ||Logical OR

Example (($1 $1) && $4 !=“frog”) || $6 >0 Data: toad frog frog salamander fish salamander cricket fish toad spider wasp bee 1 Which of these lines do you predict will match? 16 P51UST: Unix and Software Tools

Patterns (2) Regular expressions –You should know this already –A regular expression should be enclosed in forward slashes awk ‘/6$/ {print $0}’ fingr.txt 17 P51UST: Unix and Software Tools

Patterns (2) Regular expressions –You should know this already –A regular expression should be enclosed in forward slashes awk ‘/6$/ {print $0}’ fingr.txt 18 P51UST: Unix and Software Tools

Pattern (3) Special Patterns: BEGIN, END BEGIN: –Is the first pattern in an awk script –The associated actions will be performed before the target file is opened. –The open curly bracket must appear on the same line as the BEGIN –A good place to print out headers, initilise counters, and set the field separator for the target file. –Do not try to manipulate target files in the associated actions. 19 P51UST: Unix and Software Tools

Pattern -BEGIN BEGIN { FS=“:” OFS=“ ” count=0 print (“This is a cool heading”) } 20 P51UST: Unix and Software Tools

Pattern (4) Special Patterns: END –Is the last pattern in an awk script –The associated actions will be performed after the target file is successfully closed. –The open curly bracket must appear on the same line as the END –Useful for putting trailers on files, summing up the data presented, and doing other end-of-file housekeeping tasks. 21 P51UST: Unix and Software Tools

Patterns – Putting Things Together BEGIN { print ("Here come the lines from the input file:") print " " } {print $0} END { print " " print "There were "NR" lines in the file "FILENAME"." } 22 P51UST: Unix and Software Tools

Examples BEGIN { FS="\n" RS="" # empty line as a record separator } { print $0 } END{ print "total number of fields is " NF } 23 P51UST: Unix and Software Tools

Next Lecture Awk commands Loops and conditionals Arrays Functions 24 P51UST: Unix and Software Tools