Awk 1 – an introduction. Your scripts I will put them on the shared drive. Different solutions – diversity Title of email (lab or question) Efficient?

Slides:



Advertisements
Similar presentations
ARDUINO CLUB Session 1: C & An Introduction to Linux.
Advertisements

CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Lecture 2 Introduction to C Programming
Introduction to C Programming
Introduction to C Programming
The Print Formatting Statement … named printf. 2 Introduction to printf statements print and println statements don’t allow us to easily format output.
1 Unix Talk #2 AWK overview Patterns and actions Records and fields Print vs. printf.
CS 106 Introduction to Computer Science I 11 / 09 / 2007 Instructor: Michael Eckmann.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
Introduction to C Programming
Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,
1 Nassau Community CollegeProf. Vincent Costa Acknowledgements: Introduction to Database Management, All Rights ReservedIntroduction to Database Management.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
A First Book of ANSI C Fourth Edition
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Agenda Sed Utility - Advanced –Using Script-files / Example Awk Utility - Advanced –Using Script-files –Math calculations / Operators / Functions –Floating.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Presenting results to the USER in a professional manner 1. semicolon, disp(), fprintf() 2. Placeholders 3. Special characters 4. Format-modifiers Output.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Sahar Mosleh California State University San MarcosPage 1 System.out.println for console output System.out is an object that is part of the Java language.
Input, Output, and Processing
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.
Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Introduction to programming in the Java programming language.
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.
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.
Asking the USER for values to use in a software 1 Input.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Chapter-4 Managing input and Output operation.  Reading, processing and writing of data are three essential functions of a computer program.  Most programs.
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
Asking the USER for values to use in a software 1 Input.
MATLAB for Engineers, by Holly Moore. ISBN © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is.
Lecture 6: Output 1.Presenting results in a professional manner 2.semicolon, disp(), fprintf() 3.Placeholders 4.Special characters 5.Format-modifiers 1.
Python Let’s get started!.
String Formatting Preparing strings for output. Printing println( arg ) prints its one argument on a line println can be used to print any single value,
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
1 Lecture 10 Introduction to AWK COP 3344 Introduction to UNIX.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
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
+ Note On the Use of Different Data Types Use the data type that conserves memory and still accomplishes the desired purpose. For example, depending on.
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 float Data Type Data type that can hold numbers with decimal values – e.g. 3.14, 98.6 Floats can be used to represent many values: –Money (but see warning.
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
Web Database Programming Using PHP
Arun Vishwanathan Nevis Networks Pvt. Ltd.
More about comments Review Single Line Comments The # sign is for comments. A comment is a line of text that Python won’t try to run as code. Its just.
Arithmetic Expressions Function Calls Output
CSC201: Computer Programming
Web Database Programming Using PHP
CS1010 Discussion Group 11 Week 4 – Overview of C programming.
PHP Introduction.
OUTPUT STATEMENTS GC 201.
John Carelli, Instructor Kutztown University
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
WEB PROGRAMMING JavaScript.
Programming Funamental slides
Loops CIS 40 – Introduction to Programming in Python
12th Computer Science – Unit 5
Introduction to C Programming
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Switch Case Structures
Getting Started With Coding
Presentation transcript:

Awk 1 – an introduction

Your scripts I will put them on the shared drive. Different solutions – diversity Title of (lab or question) Efficient? Clear? You will again insights Comments (what level) You do not need to reply to my auto reply

Coins.txt gold USA American Eagle gold Austria-Hungary Franz Josef 100 Korona silver USA ingot gold Switzerland ingot gold RSA Krugerrand

Using awk I could then invoke Awk to list all the gold pieces as follows: awk '/gold/' coins.txt

Selecting Fields awk '/gold/ {print $5,$6,$7,$8}' coins.txt This yields: (ie. Prints out certain columns) American Eagle Franz Josef 100 Korona ingot Krugerrand

General Form This example demonstrates the simplest general form of an Awk program: awk { }

Fields {print $5,$6,$7,$8}. The "$5", "$6", "$7", and "$8" are "fields", or "field variables", which store the words in each line of text by their numeric sequence. "$1", for example, stores the first word in the line, "$2" has the second, and so on. By default, a "word" is defined as any string of printing characters separated by spaces.

default program action * Awk's default program action is to print the entire line, which is what "print" does when invoked without parameters. This means that the first example: awk '/gold/' coins.txt -- is the same as: awk '/gold/ {print}' coins.txt

$0 Note that Awk recognizes the field variable $0 as representing the entire line, so this could also be written as: awk '/gold/ {print $0}'

If statement I want to list all the coins that were minted before I invoke Awk as follows: awk '{if ($3 < 1980) print $3, " ",$5,$6,$7,$8}' coins.txt This yields: 1908 Franz Josef 100 Korona 1979 Krugerrand

NR (number of records) The next example prints out how many coins are in the collection: awk 'END {print NR,"coins"}' coins.txt This yields: 13 coins

general form of an Awk program the general form of an Awk program to: awk 'BEGIN { } { } { }... END { }'

* Suppose the current price of gold is $425, and I want to figure out the approximate total value of the gold pieces in the coin collection. I invoke Awk as follows: awk '/gold/ {ounces += $2} END {print "value = $" 425*ounces}' coins.txt This yields: (note ounces is user defined) value = $2592.5

So the program action: {ounces += $2} Another way of saying: {ounces = ounces + $2} The final action is to compute and print the value of the gold: END {print "value = $" 425*ounces}

AWK PROGRAM EXAMPLE Instead of doing it all from the command line We can do it all from a file, With the following syntax awk -f

Output of program Summary Data for Coin Collection: Gold pieces: nn Weight of gold pieces: nn.nn Value of gold pieces: n,nnn.nn Silver pieces: nn Weight of silver pieces: nn.nn Value of silver pieces: n,nnn.nn Total number of pieces: nn Value of collection: n,nnn.nn

# This is an awk program that summarizes a gold coin collection. /gold/ { num_gold++; wt_gold += $2 } # Get weight of gold. END { val_gold = 485 * wt_gold; # Compute value of gold. print "Summary data for coin collection:"; # Print results. printf ("\n"); printf (" Gold pieces: %2d\n", num_gold); printf (" Weight of gold pieces: %5.2f\n", wt_gold); printf (" Value of gold pieces: %7.2f\n",val_gold); printf ("\n"); printf ("\n"); printf (" Total number of pieces: %2d\n", NR); printf (" Value of collection: %7.2f\n", total); }

a few interesting features: Comments can be inserted in the program by preceding them with a "#". Note the statements "num_gold++" and "num_silver++". the "++" operator simply increments the specified variable by one. Multiple statements can be written on the same line by separating them with a semicolon (";").

Printf (print format) printf(" ", ) For example, the format code "%2d" tells Awk to print a two-digit integer number, and the format code "%7.2f" tells Awk to print a seven-digit floating-point number, with two digits to the right of the decimal point.

tutorial You can cut and paste the commands from here or from these slides. You can easily look at this in your 2 hours private study time, and do not forget to mail me your scripts so far. …..