Agenda Sed Utility - Advanced –Using Script-files / Example Awk Utility - Advanced –Using Script-files –Math calculations / Operators / Functions –Floating.

Slides:



Advertisements
Similar presentations
1 Unix Talk #2 AWK overview Patterns and actions Records and fields Print vs. printf.
Advertisements

2000 Copyrights, Danielle S. Lahmani UNIX Tools G , Fall 2000 Danielle S. Lahmani Lecture 6.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Linux+ Guide to Linux Certification, Second Edition
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Lecture 02CS311 – Operating Systems 1 1 CS311 – Lecture 02 Outline UNIX/Linux features – Redirection – pipes – Terminating a command – Running program.
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.
Unix Filters Text processing utilities. Filters Filter commands – Unix commands that serve dual purposes: –standalone –used with other commands and pipes.
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
Advanced File Processing
Advanced Shell Programming. 2 Objectives Use techniques to ensure a script is employing the correct shell Set the default shell Configure Bash login and.
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
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.
Writing Web Pages By Shyam Gurram. Agenda Writing Web Pages Delimiting PHP Program Units. Displaying Output to Web Pages Putting Comments in PHP Programs.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
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
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Advanced File Processing. 2 Objectives Use the pipe operator to redirect the output of one command to another command Use the grep command to search for.
Linux+ Guide to Linux Certification, Third Edition
Chapter Five Advanced File Processing Guide To UNIX Using Linux Fourth Edition Chapter 5 Unix (34 slides)1 CTEC 110.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
(Stream Editor) By: Ross Mills.  Sed is an acronym for stream editor  Instead of altering the original file, sed is used to scan the input file line.
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
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
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.
Revision Lecture Mauro Jaskelioff. AWK Program Structure AWK programs consists of patterns and procedures Pattern_1 { Procedure_1} Pattern_2 { Procedure_2}
Time to talk about your class projects!. Shell Scripting Awk (lecture 2)
Chapter Five Advanced File Processing. 2 Lesson A Selecting, Manipulating, and Formatting Information.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
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
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional.
The awk command. Introduction Awk is a programming language used for manipulating data and generating reports. The data may come from standard input,
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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
CSE 1320 Basics Dr. Sajib Datta
1 © 2001 John Urrutia. All rights reserved. CIS52 – File Manipulation File Manipulation Utilities Regular Expressions sed, awk.
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
Programming Languages Meeting 12 November 18/19, 2014.
Linux Administration Working with the BASH Shell.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
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.
Arun Vishwanathan Nevis Networks Pvt. Ltd.
Lesson 5-Exploring Utilities
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
PROGRAMMING THE BASH SHELL PART IV by İlker Korkmaz and Kaya Oğuz
Agenda Control Flow Statements Purpose test statement
John Carelli, Instructor Kutztown University
Introduction to Bash Programming, part 3
Presentation transcript:

Agenda Sed Utility - Advanced –Using Script-files / Example Awk Utility - Advanced –Using Script-files –Math calculations / Operators / Functions –Floating Point Decimal Calculations Sort Utility - Advance –Reverse Sort / Sort & Subsort by Fields

Using Script-Files As you recall from the previous lesson, sed and awk utilities can contain search and action commands within single or double quotes (often referred to as “start-stop” commands) which can be executed as a command line Unfortunately, executing a one-line sed or awk command may not allow for large quantities of separate categories or search and action for lines contained within a file.

Using Script-Files Therefore, for more complex manipulation of records contained within files involving the sed and awk utilities, it may be more efficient to have the utilities read search and instructor or action sequences from a script- file We will now demonstrate the use of script- files for the sed and awk utilities

Examples of sed Using Script-Files Assume that you want to display contents of a file called “data” that matches several different searches and instructions: –Display all lines that contain a single character “#” at the beginning of a comment line –Display all lines that contain three characters “###” at the beginning of a comment line, but replace “###” with just “#” –Quit the program at the comment line: “# This is the last comment”

Examples of sed Using Script-Files You could create a script-file called “change” that could contain regular expressions and instructions Contents of “change” script-file: /^#/ p /^###/ s/###/#/g w output_file /^# This is the last comment/ q

Examples of sed Using Script-Files To execute this command, you would enter: sed -nf change data Refer to my website for further examples of issuing sed commands with script-files

Using Script-Files with awk The concept is similar when using script- file with “awk” as it was for “sed”, except that the actions can be more complex. In fact, many awk script-files tend to resemble c programming code!

Examples of Using Script-Files with awk Utility Examples: –Generate totals –Generate averages –Format report display –Perform statistical analysis / Create graphs –Change file formats –Perform Floating-Point Decimal Calculations In fact, there are many programs that you can write in the script-file for awk!

Program File (Script) Basic Structure The basic structure of a program file is a /pattern/ search followed by an {action} Below are contents of search program-file: /chevy/{print $1 $3} /ford/{print $1 $3} Command :awk -f search cars

Variables The following is a list of common variables that can be used with awk: $0 Current Record $1 Field number in Record NF Number of fields in record NR Record number of current record FS Input Field Separator (default space / tab) OFS Output Field Separator (default space) RS Input Record Separator (default new line) FILENAME Name of current input file Please check my OPS224 webpage for more variables and examples using variables

Functions The following is a list of common functions that can be used with awk: length(str) int(num) index(str1,str2) substr(str,pos,len) tolower(str) toupper(str) Please check my OPS224 webpage for more variables and examples using functions

Operators The following is a list of common operators that can be used with awk: –+, -, *, /, % (add,subtract,mulitply,divide,modulus) ++, -- (Increment / Decrement variable) +=, -= (Add/Sub expression to preceeding variable) *=, /= (Mult/Div expression to preceeding variable) %= (Modulus after dividing preceeding variable) Please check my OPS224 webpage for more variables and examples using operators

Control-Flow Statements You can also use control-flow statements to alter the flow of awk’s processing Refer to the next slide to see how the syntax for the if, else if else statement resembles c programming language!

Example Below are contents of p_range file: { if ($5 <= 5000) $5 = "inexpensive" else if ($5 > 5000 && $5 <10000) $5 = "Please ask" else if ($5 >= 10000) $5 = "expensive” else printf "%-10s %-8s 19%2d %5d %-12s\n",\ $1, $2, $3, $4, $5 } Command: awk -f p_range cars

Mathematical Calculations Involving awk Mathematical calculations can be used with awk’s action statement involving field numbers ($n) Amounts can be stored in user-created variables, etc.

Floating-Point Decimal Calculations In system such as Phobos, floating-point decimal calculation and display is only available by using awk Since $n represents the nth argument or positional parameters, you can actually send arguments through awk to be processed as floating-point decimal (see next slide for an example)

Floating-Point Decimal Calculations - Example # sends arguments to be processed via awk echo $* | awk ' { result_1=$1+$2 result_2=$1-$2 result_3=$1*$2 result_4=$1/$2 }

Further Examples Refer to your instructor’s webpage for more examples involving the use of script-files with the awk utility For more examples, visit my OPS224 website under “Samples - Advanced awk” and “Samples - Floating Point Calculations”

Advanced Sort (Sorting within Fields) Sort [options] [field specifier list] [file-list] -r Sorts in reverse order -k KeyDefinition Specifies a sort key. -n Sorts numeric fields by arithmetic value. Alpha-numeric sorting (i.e. no “n” option) of a field containing any numeric character gives different results than a numeric sort.

Advanced Sort (Sorting within Fields) Sorting fields that contain text: sort -k2 filename (sorts records of filename by second field) Sorting fields that contain numbers; sort -kn5 filename (sorts records of filename by fifth field - if “n” option was not indicated, would perform an alpha-numeric sort!)

Advanced Sort (Sorting within Fields) A sub-sort is a method to rearrange records or line in order by a secondary field if patterns in the primary field are identifcal (eg sub-sort by model for all chevy’s) sort -k1,2 filename (sorts by field 1 and sub-sort by field 2)