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.

Slides:



Advertisements
Similar presentations
CST8177 sed The Stream Editor. The original editor for Unix was called ed, short for editor. By today's standards, ed was very primitive. Soon, sed was.
Advertisements

Tr. translate characters - standard input. tr x y < namesAndNumbers.txt translated from x to y in file namesAndNumbers.txt tr can be used to produce more.
Regular Expressions grep
7 Searching and Regular Expressions (Regex) Mauro Jaskelioff.
 *, ? And [ …] . Any single character  ^ beginning of a line  $ end of the line.
CS 497C – Introduction to UNIX Lecture 31: - Filters Using Regular Expressions – grep and sed Chin-Chih Chang
Lecture 02CS311 – Operating Systems 1 1 CS311 – Lecture 02 Outline UNIX/Linux features – Redirection – pipes – Terminating a command – Running program.
CS465 - UNIX The vi Editor. Creating Files Most human-readable files on Unix are created with a text editor Unix has many, many different editors ed a.
Unix Filters Text processing utilities. Filters Filter commands – Unix commands that serve dual purposes: –standalone –used with other commands and pipes.
UNIX Filters.
Filters using Regular Expressions grep: Searching a Pattern.
Shell Script Examples.
Advanced File Processing
System Programming Regular Expressions Regular Expressions
Computer Programming for Biologists Class 5 Nov 20 st, 2014 Karsten Hokamp
Unix Talk #2 (sed). 2 You have learned…  Regular expressions, grep, & egrep  grep & egrep are tools used to search for text in a file  AWK -- powerful.
Introduction to Unix (CA263) File Processing. Guide to UNIX Using Linux, Third Edition 2 Objectives Explain UNIX and Linux file processing Use basic file.
LIN Unix Lecture 3 Hana Filip. LIN UNIX Resources UNIX Tutorials UNIX help for.
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.
Perl and Regular Expressions Regular Expressions are available as part of the programming languages Java, JScript, Visual Basic and VBScript, JavaScript,
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
Chapter Five Advanced File Processing Guide To UNIX Using Linux Fourth Edition Chapter 5 Unix (34 slides)1 CTEC 110.
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.
(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.
January 23, 2007Spring Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip.
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)
CSC 352– Unix Programming, Spring 2015 April 28 A few final commands.
I/O Redirection and Regular Expressions February 9 th, 2004 Class Meeting 4.
Sed 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.
Regular Expression - Intro Patterns that define a set of strings (or, pieces of a string) Not wildcards (similar notion, but different thing) Used by utilities.
Introduction to Unix (CA263) File Processing (continued) By Tariq Ibn Aziz.
Introduction to sed. Sed : a “S tream ED itor ” What is Sed ?  A “non-interactive” text editor that is called from the unix command line.  Input text.
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
Regular Expressions CS 2204 Class meeting 6 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Regular Expressions.
BASH – Text Processing Utilities Erick, Joan © Sekolah Tinggi Teknik Surabaya 1.
UNIX Commands RTFM: grep(1), egrep(1) & fgrep(1) Gilbert Detillieux April 13, 2010 MUUG Meeting.
CSCI 330 UNIX and Network Programming Unit IV Shell, Part 2.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 6 – sed, command-line tools wrapup.
ORAFACT Text Processing. ORAFACT Searching Inside Files grep - searches for patterns within files grep [options] [[-e] pattern] filename [...] -n shows.
CSC 352– Unix Programming, Fall 2011 November 8, 2011, Week 11, a useful subset of regular expressions, grep and sed, parts of Chapter 11.
6/13/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 3 (UNIX) 6/13/2016Course material created by D. Woit 1.
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.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/9/2006 Lecture 6 – String Processing.
CIRC Summer School 2016 Baowei Liu
PROGRAMMING THE BASH SHELL PART III by İlker Korkmaz and Kaya Oğuz
Lesson 5-Exploring Utilities
CSE 374 Programming Concepts & Tools
CSC 352– Unix Programming, Spring 2016
CIRC Summer School 2017 Baowei Liu
CST8177 sed The Stream Editor.
CIRC Winter Boot Camp 2017 Baowei Liu
Lecture 9 Shell Programming – Command substitution
PROGRAMMING THE BASH SHELL PART IV by İlker Korkmaz and Kaya Oğuz
CSE 390a Lecture 7 Regular expressions, egrep, and sed
CSC 352– Unix Programming, Fall 2012
Folks Carelli, Instructor Kutztown University
Guide To UNIX Using Linux Third Edition
Unix Talk #2 grep/egrep/fgrep (maybe add more to this one….)
Unix Talk #2 (sed).
CSE 390a Lecture 7 Regular expressions, egrep, and sed
Regular expressions, egrep, and sed
CSE 303 Concepts and Tools for Software Development
CSCI The UNIX System Regular Expressions
CSE 390a Lecture 7 Regular expressions, egrep, and sed
Presentation transcript:

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 30 17:32:35 EDT 2001

vi and ex are basically the same! Editor was designed to allow input from a line oriented terminal (teletype) Editor has to be switched between input mode and command mode ESC -- I Commands appear at bottom of screen after : For example substitution of text : s/UNIX/Unix/ will replace UNIX by Unix in current line In general :s/pattern/text/ Any other separator besides / can also be used

Rules for search pattern used by other commands vi provides the ‘:’ :s/summer/each &/ –replaces first occurrence of summer in line by each summer :s/pattern/text/g –All occurrences in file are changed :/Unix/s/System/SVR4/g –Searches for a line containing Unix and in this line replaces all occurrences of System with SVR4 :g/Unix/s/System/SVR4/g –Searches for all lines containing Unix and in these lines replaces all occurrences of System with SVR4 :g/Unix/s/System/SVR4/gp –Will display changes as they are made

Rules for search patterns x matches that character, if x is not special \x matches character x, to be used with. * [ ] and ^, can not be used with NEWLINE Special characters. * [ ] \ –^ at beginning of string –$ at end of string ^ matches beginning of line $ matches end of line. matches any single character [string] matches any single character in string [x-z] matches any single character from x to z [^string] matches any character not in string pattern* matches zero or more repetitions of pattern \< matches beginning of a word \> matches end of a word

Examples of search patterns [9] searches for the number 9 \[9\] searches for [9] \.\.\. Searches for three dots … \\\\ Searches for \\ [A-Z] Searches for any capital letter ^Unix Searches for Unix at start of line Unix$ Searches for Unix at end of line [^A-Z] Searches for characters other than capital letters A.C matches AxC with x any character A.*C matches any string, which has an A and later a C, i.e. AC AXC QASDECS

More examples H.. H followed by any two characters H[a-z][a-z] H followed by any two lower case characters ^Harry$Harry on a single line \ now as a complete word, surrounded by blanks or tabs Whitespaces include,,,,,, that is anything that produces a white space on a printer.

More examples for searches in /usr/dict/words ^qu[a-z]*y$ ^now H[a-z]* H[^A-Z] a[a-z]*e[a-z]*i[a-z]*o[a-z]*u a[^e]*e[^i]*i[^o]*o[^u]*u

Special problem ab.*c what is matched in xyabcbbcabbcrt is it xyabcbbcabbcrt or is it xyabcbbcabbcrt The answer is the longest string that matches! To find smallest substring use ab[^c]*c

Searching for Strings grep global regular expression print –(print: Unix jargon for display) g/re/p see search commands for vi. fgrep - fast grep egrep - extended regular expression

Syntax for egrep +one ore more occurrences ?zero or one occurrence * zero or more occurrences ^matches at beginning $ matches at end \ escape character ( ) grouping | or

Examples for egrep "ab+c" matches abc, abbc, … "ab?c" matches only ac and abc "ab*c" matches ac, abc, abbc,… "(a|b)(c|d)" matches ac, ad, bc, bd Other delimiters used by grep, fgrep or egrep: Single quotes, double quotes, or blanks See also related command: look

Character translation tr [-csd] str1 [str2] Options –d delete characters in str1 instead replacing them with characters in str2 –s squeeze repetitions of same character into one –c use complement of str1 (all characters not listed in str1) tr uses stdin and stdout

Special characters allowed by tr \b \f \n \r \t \v \012 any character given in octal :upper:, :lower:

Examples for tr tr "A-Z" "a-z" outputfile tr "[:upper:]" "[:lower:]" tr –s 'a-z' 'A-Z' tr '\012' ':' replace all linefeeds to :

Expansion of tabs tr can translate a tab into a single character, To expand it into several blank spaces use expand or unexpand For more general translations need to use sed stream editor.

Stream editor 1.Read next input line from stdin into buffer. 2.Apply all given editing commands to buffer 3.Write buffer to stdout 4.Go to step 1 Editing commands subset of ex (or vi). sed can do most things that grep can do.

Noninteractive editing sed 's/Unix/UNIX/g' < chapter1 Warning: Do not give command sed 's/Unix/UNIX/g' !chapter1 It will delete the file. Input file can also be given as a parameter: sed 's/Unix/UNIX/g;s/ucb/UCB/g' chapter1

Noninteractive editing continued sed '/^$/d' file removes empty lines sed '/^[ ]*$/d' file removes blank and empty lines sed 's/$/ /' file might be the first attempt to doublespace. First mistake: After sed 's/$/ need to type and UNIX will take that it is time to execute command. To continue command on next line need to quote i.e. sed 's/$/\ /' file

Noninteractive editing corrected Second mistake, now absorbed by quote. Correct version uses script file (option –f) sed -f double file Where script file double holds editing command s/$/\ /

Format of each man page Namename and purpose of command Syntax Descriptionmay be long Options Fileslist of files important for this command Return values Diagnosticspossible error and warning messages Bugsknown errors or short comings See alsorelated information