Chapter 4: UNIX File Processing Input and Output.

Slides:



Advertisements
Similar presentations
UNIX Chapter 12 Redirection and Piping Mr. Mohammad Smirat.
Advertisements

การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation วิบูลย์ วราสิทธิชัย นักวิชาการคอมพิวเตอร์ ศูนย์คอมพิวเตอร์ ม. สงขลานครินทร์ เวอร์ชั่น 1 วันที่
CIS 118 – Intro to UNIX Shells 1. 2 What is a shell? Bourne shell – Developed by Steve Bourne at AT&T Korn shell – Developed by David Korn at AT&T C-shell.
1 CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection slides created by Marty Stepp, modified by Josh Goodwin
Linux+ Guide to Linux Certification, Second Edition
Guide To UNIX Using Linux Third Edition
T UTORIAL OF U NIX C OMMAND & SHELL SCRIPT S 5027 Professor: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2015.
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.
BILKENT UNIVERSITY DEPARTMENT OF COMPUTER TECHNOLOGY AND INFORMATION SYSTEMS CTIS156 INFORMATION TECHNOLOGIES II CHAPTER 10: ADVANCED FILE PROCESSING.
©NIIT Pipes and Filters Lesson 2B / Slide 1 of 28 Introduction to Linux Pre-Assessment Questions 1.Consider the following statements: Statement A: A text.
COMP1070/2002/lec4/H.Melikian COMP1070 Lecture #5  Files and directories in UNIX  Various types of files  File attributes  Notion of pathname  Commands.
Advanced File Processing
Va-scanCopyright 2002, Marchany Unit 6 – Solaris File Security Randy Marchany VA Tech Computing Center.
3 File Processing Mauro Jaskelioff. Introduction More UNIX commands for handling files Regular Expressions and Searching files Redirection and pipes Bash.
Overview of the grep Command Alex Dukhovny CS 265 Spring 2011.
System Programming Regular Expressions Regular Expressions
Lesson 11-Locating, Printing, and Archiving User Files.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
File Processing. Introduction More UNIX commands for handling files Regular Expressions and Searching files Redirection and pipes Bash facilities.
Chapter Four UNIX File Processing. 2 Lesson A Extracting Information from Files.
Guide To UNIX Using Linux Fourth Edition
BIF703 stdin, stdout, stderr Redirection. stdin, stdout, stderr Recall the Unix philosophy “do one thing well”. Unix has over one thousand commands (utilities)
Advanced UNIX Shell Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
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.
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.
Linux+ Guide to Linux Certification, Third Edition
The Shell Chapter 7. Overview The Command Line Standard IO Redirection Pipes Running a Program in the Background Killing (a process!)
Guide to Linux Installation and Administration, 2e1 Chapter 7 The Role of the System Administrator.
Week 3 Exploring Linux Filesystems. Objectives  Understand and navigate the Linux directory structure using relative and absolute pathnames  Describe.
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.
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.
Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections.
I/O and Redirection. Standard I/O u Standard Output (stdout) –default place to which programs write u Standard Input (stdin) –default place from which.
I/O Redirection and Regular Expressions February 9 th, 2004 Class Meeting 4.
Chapter Five Advanced File Processing. 2 Lesson A Selecting, Manipulating, and Formatting Information.
Chapter Four I/O Redirection1 System Programming Shell Operators.
Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.
Getting the most out of the workshop Ask questions!!! Don’t sit next to someone you already know Work with someone with a different skillset and different.
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
Week 9 - Nov 7, Week 9 Agenda I/O redirection I/O redirection pipe pipe tee tee.
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.
ORAFACT Text Processing. ORAFACT Searching Inside Files grep - searches for patterns within files grep [options] [[-e] pattern] filename [...] -n shows.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
1 CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection slides created by Marty Stepp, modified by Jessica Miller & Ruth Anderson
File Processing. Introduction More UNIX commands for handling files Regular Expressions and Searching files Redirection and pipes Bash facilities.
CIRC Summer School 2016 Baowei Liu
Tutorial of Unix Command & shell scriptS 5027
Lesson 5-Exploring Utilities
CIRC Summer School 2017 Baowei Liu
stdin, stdout, stderr Redirection
Unix Operating System (Week Two)
Exploring Shell Commands, Streams, and Redirection
Tutorial of Unix Command & shell scriptS 5027
Tutorial of Unix Command & shell scriptS 5027
CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection
Guide To UNIX Using Linux Third Edition
Tutorial of Unix Command & shell scriptS 5027
Unix Talk #2 (sed).
Chapter Four UNIX File Processing.
Exploring Shell Commands, Streams, and Redirection
Tutorial Unix Command & Makefile CIS 5027
CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection
Lecture 4 Redirecting standard I/O & Pipes
Exploring Shell Commands, Streams, and Redirection
Exploring Shell Commands, Streams, and Redirection
Exploring Shell Commands, Streams, and Redirection
LPI Linux Certification
Presentation transcript:

Chapter 4: UNIX File Processing Input and Output

Extracting Information from Files Section A: Objectives After studying this lesson, you should be able to understand and use: –Redirection –Pipes –Wildcards and Regular Expressions –Commands: find, sort, cat grep

Processing Files When performing commands, UNIX processes data, it receives input from the standard input device (the keyboard) and then sends output to the standard output device (the screen). System administrators and programmers refer to standard input as stdin They refer to standard output as stdout The third standard output is called standard error, or stderr ( also typically the screen ), where error messages are displayed

Using Input and Error Redirection File Redirection operators > - redirect output, send it to a file >> - redirect output, appends it to a file < - redirects input, retrieves input from a file

Redirecting Outputs $ ls > foo Send the directory list to the file named “foo” $ cat foo Show contents of “foo” $ date >> foo Append the date to the file name “foo”

Redirecting Outputs (cont) $ ls -l > bar Send the long directory list to “bar” $ cat foo bar > foobar Combine “foo” and “bar” into a new file name “foobar” $ cat foobar Display contents of new file

Redirecting Inputs $ who > people Put the list of users currently on the system into the file “people” $ sort < people Sort by sending “people” as input to sort $ sort speople Send sorted list to new file “speople”

Using Pipes The pipe operator (|) redirects the output of one command to the input of another command The pipe operator is used in the following manner: first_command | second_command The pipe operator connects the output of the first command with the input of the second command The pipe operator can connect several commands on the same command line, in the following manner: first_command | second_command | third_command...

Pipes Examples $ who > people $ sort < people Is there a shortcut? Yes, using pipes. $ who | sort List of users on the system $ who | wc –l How many users are on the system?

Wildcards The ‘*’ and ‘?’ characters are wildcards, which match against zero or more characters in a file or directory name. ‘*’ matches any # of characters (0 or more). ‘?’ matches exactly one character [ ] and - : character range, a way to specify a sub range of characters to match.

Wildcard Examples $ ls foo*Lists all files starting with “foo” $ ls *.cLists all files ending with “.c” $ ls f.?Lists all files named “f” plus an extra character, such as “f.1” or “f.c” $ ls a* d* f* List all files starting with a, d, f $ ls [adf]*same $ ls [a-z]*List files starting with any letter $ ls [a-z] * List all one letter files, then all files

Wildcard Examples (cont) $ ls *unix* list all files/subdirectories which contain the word “unix” $ ls *s* the following example files will match this pattern. grades.txt, prog1.s, summary.doc $ ls *[0-9._]*files having 0-9 or. or _

Wildcard Examples (cont) $ rm * Delete ALL files $ rm ??? Delete all filenames with 3 characters $ cp *.txt.. Copy all files with a “.txt” extension to the parent directory

Wildcard Examples (cont) Example: to list any file that start with a lowercase letter range from a to z as specified ls -l [a-z]* Which of the following files will this pattern match. ls hw[0-9]* For example: hw1, hw3.c, hw50.txt, hw.

Finding Files The find command searches for files that have a specified name The command has the form: find pathname - name filename The find command prohibits you from searching where you do not have system-level permissions Although Linux does not use it, other UNIX versions require the -print option after the filename to display the names of fields the find command locates

Finding Files Examples find /usr -name Chapter1 -type f -print search through the "/usr" directory for the file named "Chapter1". find /usr -name "Chapter*" -type f -print search through the "/usr" directory for all files that begin with the letters "Chapter". The filename can end with any other combination of characters. This will match filenames such as "Chapter", "Chapter1", "Chapter1.bad", "Chapter_in_life".

Using the Sort Command Use the sort command to sort a file’s contents alphabetically or numerically Here is an example of its use: sort file1 > file2 Here are more complex examples: ls –s | sort –n list files in sorted order ls –s | sort –nr reverse sorted order ls –s | sort –nr | head -5 show 5 largest files

grep grep command is used to match a pattern in one or more files, and displays the matching output on standard output. The format of grep command is: grep [options] regexp [files] grep means global regular expression print.

grep Example: grep luo /etc/passwd: to search a user in the system who called 'luo' as the first name or the last name. Quotes (single or double) are needed if regular expressions are to be used grep olga* will stall grep ‘olga*’ will work fine Wildcards in filename can be used with grep as usual grep ‘olga*’ *.txt

Regular Expressions b*(, b, bb, bbb, bbbb, ….). matches any single character ^ beginning of the line grep ‘^taylor’ file1find pattern ^taylor $end of the line grep ‘:$’ file1entries that ends with : grep ‘100\$’ f1searches for 100$ grep ‘100$’ f1searches for 100 at the end

Regular Expressions [xy]any single character in the set [^xy]any single character not in the set grep ‘^taylor[a-z]’ file1starts with taylor and contains a lower case letter afterwards grep ‘^taylor[^a-z]’ file1 starts with taylor and no lower case letter afterwards grep ‘z*’ f1 zero or more occurrences of z (everything in f1) grep ‘zz*’ f1 one or more occurrences of z (does not return lines without z)

Grep flags -c: print a count of matched lines only -l: list filenames that contain the specified pattern -i: Ignore the case of the letters in the pattern -n: Including line numbers -b: precede each line with its block number -h: print matched lines but not filenames(inverse of -l) -s: Suppress error messages for nonexistent of unreadable -v: Print all lines that don't match regexp

grep Example 1: to indicate how many users on the system has a last name or first name as "marylin" ignoring the upper or lower cases. grep -ci marylin /etc/passwd Example2: to indicate how may case- sensitive matches are in the file before any of them are displayed on the screen grep -c lu /etc/passwd Example3: to number the output lines. grep -n lu /etc/passwd

Chapter Summary Output from a command may be redirected from stdout to a disk file Input to a command may be redirected from stdin to a disk file Use the sort command to sort a file’s contents alphabetically or numerically A pipe directs the standard output of one Unix command into the standard input of another Unix command. A wildcard is a tool that allow you to specify a group of filenames or directories. Useful commands such as find, grep, cat,