CISC3130: Review of Commands

Slides:



Advertisements
Similar presentations
CST8177 awk. The awk program is not named after the sea-bird (that's auk), nor is it a cry from a parrot (awwwk!). It's the initials of the authors, Aho,
Advertisements

Learning Unix/Linux Bioinformatics Orientation 2008 Eric Bishop.
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
T UTORIAL OF U NIX C OMMAND & SHELL SCRIPT S 5027 Professor: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2015.
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.
7/17/2009 rwjBROOKDALE COMMUNITY COLLEGE1 Unix Comp-145 C HAPTER 2.
CS 141 Labs are mandatory. Attendance will be taken in each lab. Make account on moodle. Projects will be submitted via moodle.
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.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
Chapter Four UNIX File Processing. 2 Lesson A Extracting Information from Files.
Guide To UNIX Using Linux Fourth Edition
LIN 6932 Unix Lecture 6 Hana Filip. LIN 6932 HW6 - Part II solutions posted on my website see syllabus.
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.
CISC3130, Spring 2013 Xiaolan Zhang 1 Chapter 3: Searching/Substitution: regular expression 1.
Linux+ Guide to Linux Certification Chapter Four Exploring Linux Filesystems.
Linux+ Guide to Linux Certification, Third Edition
CS 6560 Operating System Design Lecture 3:Tour of GNU/Linux.
INTRODUCTION TO LINUX Jacob Chan. GNU/Linux Consists of Linux kernel, GNU utilities, and open source and commercial applications Works like Unix –Multi-user.
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.
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
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.
Agenda Regular Expressions (Appendix A in Text) –Definition / Purpose –Commands that Use Regular Expressions –Using Regular Expressions –Using the Replacement.
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.
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.
UNIX An Introduction. Brief History UNIX UNIX Created at Bell Labs, 1969 Created at Bell Labs, 1969 BSD during mid 70s BSD during mid 70s AT&T began offering.
40 Years and Still Rocking the Terminal!
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
Sed. Class Issues vSphere Issues – root only until lab 3.
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.
Lesson 6-Using Utilities to Accomplish Complex Tasks.
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.
Learning Unix/Linux Based on slides from: Eric Bishop.
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.
Tutorial of Unix Command & shell scriptS 5027
Lesson 5-Exploring Utilities
Prepared by: Eng. Maryam Adel Abdel-Hady
The UNIX Shell Learning Objectives:
Some Linux Commands.
Chapter 6 Filters.
Shell Script Assignment 1.
INTRODUCTION TO UNIX: The Shell Command Interface
Tutorial of Unix Command & shell scriptS 5027
Introduction to UNIX.
Tutorial of Unix Command & shell scriptS 5027
Folks Carelli, Instructor Kutztown University
Guide To UNIX Using Linux Third Edition
CSC 352– Unix Programming, Spring 2016
Tutorial of Unix Command & shell scriptS 5027
Unix Talk #2 (sed).
Chapter Four UNIX File Processing.
Linux Shell Script Programming
CSE 303 Concepts and Tools for Software Development
Introduction to Bash Programming, part 3
Review.
Presentation transcript:

CISC3130: Review of Commands A review Spring 2011

Review: File systems Absolute pathname, relative pathname Commands: cd, pwd, mkdir, rmdir, rm, mv File examination: cat, head, tail, more, less, wc, file File manipulation: chmod, editors (vi, emacs, pico) Directory exploration: ls Understand the long listing format Different types of file Regular file: Directory: Links: … Understand permission setting of file/directory Compare text files files: diff, comp

Review: system info & misc /etc/passwd: stores user accounts in the system Different fields separated by : Understand the meaning of the fields who, who am i: list the users currently logged on ps: list processes running in the system date: current date sleep: resume executing next command after delaying for specified amount of time

Shell related commands chsh: change default shell to use Startup configuration files for bash .bash_profile, .bash_rc To find out the name of the shell that you are using: From command line, type echo $0 Shell built-in commands echo set alias read

Filters commands For each command: Common behavior: Understand the basic functionalities of the following commands Know how to read online manual for more info. Common behavior: If no files are specified in command line, process standard input Output is generally written to standard output Benefits of such design?

filters sort cut, paste comp, diff uniq tr grep family

grep grep - uses regular expressions for pattern matching fgrep - file grep, does not use regular expressions, only matches fixed strings but can get search strings from a file egrep - extended grep, uses a more powerful set of regular expressions but does not support backreferencing, generally fastest

Hints on bash scripting Start with requirement, for example: “Write a script that reports the number of processes run by each of the users that are currently online”. Identify sub-tasks: Find out all users that are currently online For a given user, find out the number of processes belonging to him/her Compose solution using solution to sub-task for user in ## all users currently online do ## find out the number of processes run by $user done

Solve sub-task #1: users online Search for candidates … Is there a command that fits this job description exactly? Well, command who almost fits… Does the output of the command directly usable ? No Try the command out … How to process the output of the command so that it can be used ? How to select only the first field of output ? Test and confirm your solution: who | cut –d ‘ ‘ –f 1

Solve subtask #2: number of processes Task #2: to find out # of processed by a user $user No command fits this requirement directly… But command ps can give us all processes in the system … The output of ps also tells us the user each process belongs to Command wc can be used to count # of processes, as ps displays one process per line Here, we are collecting info (using ps), and processing the info in multiple steps using multiple commands Now you try to put them together …

Final solution

Another example: Requirement: “ For all regular files under a directory given in command line, displays what kind of file it is (using command file)”. Sub-tasks: Find all regular files under current directory Psedocode: dir=$1 for filename in ##list of all regular file under $dir do file $filename done

Alternatively Psedocode: dir=$1 for filename in ##the list of all files under $dir do ## if the file is regular file file $filename done

Solving subtask Find all files under a directory dir ls $dir So we have dir=$1 for filename in ls $dir do ## if the file is regular file file $filename done Any problem ?

Sed: Stream-oriented, Non-Interactive, Text Editor Look for patterns one line at a time, like grep Change lines of the file Non-interactive text editor Editing commands come in as script There is an interactive editor ed which accepts the same commands A Unix filter Superset of previously mentioned tools http://www.grymoire.com/Unix/Sed.html#uh-0

Two power filters sed and awk

Introduction to sed: substitution Substitute “command”: s changes all occurrences of a regular expression into a new value. to change "day" in file old to "night" in the "new" file: sed s/day/night/ <old >new Generally: replace occurrence of a pattern in standard input, with a given string, and display result in standard output sed s/regular expression/replace string/

Introduction to sed: substitution If you have meta-characters in the command, quotes are necessary sed 's/3.1415[0-9]*/PI/' <old >new To mark a matching pattern grep –n count mylab1.cpp | sed s/count/<count>/

Delimiter One can use any letter to delimit different parts of command s If delimiter appears in regular expr or replace str, escape them To change /usr/local/bin to /common/bin: sed 's/\/usr\/local\/bin/\/common\/bin/' <old >new It is easier to read if you use other letter as a delimiter: sed 's_/usr/local/bin_/common/bin_' <old >new sed 's:/usr/local/bin:/common/bin:' <old >new sed 's|/usr/local/bin|/common/bin|' <old >new

How sed works? sed, like most Unix utilties, read a line at a time By default, sed command applies to the first occurrence of the pattern in a line. [zhang@storm ~]$ sed 's/aa*/bb/' ab ab bbb ab To apply to every occurrence, use option g (global) sed 's/aa*/bb/g‚

How sed works: aggressive matching sed finds the longest string in the line that matches the pattern, and substitute it with the replacing string Pattern aa* matches with 1 or more a’s [zhang@storm ~]$ sed 's/aa*/bb/' aaab bbb

Substitution with referencing How to mark all numbers (integers or floating points) using angled brackets? E.g., 28 replaced by <28>, 3.1415 replaced by <3.1415> Use special character "&“, which refer to string that matches the pattern (similar to backreference in grep.) sed 's/[0-9][0-9]*\.[0-9]*/(&)/g' You can have any number of "&" in replacement string. You could also double a pattern, e.g. the first number of a line: $echo "123 abc" | sed 's/[0-9]*/& &/' 123 123 abc

Multiple commands To combine multiple commands, use -e before each command: sed -e 's/a/A/' -e 's/b/B/' <old >new If you have a large number of sed commands, you can put them into a file, say named as sedscript # sed comment - This script changes lower case vowels to upper case s/a/A/g s/e/E/g s/i/I/g s/o/O/g s/u/U/g each command must be on a separate line. Invoke sed with a script: sed -f sedscript <file.txt >file_cap.txt

sed interpreter script Alternatively, starts the script file (named CapVowel) with #!/bin/sed -f s/a/A/g s/e/E/g s/i/I/g s/o/O/g s/u/U/g and make the file executable Then you can evoke it directly: CapVowel <old >new

Restrict operations Restrict commands to certain lines Specifying a line by its number. sed '3 s/[0-9][0-9]*//' <file >new Specifying a range of lines by number. sed '1,100 s/A/a/' All lines containing a pattern. To delete first number on all lines that start with a "#," use: sed '/^#/ s/[0-9][0-9]*//' Many other ways to restrict

Command d A useful command deletes every line that matches the restriction: "d.“ To look at the first 10 lines of a file, you can use: sed '11,$ d' <file i.e., delete from line 11 to the end of the file If you want to chop off the header of a mail message, which is everything up to the first blank line, use: sed '1,/^$/ d' <file

Command q Ex: another way to duplicate the head command is: This command is most useful when you wish to abort the editing after some condition is reached. Ex: another way to duplicate the head command is: sed '11 q' which quits when the eleventh line is reached.

Backreference To keep first word of a line, and delete the rest of the line, mark first word with the parenthesis: sed 's/\([a-z]*\).*/\1/' Recall: regular exprssions are greedy, and try to match as much as possible. "[a-z]*" matches zero or more lower case letters, and tries to be as big as possible. ".*" matches zero or more characters after the first match. Since the first one grabs all of the lower case letters, the second matches anything else. Ex: $echo abcd123 | sed 's/\([a-z]*\).*/\1/' abcd

Backreference (cont’d) If you want to switch two words around, you can remember two patterns and change the order around: sed 's/\([a-z][a-z]*\) \([a-z][a-z]*\)/\2 \1/’ To eliminate duplicated words: sed 's/\([a-z]*\) \1/\1/' If you want to detect duplicated words, you can use sed -n '/\([a-z][a-z]*\) \1/p’ Numeric value can have up to nine values: "\1" thru "\9.“ To reverse first three characters on a line, you can use sed 's/^\(.\)\(.\)\(.\)/\3\2\1/'

Sed commands & scripts Each sed command consists of up to two addresses and an action, where the address can be a regular expression or line number. A script is nothing more than a file of commands address action command address action address action address action address action script

sed: a conceptual overview All editing commands in a sed script are applied in order to each input line. If a command changes input, subsequent command address will be applied to current (modified) line in the pattern space, not original input line. Original input file is unchanged (sed is a filter), and the results are sent to standard output (but can be redirected to a file).

Why awk? It is an excellent filter and report writer. Many UNIX utilities generates rows and columns of information. AWK is an excellent tool for processing these rows and columns, and is easier to use AWK than most conventional programming languages. A. Aho, B. W. Kernighan and P. Weinberger.

Features of awk a pseudo-C interpretor, it understands same arithmetic operators as C string manipulation functions search for particular strings and modify output. AWK also has associative arrays, which are incredible useful, and is a feature most computing languages lack. Associative arrays can make a complex problem a trivial exercise.