2000 Copyrights Danielle S. Lahmani UNIX Tools G22.2245-001, Fall 2000 Danielle S. Lahmani Lecture 5.

Slides:



Advertisements
Similar presentations
NETW-240 Shells Last Update Copyright Kenneth M. Chipps Ph.D. 1.
Advertisements

2000 Copyrights, Danielle S. Lahmani UNIX Tools G , Fall 2000 Danielle S. Lahmani Lecture 6.
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 Basics CS465 - Unix. Shell Basics Shells provide: –Command interpretation –Multiple commands on a single line –Expansion of wildcard filenames –Redirection.
Quotes: single vs. double vs. grave accent % set day = date % echo day day % echo $day date % echo '$day' $day % echo "$day" date % echo `$day` Mon Jul.
Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
Linux+ Guide to Linux Certification, Second Edition
Lecture 02CS311 – Operating Systems 1 1 CS311 – Lecture 02 Outline UNIX/Linux features – Redirection – pipes – Terminating a command – Running program.
2000 Copyrigths Danielle S. Lahmani UNIX Tools G , Fall 2000 Danielle S. Lahmani Lecture 4.
Introduction to Unix – CS 21 Lecture 5. Lecture Overview Lab Review Useful commands that will illustrate today’s lecture Streams of input and output File.
Unix Filters Text processing utilities. Filters Filter commands – Unix commands that serve dual purposes: –standalone –used with other commands and pipes.
UNIX Filters.
Shell Script Examples.
Chapter 4: UNIX File Processing Input and Output.
Overview of the grep Command Alex Dukhovny CS 265 Spring 2011.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
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.
LIN 6932 Unix Lecture 6 Hana Filip. LIN 6932 HW6 - Part II solutions posted on my website see syllabus.
An Introduction to Unix Shell Scripting
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.
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
CS 403: Programming Languages Lecture 21 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Regular expressions Used by several different UNIX commands, including ed, sed, awk, grep A period ‘.’ matches any single characters.X. matches any X.
CS 403: Programming Languages Fall 2004 Department of Computer Science University of Alabama Joel Jones.
OPERATING SYSTEMS DESIGN UNIX BASICS & SHELL SCRIPTING.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
Linux+ Guide to Linux Certification, Third Edition
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections.
Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.
Agenda Regular Expressions (Appendix A in Text) –Definition / Purpose –Commands that Use Regular Expressions –Using Regular Expressions –Using the Replacement.
BIF713 Additional Utilities. Linux Utilities  You have learned many Linux commands. Here are some more that you can use:  Data Manipulation (Reg Exps)
I/O Redirection and Regular Expressions February 9 th, 2004 Class Meeting 4.
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 Five Advanced File Processing. 2 Lesson A Selecting, Manipulating, and Formatting Information.
Chapter Four I/O Redirection1 System Programming Shell Operators.
I/O Redirection & Regular Expressions CS 2204 Class meeting 4 *Notes by Doug Bowman and other members of the CS faculty at Virginia Tech. Copyright
LIN Unix Lecture 5 Unix Shell Scripts. LIN Command Coordination ; && || command1 ; command2 Interpretation: Do command 1. Then do command.
Advanced Text Processing. 222 Lecture Overview  Character manipulation commands cut, paste, tr  Line manipulation commands sort, uniq, diff  Regular.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems.
Linux+ Guide to Linux Certification, Second Edition
Lecture 1: Introduction, Basic UNIX Advanced Programming Techniques.
ORAFACT Text Processing. ORAFACT Searching Inside Files grep - searches for patterns within files grep [options] [[-e] pattern] filename [...] -n shows.
Lesson 6-Using Utilities to Accomplish Complex Tasks.
1 © 2012 John Urrutia. All rights reserved. Chapter 09 The TC Shell.
File Processing. Introduction More UNIX commands for handling files Regular Expressions and Searching files Redirection and pipes Bash facilities.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Linux Tutorial Lesson Two *Getting Help in Linux *Data movement and manipulation *Relative and Absolute path *Processes Note: see chapter 1,2,3 from Linux.
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.
Regular Expressions Copyright Doug Maxwell (
Tutorial of Unix Command & shell scriptS 5027
Lesson 5-Exploring Utilities
Regular Expression Basic and Extended regular expressions. The grep, egrep. Typical examples involving different regular expressions. By: Abhilash C B.
The UNIX Shell Learning Objectives:
Lecture 9 Shell Programming – Command substitution
CS 403: Programming Languages
INTRODUCTION TO UNIX: The Shell Command Interface
Tutorial of Unix Command & shell scriptS 5027
Tutorial of Unix Command & shell scriptS 5027
Folks Carelli, Instructor Kutztown University
Tutorial of Unix Command & shell scriptS 5027
Unix Talk #2 grep/egrep/fgrep (maybe add more to this one….)
Regular Expressions and Grep
Software I: Utilities and Internals
Review.
LPI Linux Certification
Presentation transcript:

2000 Copyrights Danielle S. Lahmani UNIX Tools G , Fall 2000 Danielle S. Lahmani Lecture 5

2000 Copyrights Danielle S. Lahmani Overview: Unix Filters finish discussing /bin/csh features sort grep/ fgrep/ egrep find cmp/diff uniq, tr tar/cpio

2000 Copyrights Danielle S. Lahmani /bin/csh: history mechanism For numbered commands –set prompt = '\! %’ storage of commands –set history = 40size of history file –set savehist=32save 32 commands across sessions

2000 Copyrights Danielle S. Lahmani /bin/csh: hist mech (continued) command reexecution –!!reexecute previous command –!number –!prefix –!?substring command editing –% !even :s/pat1/pat2 substitute pat1 with pat2 –%^pat1^pat2reexecution of previous command substituting pat1 with pat2

2000 Copyrights Danielle S. Lahmani ACCESS PORTION OF A FILENAME Modifierpart of fileportion of filename returned :hheadfilename minus trailing path :rrootfilename minus trailing *.suffix :eextensiontrailing.* suffix :ttailfilename minus leading dir path

2000 Copyrights Danielle S. Lahmani /bin/csh: CONTROL STRUCTURES foreach ……. end if then else endif switch case … endsw while … end onintr [-l label] allows to specify a label to jump to when shell receives a SIGINT

2000 Copyrights Danielle S. Lahmani /bin/csh: PIPING Process1 |& process2 Redirects standard output and standard error from process 1 to process 2

2000 Copyrights Danielle S. Lahmani /bin/csh:MULTIPLE REDIRECTION Cmd >& filesend both standard output and standard error to file Cmd >&! file same as above, even in noclobber is set Cmd >>& file append standard error and standard output to end of file Cmd1 | & cmd2 pipe standard error together with standard output (cmd > file1) >& file2 send standard output to file1; send standard error to file2

2000 Copyrights Danielle S. Lahmani /bin/csh: JOB CONTROL Same as ksh, plus stopsuspend background process suspendsuspend foreground process notifynotify when current job changes status

2000 Copyrights Danielle S. Lahmani /bin/csh: DIRECTORY STACK pushd [+number|name] pushd pushes the directory onto the directory stack popd [+number] popdpops a directory from the directory stack dirs: lists the current directory stack.

2000 Copyrights Danielle S. Lahmani Effective and efficient shell prog reference: Unix shell Tutorial G.Snyder J.R. Mashey Bell Labs Minimize number of processes generated Find out which cmds are built-in and which are not.  Number of data bytes accessed  Minimize Directory searches  Optimize Directory search order and the PATH variable  Minimize arithmetic and character processing  Avoid command substitution when you can  Move loop invariant outside of loop, especially for command substitution

2000 Copyrights Danielle S. Lahmani sort utility sort sorts a file in ascending or descending order based on one or more sort fields, and works as follows : sort [options]{filename}*

2000 Copyrights Danielle S. Lahmani sort utility : common options sort filesort file by line, in ASCII order sort -r reverse normal order sort -n sort in numeric (arithmetic) order starting at the beginning of line sort -nr sort in reverse numeric order

2000 Copyrights Danielle S. Lahmani sort utility: common options (continued) sort -f (“fold”) sort in alphabetical order ignoring case sort -o file save output to filename sort -u suppresses all but one of each group if lines that are identical in the sort fields

2000 Copyrights Danielle S. Lahmani sort utility : common options (continued) sort [+m] [-n] sort starting at m+1 field and sort up to field n non- inclusive. If n is not defined, then it is assumed until the end of the line. The first field is numbered 0. sort -k field_start, field_end sort starting at field_start and ending at field_end inclusive. field numbers with this option start at 1.

2000 Copyrights Danielle S. Lahmani grep/fgrep/egrep utilities grep [option] pattern {filename}* grep searches the named files or standard input and prints each line that contains an instance of the pattern. grep interprets the same regular expressions as ed(1)

2000 Copyrights Danielle S. Lahmani grep/egrep/fgrep utilities g/re/p global regular expression print –regular expressions are specified by giving special meanings to certain characters.

2000 Copyrights Danielle S. Lahmani grep/egrep/fgrep utilities ^ and $ "anchor" the pattern to the beginning (^) or end ($) of the line. –regular expressions metacharacters overlap with shell metacharacters, so it's always a good idea to enclose grep patterns in single quotes, to suppress interpretation by the shell. $ grep From $MAIL locates lines containing From in your mailbox $ grep '^From'$MAIL prints lines that begin with From in your mailbox

2000 Copyrights Danielle S. Lahmani grep utility grep supports character classes much like those in the shell. –[a-z] matches any lower case letter –[^….] matches any character except those in the class –[^0-9] matches any non-digit –A period '.' is equivalent to the shell's ? it matches any single character.

2000 Copyrights Danielle S. Lahmani grep examples print lines that begin with mary –$ who | grep '^mary' print lines that end with mary –$ who | grep 'mary$’ list files others can read and write –$ls -l | grep '^ rw’

2000 Copyrights Danielle S. Lahmani grep examples: the closure operator * applies to previous character or metacharacter ( including a character class) in expression grep 'abc*' matches ab followed by zero or more c's grep 'ab[a-z]*' matches ab followed by any number of lower case letters

2000 Copyrights Danielle S. Lahmani grep utility grep '^[^:]*::' matches beginning of line, zero or more non- colons followed by a double colon. Inside the brackets, the ^ means not. no grep regular expression matches a newline; the expressions are applied to each line individually.

2000 Copyrights Danielle S. Lahmani grep/fgrep/ egrep utilities Example: grep '^[^aeiou]*a[^aeiou]*e$' foo –Looks for zero or more non-vowels followed by a, followed by zero or more non vowels followed by e at the end of the line –Would match dsdakjkjkje … \ turns off meaning of special character that follows: –$ grep \' foofind all ' apostrophes in file foo

2000 Copyrights Danielle S. Lahmani Common grep options -n prints line numbers $ grep -n variable *.[ch]locate variable in C source -v inverts the sense of the test $ grep -v From fooprint all lines that do not contain From in file foo -i makes lower case in pattern match either case in file $ grep -i mary $HOME/bin/phone-book

2000 Copyrights Danielle S. Lahmani grep common options (continued) -c prints only a count of matched lines $grep -c /bin/csh /etc/passwd -l list filenames but not matched lines $ grep -l '^#include' /usr/include/*

2000 Copyrights Danielle S. Lahmani egrep is an extended grep that works on extended regular expression,accepts: + one or more occurrences ? zero or one occurrence pat1| pat2 "or" op matches on either pat1 or pat2 ( r) regular expression r, can be nested (xy)* matches any of the empty string, xy, xyxy, xyxyxy and so on

2000 Copyrights Danielle S. Lahmani fgrep/ egrep utilities fgrep searches for many literal strings simultaneously. It does not work with *. Both, fgrep and egrep have -f option, to read patterns stored in a file. In file, newlines separate patterns to be searched for simultaneously.

2000 Copyrights Danielle S. Lahmani TABLE of Regular expressions for grep and egrep cany-non special character c matches itself \cturns off any special meaning of character c ^beginning of line $end of line.matches any single character […]any one of characters in …; [^…]any character not in …

2000 Copyrights Danielle S. Lahmani TABLE of Regular expressions for grep and egrep (continued) r *zero or more occurrences of r r+one or more occurrences of r (egrep only) r?zero or one occurrences of r (egrep only) r1r2r1 followed by r2 r1| r2r1 or r2 ( egrep only) (r)nested regular expression r (egrep only)

2000 Copyrights Danielle S. Lahmani find utility find pathlist expression find recursively descends through pathlist and applies expression to every file. Expression can be (subset): -name pattern true if file name matches pattern. Pattern mayinclude metacharacters such as *, must be in quotes to suppress shell interpretation.

2000 Copyrights Danielle S. Lahmani find utility (continued) -perm mode find files with given access mode, mode must be in octal. -type ch file type ch (c=character, b=block, f for plain file, etc..) -user userid/username true if owner is userid or username -group groupid/groupname true if group of file is groupid or groupname

2000 Copyrights Danielle S. Lahmani find utility (common options) !expression returns the logical negation of expression -print prints out the name of the current file Op1 a op2 matches both patterns op1 and op2 Op1 o op2 matches either op1 or op2

2000 Copyrights Danielle S. Lahmani find utility (continued) -exec cmd executes cmd,where cmd must be terminated by an escaped semicolon (\;). If you specify {} as a command line argument, it is replaced by the name of the current file just found. Exec executes command once per file. Example: find -name "*.o" -exec rm {} \;

2000 Copyrights Danielle S. Lahmani find utility (continued) find. -type f -print | xargs grep From -type f for files -print to print them out xargs works on every file found In general, xargs applies a utility (in this case grep) to one or more arguments with the remaining arguments coming from a single line of the standard input. xargs splits the standard input into a bunch of files that grep applies to.

2000 Copyrights Danielle S. Lahmani cmp: comparing two files cmp: tests two files for equality. –finds the first byte that differs between two files –cmp -ls file1 file2 [offset1] [offset2 ] –-s causes all output to be inhibited –-l displays byte offset and values of every byte that doesn't match

2000 Copyrights Danielle S. Lahmani diff: comparing two files diff: compares two files and outputs a description of their differences diff -i -Dflag file1 file2 - I makes diff ignore the case of the lines -D generates output designed for the C preprocessor

2000 Copyrights Danielle S. Lahmani uniq: removing duplicate lines uniq [options] [inputfile [outputfile]] removes duplicate adjacent lines from inputfile sending one copy of each line to outputfile or standard output often used as a filter.

2000 Copyrights Danielle S. Lahmani uniq: removing duplicate lines Options can be: - c print each line once, counting instances of each -d print duplicate lines once, but no unique lines - u print only unique lines ( no copy of duplicate entries is kept) -n ignore first n fields of a line. (fields are separated by space or tab)

2000 Copyrights Danielle S. Lahmani tr:translating characters tr maps maps the characters in a file from one character set to another –example: –translate all lowercase characters to uppercase –tr a-z A-Z < inputfile –deletes all a-c characters –tr -d a-c < inputfile