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-560085.

Slides:



Advertisements
Similar presentations
Jump to first page Unix Commands Monica Stoica Jump to first page Introduction to Unix n Unix was born in 1969 at Bell Laboratories, a research subdivision.
Advertisements

A Guide to Unix Using Linux Fourth Edition
Nested Loops. Problem Print The only printfs you can use are: –printf(“*”); –printf(“\n”); *****
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
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
Introduction to Linux and Shell Scripting Jacob Chan.
2 $ command Command Line Options ls –a –l hello hi Command Arguments.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Advanced File Processing
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
Chapter Nine Advanced Shell Scripting1 System Programming Advanced Shell Scripting.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Chapter Four UNIX File Processing. 2 Lesson A Extracting Information from Files.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
Introduction to Unix (CA263) File Processing. Guide to UNIX Using Linux, Third Edition 2 Objectives Explain UNIX and Linux file processing Use basic file.
CS 497C – Introduction to UNIX Lecture 7: General-Purpose Utilities Chin-Chih Chang
UNIX and Shell Programming (06CS36) Unit 1 Continued… Shrinivas R. Mangalwede Department of Computer Science and Engineering K.L.S. Gogte Institute of.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Session 2 Wharton Summer Tech Camp Basic Unix. Agenda Cover basic UNIX commands and useful functions.
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.
Essential Shell Programming by Prof. Shylaja S S Head of the Dept. Dept. of Information Science & Engineering, P.E.S Institute of Technology, Bangalore
Linux+ Guide to Linux Certification, Third Edition
Linux Operations and Administration
CMPSC 60: Week 6 Discussion Originally Created By: Jason Wither Updated and Modified By: Ryan Dixon University of California Santa Barbara.
Shell Programming Any command or a sequence of UNIX commands stored in a text file is called a shell program. It is common to call this file a command.
Chapter Five Advanced File Processing Guide To UNIX Using Linux Fourth Edition Chapter 5 Unix (34 slides)1 CTEC 110.
UNIX Commands. Why UNIX Commands Are Noninteractive Command may take input from the output of another command (filters). May be scheduled to run at specific.
Chapter Five Advanced File Processing. 2 Objectives Use the pipe operator to redirect the output of one command to another command Use the grep command.
Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections.
A Menu-Driven Application Yonglei Tao. Problem Statement  Develop a script program that allows the user to create and maintain an directory, called.
Agenda Link of the week Use of Virtual Machine Review week one lab assignment This week’s expected outcomes Review next lab assignments Break Out Problems.
De Anza College Instructor: Clare Nguyen
Day 6 COP 3502 David A. Gaitros Department of Computer Science Florida State University.
Chapter Five Advanced File Processing. 2 Lesson A Selecting, Manipulating, and Formatting Information.
CSC 4630 Meeting 5 January 31, Next Time Enhance the steps that you used to clean the Moby Dick chapter to create a shell script that takes any.
LING 408/508: Programming for Linguists Lecture 8 September 23 rd.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
Chapter Six Introduction to Shell Script Programming.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Week Two Agenda Announcements Link of the week Use of Virtual Machine Review week one lab assignment This week’s expected outcomes Next lab assignments.
Shell Control Statements and More
Environment After log in into the system, a copy of the shell is given to the user Shell maintains an environment which is distinct from one user to another.
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.
– Introduction to the Shell 1/21/2016 Introduction to the Shell – Session Introduction to the Shell – Session 3 · Job control · Start,
Sed. Class Issues vSphere Issues – root only until lab 3.
Linux+ Guide to Linux Certification, Second Edition
Assigning Values 1. $ set One Two Three [Enter] $echo $1 $2 $3 [Enter] 2. $set `date` [Enter] $echo $1 $2 $3 [Enter] 3. $echo $1 $2 $3 $4 $5 $6 [Enter]
© Prepared By: Razif Razali 1 TMK 265: UNIX SYSTEM CHAPTER 8: USER INPUT.
2 nd Intro to Shell Scripting. Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by midnight Labs 7 & 8 or late ones – 8 is extra credit – Due.
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
File Management commands cat Cat command cat cal.txt cat command displays the contents of a file here cal.txt on screen (or standard out).
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
Part I: n Q1: n $ file * | grep “ascii text” | wc -l n Q2: n $ a.out | tee logfile n Q3: $touch file.`date +’%H%M%S’` n.
 Prepared by: Eng. Maryam Adel Abdel-Hady
Linux Administration Working with the BASH Shell.
 Prepared by: Eng. Maryam Adel Abdel-Hady
Part 1: Basic Commands/Utilities
Unix Scripting Session 4 March 27, 2008.
What is Bash Shell Scripting?
Guide To UNIX Using Linux Third Edition
awk- An Advanced Filter
Essential Shell Programming
Essential Shell Programming
Essential Shell Programming
awk- An Advanced Filter
Introduction to Bash Programming, part 3
Essential Shell Programming
Presentation transcript:

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

Course Objectives shell & awk scripts

Program 1: This shell script finds and displays all the links of a file specified as the first argument to the script. The second argument, which is optional, can be used to specify the directory in which the search is to begin. If this second argument is not present the search is to begin in current working directory.

Contd.. #!/bin/bash if [ $# -eq 0 ] then echo "Usage:sh 8a.sh[file1] dir1(optional)]" exit fi if [ -f $1 ] then dir="."

Contd.. if [ $# -eq 2 ] then dir=$2 fi inode=`ls -i $1|cut -d " " -f 2` echo "Hard links of $1 are" find $dir -inum $inode -print echo "Soft links of $1 are" find $dir -lname $1 -print

Contd.. else echo "The file $1 does not exist" fi Run1: shellprgms]$ sh 9.sh hai.c Hard links of hai.c are./hai.c Soft links of hai.c are./hai_soft

Program 2: This shell script displays the calendar for current month with current date replaced by * or ** depending on whether date has one digit or two digits.

Contd.. #!/bin/bash n=` date +%d` echo " Today's date is : `date +%d%h%y` "; cal > calfile if [ $n -gt 9 ] then sed "s/$n/\**/g" calfile else sed "s/$n/\*/g" calfile fi

Contd.. shellprgms]# sh 10.sh Today's date is : 10 May 05 May 2005 Su Mo Tu We Th Fr Sa **

Program 3 awk script to delete duplicate lines in a file. BEGIN { i=1;} { flag=1; for(j=1; j<i && flag ; j++ ) { if( x[j] == $0 ) flag=0; }

Contd.. if(flag) { x[i]=$0; printf "%s \n",x[i]; i++; }

Contd.. Run1: shellprgms]$ cat >for7.txt hello world hello this is this

Contd.. Output: shellprgms]$ awk -F "|" -f 11.awk for7.txt hello world this is

Program 4 awk script to print the transpose of a matrix. BEGIN{ system(“tput clear”) count =0 } { split($0,a); for(j=1;j<=NF;j++)

Contd.. { count = count+1 arr[count] =a[j] } K=NF } END{ printf(“Transpose\n”); for(j=1;j<=K;j++) { for(i=j; i<=count; i=i+K)

Contd.. { printf(“%s\t”, arr[i]); } printf(“\n”); }

Contd.. Run1: shellprgms]$ qwk –f p8.awk Transpose 25 36

Program 5: This shell script implements terminal locking. Prompt the user for a password after accepting, prompt for confirmation, if match occurs it must lock and ask for password, if it matches terminal must be unlocked

Contd.. trap “ " clear echo -e “\nenter password to lock terminal:" stty -echo read keynew stty echo echo -e “\nconfirm password:" stty -echo read keyold stty echo

Contd. if [ $keyold = $keynew ] then echo "terminal locked!" while [ 1 ] do echo "retype the password to unlock:" stty -echo read key if [ $key = $keynew ] then

Contd.. stty echo echo "terminal unlocked!" stty sane exit fi echo "invalid password!" done else

Contd.. echo " passwords do not match!" fi stty sane

Contd.. Run1: shellprgms]# sh 13.sh enter password: confirm password: terminal locked! retype the password to unlock: invalid password! retype the password to unlock: terminal unlocked!

Program 6: Awk script that folds long line into 40 columns. Thus any line that exceeds 40 Characters must be broken after 40th and is to be continued with the residue. The inputs to be supplied through a text file created by the user.

Contd.. BEGIN{ start=1; } { len=length; for(i=$0; length(i)>40; len-=40) { print substr(i,1,40) "\\" i=substr(i,41,len); } print i; }

Contd.. Run1: shellprgms]$ awk -F "|" -f 15.awk sample.txt aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaa\ aaaaaaaaaaaa aaaaaaaaaaaaaa

Contd.. aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaa\ aaaaaaaaa Output: shellprgms]$ cat sample.txt aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa

Contd.. aaaaaaaaaaaa aaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaa

Program 7: This is an awk program to provide extra spaces at the end of the line so that the line length is maintained as 127.

Contd.. awk ‘ { y=127 – length($0) printf “%s”, $0 if(y > 0) for(i=0;i<y;i++) printf “%s”, “ ” printf “\n” }’ foo

Program 8: A file contains a fixed number of fields in the form of space-delimited numbers. This is an awk program to print the lines as well as total of its rows.

Contd.. awk ‘{ split($0,a) for (i=1;i<=NF;i++) { row[NR]+=a[$i] } printf “%s”, $0 printf “%d\n”, row[NR] } ’ foo

Conclusion In this session we saw several scripts which make use of different features offered by awk