Awk.

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.
2000 Copyrights, Danielle S. Lahmani UNIX Tools G , Fall 2000 Danielle S. Lahmani Lecture 6.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Integer variables #!/bin/csh # sum of numbers from $argv[1] to $argv[2] set low = $argv[1] set high = $argv[2] set sum = 0 set current = $low while ( $current.
AWK: The Duct Tape of Computer Science Research Tim Sherwood UC San Diego.
Unix Shell Scripts. What are scripts ? Text files in certain format that are run by another program Examples: –Perl –Javascript –Shell scripts (we learn.
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
Lists in Python.
Agenda Sed Utility - Advanced –Using Script-files / Example Awk Utility - Advanced –Using Script-files –Math calculations / Operators / Functions –Floating.
Introduction to Unix (CA263) File Processing. Guide to UNIX Using Linux, Third Edition 2 Objectives Explain UNIX and Linux file processing Use basic file.
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.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Lecture 4  C Shell Scripts(Chapter 10). Shell script/program  Shell script: a series of shell commands placed in an ASCII text file  Commands include.
#!/bin/sh echo Hello World cat Firstshellscript.sh Firstshellscript.sh.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
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.
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}
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.
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.
(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.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
숙명여대 창병모 2011 가을 1. 파일 조작  filtering, sorting, comparing, searching for files 명령어 스케줄링  cron, at 텍스트 처리 프로그램  AWK 파일 보관 및 압축  archiving  tar, gzip.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
Chapter Twelve sed, awk & perl1 System Programming sed, awk & perl.
(Re)introduction to Unix Sarah Medland. So Unix…  Long and venerable history  
Chapter Six Introduction to Shell Script Programming.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
© 2006 KDnuggets [16/Nov/2005:16:32: ] "GET /jobs/ HTTP/1.1" "
16-Dec-15Advanced Programming Spring 2002 sed and awk Henning Schulzrinne Dept. of Computer Science Columbia University.
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
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.
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.
Shell Control Structures CSE 2031 Fall June 2016.
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.
AWK One tool to create them all AWK Marcel Nijenhof Eth-0 11 Augustus 2010.
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.
Shell Control Structures
Kevin Taylor.
PROGRAMMING THE BASH SHELL PART IV by İlker Korkmaz and Kaya Oğuz
Grep Allows you to filter text based upon several different regular expression variants Basic Extended Perl.
CS 403: Programming Languages
What is Bash Shell Scripting?
John Carelli, Instructor Kutztown University
Introduction to C Topics Compilation Using the gcc Compiler
Programming Languages
CSE 303 Concepts and Tools for Software Development
Introduction to C Topics Compilation Using the gcc Compiler
Functions continued.
Presentation transcript:

awk

awk? Alfred Aho, Peter Weinberger, Brian Kernighan Bells Labs 1977

You think awk is bad? Winner of the International Obfuscated C Code (one liner): main(int c,char**v){return!m(v[1],v[2]);}m(char*s,char*t) {return*t-42?*s?63==*t|*s==*t&&m(s+1,t+1):!*t:m(s,t+1)||*s&&m(s+1,t);}

Awk variants awk - original from AT&T - 1977 nawk - A newer & improved (AT&T) - 1993 gawk - The Free Software Foundation - 1985-88

Why awk? Excellent filter and report writer. Processing these rows and columns Easier to use AWK than most conventional programming languages. Considered as a pseudo-C interpreter  understands the same arithmetic operators as C. Has string manipulation functions, so it can search for particular strings and modify the output. Has associative arrays, which are incredible useful.

Pattern Action Pairs condition { action } :

awk Syntax { [ statement ] ...} variable=expression print [ expression-list ] [ > expression ] printf format [ , expression-list ] [ > expression ] next  exit

awk Syntax - more if ( conditional ) statement [ else statement ] while ( conditional ) statement for ( expression ; conditional ; expression ) statement for ( variable in array ) statement break continue

BEGIN …. END BEGIN { do something before main body } condition { action – main body } END { do this after main body } e.g., create a file called fields: #!/bin/awk –f BEGIN { FS = ":" } { print "Name:\t ", $1 print "Year:\t ", $2, "\tMovie: ", $3 } END { print "Number of records:\t ", NR print "Number of fields:\t ", NF } $ chmod 755 fields $ fields moviedb (or ./fields moviedb if you don’t have $PATTH set)

Run as awk or bash script? #!/bin/sh awk ‘ BEGIN { print "Using bash -f" } {print $8, "\t", $3} END { print " -- completed --" } ‘ Or #!/bin/awk -f BEGIN { print "Using awk -f" }

Arithmetic Operators Operator Type Meaning + Arithmetic Addition - Subtraction * Multiplication / Division % Modulo <space> String Concatenation

Arithmetic Operators Examples Expression Result 8+5 13 8-5 3 8*5 40 8/5 1.6 8%5 8 5 85 What’s the output of: x = 2+1*3 8 Same as: (2+(1*3)) “8”  “58”

#0 Put this code in a file called avg BEGIN { FS = "\t" } #1 Expect 1st record = number of students NR == 1 { print "Number of students: ", $1 total=0 next } #2 Add each record and add to total { print $1, "\t", $2 total+=$2 END { print "Average = ", total/NR } $ cp ~tan/public/scores . $ avg scores

# File: matchregex  Counts number of lines matching regex BEGIN { total=0 } /^..*:$/ { # line begins with a ".", followed by any number of chars # and ends in a colon print “Found: ", $0 # $0 means whole line total += 1 } END { print "\n---------------------------------" print "#Matches = ", total } $ chmod 755 match; cp ~tan/public/test . $ match test

Comparing Regex Operator Meaning ~ Matches !~ Doesn't match

# File: matchregex: Counts number of lines where 1st arg matches regex BEGIN { total=0 } $1 ~ /^\.[0-9]+.*/ { # line begins with a-m # followed by any number of char print "Found:\t", $0 total += 1 } END { print "#Matches = ", total } $ chmod 755 matchregex $ matchregex test

# File: matchregex: Counts number of lines where 1st arg matches regex BEGIN { total=0 } $1 ~ /^\.[0-9]+.*/ { # line begins with a-m # followed by any number of char print "Found:\t", $0 total += 1 } END { print "#Matches = ", total } $ chmod 755 matchregex $ matchregex test

Really Weird Syntax !!! Embedding awk in bash # Bash’s arguments vs. awk’s arguments # Find an acronym. File: lookup, DBfile: acronym (copy from ~tanjs/public) #!/bin/sh awk '$1 == find' find=$1 acronyms # or awk '$1 ~ find' find=$1 acronyms Parameters passed to awk are specified after awk script!! $ chmod 755 lookup $ lookup GOT acronyms

Arithmetic Operators Operator Type Meaning + Arithmetic Addition - Subtraction * Multiplication / Division % Modulo <space> String Concatenation

For loop #!/bin/awk –f BEGIN { sum=0 for (i=1; i <= 10; i++) { printf "The sum of integers up to : " printf sum+=i printf " is " print sum } # now end exit;

Associative Arrays #!/bin/awk –f # filename: assArray BEGIN { FS = "\t" } { acro[$1] = $2 } END { for ( abbrev in acro ) print abbrev, acro[abbrev] $ assArray acronyms

bash & awk combo #!/bin/sh # arraylookup: look for an abbreviation in a file using associatve array # Syntax: arraylookup <abbrev> <file>" assArray $2 | grep $1 } $ arraylookup GOT acronyms