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.

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/12 Steven Leung Very Basic Perl Tricks A Few Ground Rules File I/O and Formatting Operators, Flow Control Statements Regular Expression Subroutines Hash.
CSCI 330 The UNIX System Awk The Bash Shell
CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Shell Script Assignment 1.
EMT 2390L Lecture 4 Dr. Reyes Reference: The Linux Command Line, W.E. Shotts.
1 Unix Talk #2 AWK overview Patterns and actions Records and fields Print vs. printf.
2000 Copyrights, Danielle S. Lahmani UNIX Tools G , Fall 2000 Danielle S. Lahmani Lecture 6.
UNIX Filters.
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
Chapter 4: UNIX File Processing Input and Output.
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.
A Guide to Unix Using Linux Fourth Edition
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.
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.
Linux+ Guide to Linux Certification, Third Edition
Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections.
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.
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.
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.
5 1 Data Files CGI/Perl Programming By Diane Zak.
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.
Searching and Sorting. Why Use Data Files? There are many cases where the input to the program may come from a data file.Using data files in your programs.
(Re)introduction to Unix Sarah Medland. So Unix…  Long and venerable history  
Getting the most out of the workshop Ask questions!!! Don’t sit next to someone you already know Work with someone with a different skillset and different.
(Re)introduction to Unix Sarah Medland. So Unix…  Long and venerable history  
Chapter Six Introduction to Shell Script Programming.
© 2006 KDnuggets [16/Nov/2005:16:32: ] "GET /jobs/ HTTP/1.1" "
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
– Introduction to the Shell 1/21/2016 Introduction to the Shell – Session Introduction to the Shell – Session 3 · Job control · Start,
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.
Linux+ Guide to Linux Certification, Second Edition
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
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
Filters and Utilities. Notes: This is a simple overview of the filtering capability Some of these commands are very powerful ▫Only showing some of the.
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.
Lesson 5-Exploring Utilities
AWK.
CSC 4630 Meeting 7 February 7, 2007.
Chapter 6 Filters.
Shell Script Assignment 1.
PROGRAMMING THE BASH SHELL PART IV by İlker Korkmaz and Kaya Oğuz
CS 403: Programming Languages
Tutorial of Unix Command & shell scriptS 5027
Tutorial of Unix Command & shell scriptS 5027
John Carelli, Instructor Kutztown University
Tutorial of Unix Command & shell scriptS 5027
CSCI The UNIX System Regular Expressions
Awk.
Presentation transcript:

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 release 1985 In BSD, OS X: bawk or nawk. GNU/Linux : gawk The basic function of AWK: – Search files for lines that contains certain patterns

Basic command pattern {action} pattern, can use R.E pattern is optional awk ‘/foo/{print $0}’ file Use single quote, to avoid shell interpret the pattern print the whole line that matches pattern The whole line

The Basics of AWK A line is called a record text separated by delimiter is called field $0, $1,... etc – $0 : the whole line – $1 : the first field in a line NR : Number of record – also the line number NF : number of fields in a line

Demos print line number, employee first name, and number of fields % cat employees Tom Jones /12/ Mary Adams /4/ Sally Chang /22/ Billy Black /23/ %awk ‘{print NR, $1, NF}’ employees 1 Tom 5 2 Mary 5 3 Sally 5 4 Billy 5

The Basics of AWK cont. FS : field separator – default is space and/or tabs (strip the leading blanks) – change by -F (e.g. -F:, -F’[ :\t]’ ) OFS : output field separator – default is space – change by ‘{OFS=DELIMITER};’ –% awk ‘{OFS=“-----”} ;{print $1, $2}’ employees Tom----Jones Mary----Adams Sally----Chang Billy----Black

Demos use ‘:’ as the delimiter, instead of space awk –F: ‘{print $1}’ newlist Use ‘:’ and ‘ ‘ as delimiter %cat newlist.txt Tom Jones:4424:5/12/66: Mary Adams:5346:11/4/63:28765 Sally Chang:1654:7/22/54: Billy Black:1683:9/23/44: %awk -F: '{print $1}' newlist.txt Tom Jones Mary Adams Sally Chang Billy Black %awk -F'[: ]' '{print $1}' newlist.txt Tom Mary Sally Billy

Awk patterns and actions awk ‘$3<4000’ employees – print lines where $3 is less than 4000 awk ‘/Tom/{print “Hello, “ $1}’ employees – find the line contains Tom, then print “Hello Tom” awk ‘$1 !~ /ly$/{ print $1}’ employees – print the names that dose not end with ly

Awk in script #file: awk_first /Tom/{print “Tom‘s birthday is ” $3} /Mary/{print NR, $0} #print line number /^Sally/{print "Hi, Sally. " $1 " has salary of $" $4 "."} % awk –F: -f awk_first newlist.txt

AWK comparison expression OperatorMeaningExample < Less than $1 < 100 <= Less than or equal to $1 <= 100 == Equal to $1 == 100 !=!= Not equal to $1 ! =100 >= Greater than or equal to $1 >= 100 > Greater than $1 > 100 ~ Match by regular expression $1 ~ /*ly/ !~ Not match by regular expression $1 !~ /T*/ Conditional expression condition? exp1 : exp2 Logical Operation &&, ||, !

Condition expression example % awk ‘{max=($1>$2)? $1 : $2; print max}’ filename %cat needmax.txt % awk '{max=($1>$2)? $1 :$2; print max}' needmax.txt awk '{ max=0; if ($1 > $2 ) $max = $1 ; else $max = $2; print $max;}' needmax.txt

datafile northwestNWJoel Craig westernWESharon Kelly southwestSWChris Foster southernSOMay Chin southeastSEDerek Johnson easternEASusan Beal northeastNETJ Nichols northNOVal Shultz centralCTSheri Watson

More comparison examples awk ‘$7==5{print $7+5}’ datafile – print lines where the 7 th field is 5 awk ‘$2==“CT” { print $1, $2}’ datafile – print the 1 st and 2 nd field of lines that the 2 nd field is CT awk ‘!($2==“NW”) || $1 ~ /south/{ print $0}’ datafile awk '$8 > 10 && $8 < 17' datafile

Math operators awk '/southern/{print $ }' datafile awk '/southern/{print $8 - 10}' datafile awk '/southern/{print $8 / 2}' datafile awk '/southern/{print $8 * 2}' datafile awk '/northeast/ {print $8 % 3}' datafile

Assignment Operator assignment operators: =, +=, -=, *=, /=, %=, ^= increment and decrement: ++, -- awk '$3 == "Chris"{ $3 = "Christian"; print}' datafile – if a line’s 3 rd field is “Chris”, change it to Christian and print out the line awk ‘/Derek/{$8+=12; print $8}’ datafile awk ‘{$7^=2; print $7}’ datafile – square the 7 th field and print out the 7 th field awk ‘{x=1; y=x++; print x, y}’ datafile

BEGIN Patterns BEGIN pattern is followed by an action block that is executed before AWK processes any lines from the input file. – can run an awk command without file –%awk ‘BEGIN{ print “Hello”;}’ % awk ‘BEGIN{FS=“:”; OSF=“—”; ORS=“\n\n”}{print $1 $2 $3}’ newlist.txt Tom Jones /12/66 Mary Adams /4/63 Sally Chang /22/54 Billy Black /23/44

END Patterns END patterns executes the commands after processing a file –%awk ‘END{ print “The number of records is “ NR}’ employees –awk '/Mary/{count++}END{print "Mary was found " count " times."}' employees The number of records is 4 Mary was found 1 times.

Redirections and Pipes > : save to file >> : append to file awk '$7 >=5 {print $1, $2, $7> “out.txt" }‘ datafile – instead of print to screen, save to ‘out.txt’ file %cat out.txt western WE 5 eastern EA 5 north NO 5 central CT 5

Pipe %awk '/ly/{print $1, $2}' employees Sally Chang Billy Black %awk '/ly/{print $1, $2 |"sort"}' employees Billy Black Sally Chang %awk '/ly/{print $1, $2 |"sort | head – n1"}' employees Billy Black Sally Chang # you can chain as many as you want

There are much more to explore conditional statement loop arrays user-defined functions. Suggest Read Chapter 6 of the book