CSCI 330 UNIX and Network Programming

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

CSCI 330 The UNIX System Awk The Bash Shell
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.
Enumerated data type & typedef. Enumerated Data Type An enumeration consists of a set of named integer constants. An enumeration type declaration gives.
ENUMERATED, typedef. ENUMERATED DATA TYPES An enumeration consists of a set of named integer constants. An enumeration type declaration gives the name.
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.
Lecture 5 sed and awk. Last week Regular Expressions –grep (BRE) –egrep (ERE) Sed - Part I.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Lecture 5 sed and awk. Last week Regular Expressions –grep –egrep.
AWK: The Duct Tape of Computer Science Research Tim Sherwood UC San Diego.
Guide To UNIX Using Linux Third Edition
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Sed and awk.
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
Agenda Sed Utility - Advanced –Using Script-files / Example Awk Utility - Advanced –Using Script-files –Math calculations / Operators / Functions –Floating.
A Guide to Unix Using Linux Fourth Edition
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
AWK. text processing languge awk Created for Unix by Aho, Weinberger and Kernighan Basicly an: ▫interpreted ▫text processing ▫programming language Updated.
CS 403: Programming Languages Fall 2004 Department of Computer Science University of Alabama Joel Jones.
Programming Languages Meeting 13 December 2/3, 2014.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
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
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}
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)
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.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
© 2006 KDnuggets [16/Nov/2005:16:32: ] "GET /jobs/ HTTP/1.1" "
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.
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.
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.
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.
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.
Awk Programmable Filters 1.
Arun Vishwanathan Nevis Networks Pvt. Ltd.
AWK.
CSC 4630 Meeting 7 February 7, 2007.
Jan 2016 Solar Lunar Data.
BASIC ELEMENTS OF A COMPUTER PROGRAM
PROGRAMMING THE BASH SHELL PART IV by İlker Korkmaz and Kaya Oğuz
John Carelli, Instructor Kutztown University
Average Monthly Temperature and Rainfall
Gantt Chart Enter Year Here Activities Jan Feb Mar Apr May Jun Jul Aug
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Sed and awk.
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Awk.
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Presentation transcript:

CSCI 330 UNIX and Network Programming The Bash Shell CSCI 330 UNIX and Network Programming Unit IIX: awk, Part I Copyright Department of Computer Science, Northern Illinois University, 2005

What is awk? created by: Aho, Weinberger and Kernighan The AWK/NAWK Utility CSCI 330 - The UNIX System What is awk? created by: Aho, Weinberger and Kernighan scripting language used for manipulating data and generating reports versions of awk: awk, nawk, mawk, pgawk, … GNU awk: gawk Copyright Department of Computer Science, Northern Illinois University, 2004

What can you do with awk? awk operation: Useful for: CSCI 330 - The UNIX System What can you do with awk? awk operation: reads 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

Basic awk invocation awk 'script' file(s) awk –f scriptfile file(s) The AWK/NAWK Utility CSCI 330 - The UNIX System Basic awk invocation awk 'script' file(s) awk –f scriptfile file(s) common option: -F to change field separator Copyright Department of Computer Science, Northern Illinois University, 2004

Basic awk script consists of patterns & actions: pattern {action} The AWK/NAWK Utility CSCI 330 - The UNIX System Basic awk script consists of patterns & actions: pattern {action} if pattern is missing, action is applied to all lines if action is missing, the matched line is printed must have either pattern or action Example: awk '/for/ { print }' testfile prints all lines containing string “for” in testfile Copyright Department of Computer Science, Northern Illinois University, 2004

awk variables awk reads input line into buffers: record and fields CSCI 330 - The UNIX System awk variables awk reads input line into buffers: record and fields field buffer: one for each field in the current record variable names: $1, $2, … record buffer: $0 holds the entire record

CSCI 330 - The UNIX System More awk variables NR Number of the current record NF Number of fields in current record also: FS Field separator (default=whitespace)

Example: Records and Fields The AWK/NAWK Utility CSCI 330 - The UNIX System Example: Records and Fields % cat emps Tom Jones 4424 5/12/66 543354 Mary Adams 5346 11/4/63 28765 Sally Chang 1654 7/22/54 650000 Billy Black 1683 9/23/44 336500 % awk '/Tom/ { print }' emps Tom Jones 4424 5/12/66 543354 Copyright Department of Computer Science, Northern Illinois University, 2004

Example: Records and Fields The AWK/NAWK Utility CSCI 330 - The UNIX System Example: Records and Fields % cat emps Tom Jones 4424 5/12/66 543354 Mary Adams 5346 11/4/63 28765 Sally Chang 1654 7/22/54 650000 Billy Black 1683 9/23/44 336500 % awk '{print NR, $0}' emps 1 Tom Jones 4424 5/12/66 543354 2 Mary Adams 5346 11/4/63 28765 3 Sally Chang 1654 7/22/54 650000 4 Billy Black 1683 9/23/44 336500 Copyright Department of Computer Science, Northern Illinois University, 2004

Example: Space as Field Separator The AWK/NAWK Utility CSCI 330 - The UNIX System Example: Space as Field Separator % cat emps Tom Jones 4424 5/12/66 543354 Mary Adams 5346 11/4/63 28765 Sally Chang 1654 7/22/54 650000 Billy Black 1683 9/23/44 336500 % awk '{print NR, $1, $2, $5}' emps 1 Tom Jones 543354 2 Mary Adams 28765 3 Sally Chang 650000 4 Billy Black 336500 Copyright Department of Computer Science, Northern Illinois University, 2004

Example: Colon as Field Separator The AWK/NAWK Utility CSCI 330 - The UNIX System Example: Colon as Field Separator % cat emps2 Tom Jones:4424:5/12/66:543354 Mary Adams:5346:11/4/63:28765 Sally Chang:1654:7/22/54:650000 Billy Black:1683:9/23/44:336500 % awk -F: '/Jones/{print $1, $2}' emps2 Tom Jones 4424 Copyright Department of Computer Science, Northern Illinois University, 2004

Special Patterns BEGIN END matches before the first line of input CSCI 330 - The UNIX System Special Patterns BEGIN matches before the first line of input used to create header for report END matches after the last line of input used to create footer for report

CSCI 330 - The UNIX System example input file Jan 13 25 15 115 Feb 15 32 24 22 Mar 15 24 34 228 Apr 31 52 63 420 May 16 34 29 208 Jun 31 42 75 492 Jul 24 34 67 436 Aug 15 34 47 316 Sep 13 55 37 277 Oct 29 54 68 525 Nov 20 87 82 577 Dec 17 35 61 401 Jan 21 36 64 620 Feb 26 58 80 652 Mar 24 75 70 495 Apr 21 70 74 514

awk example runs awk '{print $1}' input CSCI 330 - The UNIX System awk example runs awk '{print $1}' input awk '$1 ~ /Feb/ {print $1}' input awk '{print $1, $2+$3+$4, $5}' input awk 'NF == 5 {print $1, $2+$3+$4, $5}' input

awk example script BEGIN { print "January Sales Revenue" } CSCI 330 - The UNIX System awk example script BEGIN { print "January Sales Revenue" } $1 ~ /Jan/ { print $1, $2+$3+$4, $5 END { print NR, " records processed"

Categories of Patterns CSCI 330 – UNIX and Network Programming Categories of Patterns simple patterns BEGIN, END expression patterns: whole line vs. explicit field match whole line /regExp/ field match $2 ~ /regExp range patterns specified as from and to: example: /regExp/,/regExp/

awk actions basic expressions output: print, printf decisions: if CSCI 330 – UNIX and Network Programming awk actions basic expressions output: print, printf decisions: if loops: for, while

awk Expression consists of: operands and operators operands: CSCI 330 – UNIX and Network Programming awk Expression consists of: operands and operators operands: numeric and string constants variables functions and regular expression operators: assignment: = ++ -- += -= *= /= arithmetic: + - * / % ^ logical: && || ! relational: > < >= <= == != match: ~ !~ string concatenation: space

awk Variables created via assignment: var = expression CSCI 330 – UNIX and Network Programming awk Variables created via assignment: var = expression types: number (not limited to integer) string, array variables come into existence when first used type of variable depends on its use variables are initialized to either 0 or “”

CSCI 330 – UNIX and Network Programming awk variables example BEGIN { print "January Sales Revenue" count = 0 sum = 0 } $1 ~ /Jan/ && NF == 5 { print $1, $2+$3+$4, $5 count++ sum += $5 END { print count, " records produce: ", sum

awk output: print Writes to standard output CSCI 330 – UNIX and Network Programming awk output: print Writes to standard output Output is terminated by newline If called with no parameter, it will print $0 Printed parameters are separated by blank Print control characters are allowed: \n \f \a \t \b \\ …

The AWK/NAWK Utility CSCI 330 – UNIX and Network Programming print examples % awk '{print $1, $2}' grades john 85 andrea 89 jasper 84 % awk '{print $1 "," $2}' grades john,85 andrea,89 jasper,84 Copyright Department of Computer Science, Northern Illinois University, 2004

printf: Formatting output CSCI 330 – UNIX and Network Programming printf: Formatting output Syntax: printf(format-string, var1, var2, …) each format specifier within “format-string” requires additional argument of matching type %d, %i decimal integer %c single character %s string of characters %f floating point number

Format specifier modifiers CSCI 330 – UNIX and Network Programming Format specifier modifiers between “%” and letter %10s %7d %10.4f %-20s meaning: width of field, field is printed right justified (“-” will left justify) precision: number of digits after decimal point

awk Example: list of products CSCI 330 – UNIX and Network Programming awk Example: list of products 101:propeller:104.99 102:trailer hitch:97.95 103:sway bar:49.99 104:fishing line:0.99 105:mirror:4.99 106:cup holder:2.49 107:cooler:14.89 108:wheel:49.99 109:transom:199.00 110:pulley:9.88 111:lock:31.00 112:boat cover:120.00 113:premium fish bait:1.00

CSCI 330 – UNIX and Network Programming awk Example: output Marine Parts R Us Main catalog Part-id name price ====================================== 101 propeller 104.99 102 trailer hitch 97.95 103 sway bar 49.99 104 fishing line 0.99 105 mirror 4.99 106 cup holder 2.49 107 cooler 14.89 108 wheel 49.99 109 transom 199.00 110 pulley 9.88 111 lock 31.00 112 boat cover 120.00 113 premium fish bait 1.00 Catalog has 13 parts

CSCI 330 – UNIX and Network Programming awk Example: complete BEGIN { FS= ":" print "Marine Parts R Us" print "Main catalog" print "Part-id\tname\t\t\t price" print "==================================" } { printf("%3d\t%-20s\t%6.2f\n", $1, $2, $3) END { print "Catalog has", NR, "parts"

Summary next: more awk arrays control structures CSCI 330 – UNIX and Network Programming Summary next: more awk arrays control structures