CSC 4630 Meeting 7 February 7, 2007.

Slides:



Advertisements
Similar presentations
Tcl and Otcl Tutorial Part I Internet Computing KUT Youn-Hee Han.
Advertisements

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.
CS 898N – Advanced World Wide Web Technologies Lecture 8: PERL Chin-Chih Chang
CSC 4630 Meeting 9 February 14, 2007 Valentine’s Day; Snow Day.
LING 388: Language and Computers Sandiway Fong Lecture 3: 8/28.
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
JavaScript, Third Edition
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
2440: 211 Interactive Web Programming Expressions & Operators.
Programming I Introduction Introduction The only way to learn a new programming language is by writing programs in it. The first program to.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
Input, Output, and Processing
Programming Languages Meeting 13 December 2/3, 2014.
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”
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.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
13 More Advanced Awk Mauro Jaskelioff (originally by Gail Hopkins)
Chapter 05 (Part III) Control Statements: Part II.
JLex Lecture 4 Mon, Jan 24, JLex JLex is a lexical analyzer generator in Java. It is based on the well-known lex, which is a lexical analyzer generator.
Revision Lecture Mauro Jaskelioff. AWK Program Structure AWK programs consists of patterns and procedures Pattern_1 { Procedure_1} Pattern_2 { Procedure_2}
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.
Time to talk about your class projects!. Shell Scripting Awk (lecture 2)
CSCI 125 & 161 / ENGR 144 Lecture 8 Martin van Bommel.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
1 LAB 4 Working with Trace Files using AWK. 2 Structure of Trace File.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
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
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.
Basic Scripting & Variables Yasar Hussain Malik - NISTE.
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
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
Chapter 16 Introduction to Perl Perl (Practical Extraction and Report Language) Developed by Larry Wall as a Unix scripting language. Popular server-side.
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.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Arun Vishwanathan Nevis Networks Pvt. Ltd.
Topics Designing a Program Input, Processing, and Output
Shell Scripting March 1st, 2004 Class Meeting 7.
Lecture 9 Shell Programming – Command substitution
JavaScript Syntax and Semantics
JLex Lecture 4 Mon, Jan 26, 2004.
John Carelli, Instructor Kutztown University
Arithmetic operations, decisions and looping
Topics Introduction to File Input and Output
A First Book of ANSI C Fourth Edition
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
Programming Languages
C Programming Getting started Variables Basic C operators Conditionals
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Lexical Elements & Operators
Flow of Control.
Introduction to Bash Programming, part 3
Topics Introduction to File Input and Output
Loops CGS3416 Spring 2019 Lecture 7.
Class code for pythonroom.com cchsp2cs
Presentation transcript:

CSC 4630 Meeting 7 February 7, 2007

More Scripting Languages awk, named for Aho, Weinberger, Kernighan Script is embedded in a nested looping control structure: for each pattern {action} do for each input file line do if pattern matches line then action

awk Programs Generally a sequence of pattern {action} statements If {action} is missing, matched lines are printed (meaning written to STDOUT) If pattern is missing, action is carried out for all lines

Running awk Programs Short one, composed at keyboard with little thought awk ‘program’ file1 file2 … Note that awk can take a sequence of files as input. Long one, composed in editor awk –f progfile file1 file2 …

awk’s View of Files Input to awk are text files Divided into lines Each line divided into fields by blanks or tabs (the default separator) Each field referenced by relative number, $1, $2, $3, … $0 refers to the entire line

Examples awk ‘{print $1}’ names awk ‘/M/’ names Print the first field in each line of the names file awk ‘/M/’ names Print each line of the names file that contains an upper case M

Some Built-In Variables NR, line number of current line of input (runs sequential over all input files) NF, number of fields in current line FS, the field separator FS = “\t” sets the separator to tab, only FS = “:” sets the separator to colon FNR, number of the current line (record) in the current input file (resets when a new input file is opened)

Examples {print NR, NF} {print NR, $0} {print $NF} NR == 10 NF != 3

Patterns Special patterns BEGIN Action is done once before any lines of the input file(s) are read END Action is done once after the last file has been processed Relational expressions between strings or numbers Arguments treated as numbers, if possible

Comparison Operators < less than > greater than <= less than or equal to >= greater than or equal to == equal to != not equal to ~ matches !~ does not match

Regular Expressions Enclosed in / / Matches in entire line Field match specified as $3 ~ /Ab/, for example Special symbols \ ^ $ . [ ] * + ? ( ) |

Examples /Asia/ /^.$/ /a\$/ /\t/ $2 !~ /^[0-9]+$/ /(apple|cherry) (pie|tart)/ (note space)

C Escape Sequences \b backspace \f formfeed \n newline \r carriage return \t tab \ddd character whose ASCII value in octal is ddd \” quotation mark \c any other character c literally

Actions Mini C-like programs Can extend over several lines Statements terminated by semicolons or newlines. Statements grouped with braces { }. Variables are either floating point numbers or strings. Variables are automatically declared and initialized Strings initialized to “”, the empty string Numbers initialized to 0

Assignment Statements Simple version: v = e Variable or field name assigned value of expression Assignment operators: v op= e means v = v op e Legal values of op are + - * / % ^ Used because interpreted code runs faster

Increment Operators Borrowed from C Prefix or postfix ++ or – Example: x = 3. What is the value of k? k = x++ k = ++x k = x-- k = --x

Arithmetic Functions sin(x) assumes x is in radians cos(x) assumes x is in radians atan2(y,x) range from –pi to pi exp(x) exponential log(x) natural logarithm of x, so x>0 sqrt(x) square root of x, so x >= 0 int(x) truncates fractional part rand(x) returns a random number in [0,1] srand(x) sets the seed for rand to x

Strings Literal values enclosed in double quotes “abc” “Wildcats rule” “20 bananas” Concatenation represented by juxtaposition s = “Villanova” t = “Wildcats” {print s t}

String Functions “Standard” string operations (cf. head, tail, firstfew, lastfew, allbut) length(s) length of s length = length($0) index(s,t) if t is a substring of s return position of first character, return 0 otherwise substr(s,p) returns substring starting at position p if 0<p<=length(s), returns empty string otherwise substr(s,p,n) returns substring of length n starting at position p

String Functions (2) “Editing” functions sub(r,s) replace r by s in current record (first occurrence only) sub(r,s,t) replace r by s in t (first occurrence only) gsub(r,s) replace r by s in current record (globally) gsub(r,s,t) replace r by s in t (globally) In all cases, return the number of substitutions

Control Structures if (<expression>) <s1> else <s2> <expression> can be any expression; true is defined to be non-zero or non-null <s1> and <s2> can be any group of statements Note the critical parentheses that separate the conditional expression from <s1>

Control Structures (2) while (<expression>) <s1> Same rules as for if-then-else

Control Structures (3) for (<e1>;<e2>;<e3>) <s1> is equivalent to <e1>; while (<e2>) {<s1>;<e3>} for (k in <array>) <s1> loops over the subscripts of an array but the order of the subscripts is random. Careful: awk allows general subscripting. Strings can be used as subscripts.

Control Structures (4) “Go to” structures break when executed within a for or while statement, causes an immediate exit continue when executed within a for or while statement, causes immediate execution of the next iteration next causes the next line (record) of the input file to be read and the sequence of pattern {action} statements executed on it exit causes the program to jump to the END pattern, execute it, and stop