CSCI 330 UNIX and Network Programming Unit IX: awk II.

Slides:



Advertisements
Similar presentations
1 Unix Talk #2 AWK overview Patterns and actions Records and fields Print vs. printf.
Advertisements

CSC 4630 Meeting 9 February 14, 2007 Valentine’s Day; Snow Day.
Lecture 5 sed and awk. Last week Regular Expressions –grep (BRE) –egrep (ERE) Sed - Part I.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Chapter 5: Control Structures II (Repetition)
Chapter 5: Control Structures II (Repetition)
Lecture 5 sed and awk. Last week Regular Expressions –grep –egrep.
AWK: The Duct Tape of Computer Science Research Tim Sherwood UC San Diego.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Sed and awk.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Agenda Sed Utility - Advanced –Using Script-files / Example Awk Utility - Advanced –Using Script-files –Math calculations / Operators / Functions –Floating.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
AWK. text processing languge awk Created for Unix by Aho, Weinberger and Kernighan Basicly an: ▫interpreted ▫text processing ▫programming language Updated.
C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat.
1 awk awk is a file-processing programming language. Makes it easy to perform text manipulation tasks. Is used in –Generating reports –Matching patterns.
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”
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
Sed, awk, & perl CS 2204 Class meeting 13 *Notes by Mir Farooq Ali and other members of the CS faculty at Virginia Tech. Copyright 2003.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
1. Agenda for loop Short-handed notation How to use break and continue 2.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
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}
1 P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Awk Programming (2) Ruibin Bai (Room AB326) Division of Computer Science The University.
Time to talk about your class projects!. Shell Scripting Awk (lecture 2)
5 1 Data Files CGI/Perl Programming By Diane Zak.
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Chapter Twelve sed, awk & perl1 System Programming sed, awk & perl.
1 LAB 4 Working with Trace Files using AWK. 2 Structure of Trace File.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
16-Dec-15Advanced Programming Spring 2002 sed and awk Henning Schulzrinne Dept. of Computer Science Columbia University.
CSCI 330 UNIX and Network Programming
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
1 P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Awk Programming Ruibin Bai (Room AB326) Division of Computer Science The University.
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,
1 Lecture 10 Introduction to AWK COP 3344 Introduction to UNIX.
CSCI 330 UNIX and Network Programming Unit X: Shell Scripts II.
Sed and awk CS 2204 Class meeting 13. © Mir Farooq Ali, sed Stream editor Originally derived from ed line editor Used primarily for non interactive.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
JavaScript, Sixth Edition
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.
Introduction to programming in java Lecture 21 Arrays – Part 1.
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.
Lecture 14 Programming with awk II
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Control Structures II (Repetition)
Chapter 5: Control Structures II
John Carelli, Instructor Kutztown University
- Additional C Statements
Dale Roberts, Lecturer IUPUI
Presentation transcript:

CSCI 330 UNIX and Network Programming Unit IX: awk II

What can you do with awk? awk operation: scans a file line by line splits each input line into fields compares input line/fields to pattern performs action(s) on matched lines Useful for: transform data files produce formatted reports Programming constructs: format output lines arithmetic and string operations conditionals and loops 2CSCI 330 – UNIX and Network Programming

Typical awk script divided into three major parts: comment lines start with # 3CSCI 330 – UNIX and Network Programming

awk Array awk allows one-dimensional arrays index can be number or string elements can be string or number array need not be declared its size its element type array elements are created when first used initialized to 0 or “” 4CSCI 330 – UNIX and Network Programming

Arrays in awk Syntax: arrayName[index] = value Examples: list[1] = "some value" list[2] = 123 list["other"] = "oh my !" 5CSCI 330 – UNIX and Network Programming

Illustration: Associative Arrays awk array allows string as index Age["Robert"] = 46 Age["George"] = 22 Age["Juan"] = 22 Age["Nhan"] = 19 Age["Jonie"] = 34 6CSCI 330 – UNIX and Network Programming

Example: process sales data input file: 1 clothing computers textbooks clothing computers supplies textbooks desired output: summary of department sales 7CSCI 330 – UNIX and Network Programming

Illustration: process each input line 8 deptSales array CSCI 330 – UNIX and Network Programming

Illustration: process each input line 9CSCI 330 – UNIX and Network Programming

Summary: awk program 10CSCI 330 – UNIX and Network Programming

Example: complete program { deptSales[$2] += $3 } END { for (x in deptSales) print x, deptSales[x] } 11CSCI 330 – UNIX and Network Programming

awk built-in functions arithmetic ex.: sqrt, rand string ex.: index, length, split, substr sprintf, tolower, toupper misc. ex.: system, systime 12CSCI 330 – UNIX and Network Programming

awk built-in split function split(string, array, fieldsep) divides string into pieces separated by fieldsep stores the pieces in array if fieldsep is omitted, the value of FS is used Example: split("26:Miller:Comedian", fields, ":") sets the contents of the array fields as follows: fields[1] = "26" fields[2] = "Miller" fields[3] = "Comedian" 13CSCI 330 – UNIX and Network Programming

awk control structures Conditional if-else Repetition for with counter with array index while also: break, continue 14CSCI 330 – UNIX and Network Programming

if Statement Syntax: if (conditional expression) statement-1 else statement-2 Example: if ( NR < 3 ) print $2 else print $3 Use compound { } for more than one statement: { … } 15CSCI 330 – UNIX and Network Programming

if Statement for arrays Syntax: if (value in array) statement-1 else statement-2 Example: if ("clothing" in deptSales) print deptSales["clothing"] else print "not found" 16CSCI 330 – UNIX and Network Programming

for Loop Syntax: for (initialization; limit-test; update) statement Example: for (i=1; i <= 10; i++) print "The square of ", i, " is ", i*i 17CSCI 330 – UNIX and Network Programming

for Loop for arrays Syntax: for (var in array) statement Example: for (x in deptSales) { print x print deptSales[x] } 18CSCI 330 – UNIX and Network Programming

while Loop Syntax: while (logical expression) statement Example: i=1 while (i <= 10) { print "The square of ", i, " is ", i*i i = i+1 } 19CSCI 330 – UNIX and Network Programming

loop control statements break exits loop continue skips rest of current iteration, continues with next iteration 20CSCI 330 – UNIX and Network Programming

Example: sensor data 1 Temperature 2 Rainfall 3 Snowfall 4 Windspeed 5 Winddirection also: sensor readings Plan: print report with average readings per sensor 21CSCI 330 – UNIX and Network Programming

Example: sensor readings /1/ /2/ /3/ /4/ /5/ /1/ /4/ /5/ /1/ /1/ /1/32 22CSCI 330 – UNIX and Network Programming

Report: average readings Sensor Average Windspeed Winddirection Temperature Rainfall 6.00 Snowfall CSCI 330 – UNIX and Network Programming

Step 1: print sensor data BEGIN { printf("id\tSensor\n") printf(" \n") } { printf("%d\t%s\n", $1, $2) } 24CSCI 330 – UNIX and Network Programming

Step 2: print sensor readings BEGIN { FS="/" printf(" Date Value\n") printf(" \n") } { printf("%s %7.2f\n", $1, $3) } 25CSCI 330 – UNIX and Network Programming

Step 3: print sensor summary BEGIN { FS="/" } { sum[$2] += $3 count[$2]++ } END { for (i in sum) printf("%d %7.2f\n", i, sum[i]/count[i]) } 26CSCI 330 – UNIX and Network Programming

Next steps: Remaining tasks awk -f sense.awk sensors readings Sensor Average Windspeed Winddirection Temperature Rainfall 6.00 Snowfall sensor names 2 input files CSCI 330 – UNIX and Network Programming

Example: print sensor averages Remaining tasks: recognize nature of input data use: number of fields in record substitute sensor id with sensor name use: array of sensor names 28CSCI 330 – UNIX and Network Programming

Example: sense.awk BEGIN { print " Sensor Average" print " " } NF > 1 { name[$1] = $2 } NF < 2 { split($0,fields,"/") sum[fields[2]] += fields[3] count[fields[2]]++ } END { for (i in sum) printf("%15s %7.2f\n", name[i], sum[i]/count[i]) } 29CSCI 330 – UNIX and Network Programming

Summary awk similar in operation to sed transform input lines to output lines powerful report generator 30CSCI 330 – UNIX and Network Programming