John Carelli, Instructor Kutztown 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

1 Unix Talk #2 AWK overview Patterns and actions Records and fields Print vs. printf.
CS1061 C Programming Lecture 16: Formatted I/0 A. O’Riordan, 2004.
2000 Copyrights, Danielle S. Lahmani UNIX Tools G , Fall 2000 Danielle S. Lahmani Lecture 6.
Primitive Variable types Basic types –char (single character or ASCII integer value) –int (integer) –short (not longer than int) –long (longer than int)
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
AWK: The Duct Tape of Computer Science Research Tim Sherwood UC San Diego.
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
 Pearson Education, Inc. All rights reserved Formatted Output.
1 2 2 Introduction to Java Applications Introduction Java application programming –Display messages –Obtain information from the user –Arithmetic.
 2005 Pearson Education, Inc. All rights reserved Formatted Output.
Agenda Sed Utility - Advanced –Using Script-files / Example Awk Utility - Advanced –Using Script-files –Math calculations / Operators / Functions –Floating.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
AWK. text processing languge awk Created for Unix by Aho, Weinberger and Kernighan Basicly an: ▫interpreted ▫text processing ▫programming language Updated.
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”
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
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)
Revision Lecture Mauro Jaskelioff. AWK Program Structure AWK programs consists of patterns and procedures Pattern_1 { Procedure_1} Pattern_2 { Procedure_2}
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
1 LAB 4 Working with Trace Files using AWK. 2 Structure of Trace File.
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
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
1 P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Awk Programming 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.
CSCI 330 UNIX and Network Programming Unit IX: awk II.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
1 © 2001 John Urrutia. All rights reserved. CIS52 – File Manipulation File Manipulation Utilities Regular Expressions sed, awk.
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.
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
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
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.
28 Formatted Output.
Arun Vishwanathan Nevis Networks Pvt. Ltd.
awk- An advanced Filter
AWK.
CSC 4630 Meeting 7 February 7, 2007.
Lecture 14 Programming with awk II
Formatted Input and Output
Topics Designing a Program Input, Processing, and Output
ECE Application Programming
Objectives You should be able to describe: Interactive Keyboard Input
TMF1414 Introduction to Programming
PROGRAMMING THE BASH SHELL PART IV by İlker Korkmaz and Kaya Oğuz
Java Programming: From Problem Analysis to Program Design, 4e
OUTPUT STATEMENTS GC 201.
C Formatted Input / Output Review and Lab Assignments
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Topics Designing a Program Input, Processing, and Output
Introduction to Java Applications
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Introduction to C EECS May 2019.
Awk.
awk- An Advanced Filter
Introduction to C Programming
Log Analysis with GAWK Back to Basics.
Presentation transcript:

John Carelli, Instructor Kutztown University carelli@kutztown.edu

Awk (Programming/Scripting Language) Features: programmable shares many constructs with “C” … but has a number of extensions (ex: Associative Arrays) interpreted data-driven designed for ease of use Bell Labs (Aho, Weinberger, Kernighan) Versions: awk gawk nawk

awk [options] [program] [file-list] Syntax: awk [options] [program] [file-list] awk [options] –f program-file [file-list] Input text Records Fields awk program pattern { action }

Awk – default operation Input processed one line at a time Each line is a record built-in variable $0 Each record is automatically parsed Broken into tokens Each token is a field built-in variables $1 to $n where n is the number of fields default field delimiter is whitespace (\s)

Awk – basic program An awk program consist of pattern/action lines syntax: pattern { action } patterns Match using RegEx based commands on the current line actions If matched, perform some operation on the line

BEGIN END Expression  ~  !~

if (conditional) then action [ else action] while (conditional) action for (counter; conditional; increment) action for (variable in array) action do action while (conditional) break continue variable = expression print [list of expressions] [> expression] printf format [,list of expressions] [> expression] -> a comma will print a space next exit {list of statements}

FILENAME – name of the input file NF – number of fields in the current line NR – line number of the input stream $0 – the entire line $1 – first field of the current line $2 – second field of the current line $3, $4, etc. – rest of fields FNR – record number in current file FS – input field separator OFS - output field separator

Awk commands (no single quotes) awk –f # Comments

Statements Expression String literals Assignment operator: = Logical operators: || && ! Matching operators: ~ !~ Relational operators: < <= Arithmetic operators: + - * = = != > >= / % ^ Increment and decrement operators: ++ -- Parenthesis String literals

int(num) Returns integer portion of num exp(num) log(num) sqrt(num) length(str) Length of the string substr(str,pos,l en) Returns substring of str that begins at pos for len characters index(str1, str2) Returns index of str2 in str1 or 0 if str2 is not present in str1 split(str, arr, del) Places elements of str, delimited by del, in the array arr; Returns the number of elements in the array arr printf and sprint Similar to C tolower / toupper

printf formatString, var1, var2, …. C-style output formatting - (before C++ stream objects) printf formatString, var1, var2, …. formatString formats listed variables in the order given conversion specifiers get embedded in the formatString conversion specifiers begin with %

printf – conversion specifiers Basic Syntax: % flags width [ . precision ] type if flag is a minus sign, left justify width is the character width of the output padded, if necessary precision (for floating point values) basic types: d for integers s for strings f (fixed) or e (scientific) for floats

Defintion array[string-key] = value; for (<elem> in <array>) <action>;

if / if-else while for break continue

Print each registrant, the conference and the amount due from conf Print each registrant, the conference and the amount due from conf.data Print company, name and conference for all registrants that don’t owe money Print name and amount due for registrants attending the Coatings conference and that owe money Previous example but print headings Print the number of unique conferences being held

Write a program that calculates the average amount paid by all participants in the conf.data file. In addition, calculate the average amount owed by each participant, and print these values. Write a program that calculates the amount that was paid by each registrant. For each registrant, print out the name of the registrant, the amount paid, and the average amount paid by all registrants. Write a program that will print the number of registrants registered for the Adhesions conference, Coatings conference and Resins conference

Write a program using the conf.data file that does the following: Calculate and print the number of people registered for conferences who come from Digital Calculate and print the total amount owed for people from Digital Calculate the average amount owed for registrants from Digital