CS160A EXERCISES-FILTERS2 Boyd

Similar documents
- c list The list specifies character positions.

Part III. Shell Config. Tobias Neckel: Scripting with Bash and Python Compact Max-Planck, February 16-26,

Linux Text Utilities 101 for S/390 Wizards SHARE Session 9220/5522

2) clear :- It clears the terminal screen. Syntax :- clear

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

5/20/2007. Touring Essential Programs

Module 8 Pipes, Redirection and REGEX

Regular Expressions. Todd Kelley CST8207 Todd Kelley 1

Unix as a Platform Exercises + Solutions. Course Code: OS 01 UNXPLAT

UNIX files searching, and other interrogation techniques

Week Overview. Simple filter commands: head, tail, cut, sort, tr, wc grep utility stdin, stdout, stderr Redirection and piping /dev/null file

CS Unix Tools. Fall 2010 Lecture 5. Hussam Abu-Libdeh based on slides by David Slater. September 17, 2010

Midterm 1 1 /8 2 /9 3 /9 4 /12 5 /10. Faculty of Computer Science. Term: Fall 2018 (Sep4-Dec4) Student ID Information. Grade Table Question Score

Linux & Shell Programming 2014

Principles of Bioinformatics. BIO540/STA569/CSI660 Fall 2010

Unix as a Platform Exercises. Course Code: OS-01-UNXPLAT

More text file manipulation: sorting, cutting, pasting, joining, subsetting,

Introduction To Linux. Rob Thomas - ACRC

Reading and manipulating files

CENG 334 Computer Networks. Laboratory I Linux Tutorial

CS Unix Tools & Scripting

This lab exercise is to be submitted at the end of the lab session! passwd [That is the command to change your current password to a new one]

ITST Searching, Extracting & Archiving Data

CS Advanced Unix Tools & Scripting

5/8/2012. Exploring Utilities Chapter 5

ADVANCED LINUX SYSTEM ADMINISTRATION

UNIX / LINUX - REGULAR EXPRESSIONS WITH SED

Lab Working with Linux Command Line

CS 124/LINGUIST 180 From Languages to Information. Unix for Poets Dan Jurafsky

Basic Unix Command. It is used to see the manual of the various command. It helps in selecting the correct options

WRITE COMMANDS USING sed or grep (1 mark each) 2014 oct./nov march/april Commands using grep or egrep. (1 mark each)

CSE 390a Lecture 2. Exploring Shell Commands, Streams, and Redirection

Regular Expressions. Michael Wrzaczek Dept of Biosciences, Plant Biology Viikki Plant Science Centre (ViPS) University of Helsinki, Finland

Basic Linux Commands. Srihari Kalgi M.Tech, CSE (KReSIT), IIT Bombay. May 5, 2009

CSE 390a Lecture 2. Exploring Shell Commands, Streams, Redirection, and Processes

Shell Programming Overview

QUESTION BANK ON UNIX & SHELL PROGRAMMING-502 (CORE PAPER-2)

Paolo Santinelli Sistemi e Reti. Regular expressions. Regular expressions aim to facilitate the solution of text manipulation problems

22-Sep CSCI 2132 Software Development Lecture 8: Shells, Processes, and Job Control. Faculty of Computer Science, Dalhousie University

Essential Linux Shell Commands

CPSC 217 Midterm (Python 3 version)

Set 1 MCQ Which command is used to sort the lines of data in a file in reverse order A) sort B) sh C) st D) sort -r

CST Lab #5. Student Name: Student Number: Lab section:

Multiple Choice - 58 Questions - 10 of 15%

UNIX ASSIGNMENT 1 TYBCA (Sem:V)

CSE 303 Lecture 2. Introduction to bash shell. read Linux Pocket Guide pp , 58-59, 60, 65-70, 71-72, 77-80

On successful completion of the course, the students will be able to attain CO: Experiment linked. 2 to 4. 5 to 8. 9 to 12.

CS 25200: Systems Programming. Lecture 11: *nix Commands and Shell Internals

Introduction p. 1 Who Should Read This Book? p. 1 What You Need to Know Before Reading This Book p. 2 How This Book Is Organized p.

Chapter 8: Security under Linux

Introduction to Linux

Unix Guide. Meher Krishna Patel. Created on : Octorber, 2017 Last updated : December, More documents are freely available at PythonDSP

ACS Unix (Winter Term, ) Page 51

Basics. I think that the later is better.

Understanding bash. Prof. Chris GauthierDickey COMP 2400, Fall 2008

18-Sep CSCI 2132 Software Development Lecture 6: Links and Inodes. Faculty of Computer Science, Dalhousie University. Lecture 6 p.

Appendix B WORKSHOP. SYS-ED/ Computer Education Techniques, Inc.

D. Delete the /var/lib/slocate/slocate.db file because it buffers all search results.

CS214-AdvancedUNIX. Lecture 2 Basic commands and regular expressions. Ymir Vigfusson. CS214 p.1

Part I. Introduction to Linux

Unix L555. Dept. of Linguistics, Indiana University Fall Unix. Unix. Directories. Files. Useful Commands. Permissions. tar.

Introduction to the UNIX command line

CSE II-Sem)

Introduction to Linux (and the terminal)

Bashed One Too Many Times. Features of the Bash Shell St. Louis Unix Users Group Jeff Muse, Jan 14, 2009

UNIX, GNU/Linux and simple tools for data manipulation

bash, part 3 Chris GauthierDickey

Unleashing the Shell Hands-On UNIX System Administration DeCal Week 6 28 February 2011

Exploring UNIX: Session 3

CSE 374 Midterm Exam Sample Solution 2/6/12

Chapter-3. Introduction to Unix: Fundamental Commands

Overview. Unix/Regex Lab. 1. Setup & Unix review. 2. Count words in a text. 3. Sort a list of words in various ways. 4.

Regex, Sed, Awk. Arindam Fadikar. December 12, 2017

Introduction to Linux

Introduction to Scripting using bash

Lecture 4. Log into Linux Reminder: Homework 1 due today, 4:30pm Homework 2 out, due next Tuesday Project 1 out, due next Thursday Questions?

PESIT Bangalore South Campus

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

Shells and Shell Programming

CS 4218 Software Testing and Debugging Ack: Tan Shin Hwei for project description formulation

When talking about how to launch commands and other things that is to be typed into the terminal, the following syntax is used:

File Commands. Objectives

Deep Dive: Pronto Transformations Reference

Operating Systems, Unix Files and Commands SEEM

CSCE 212H, Spring 2008, Matthews Lab Assignment 1: Representation of Integers Assigned: January 17 Due: January 22

The UNIX Shells. Computer Center, CS, NCTU. How shell works. Unix shells. Fetch command Analyze Execute

Shells and Shell Programming

Contents. Note: pay attention to where you are. Note: Plaintext version. Note: pay attention to where you are... 1 Note: Plaintext version...

N I X S U P P L E M E N T

No Food or Drink in this room. Logon to Windows machine

STATS Data Analysis using Python. Lecture 15: Advanced Command Line

d. Permissions 600 on directory dir and 300 on file dir/foo. c. Permissions 700 on directory dir and 200 on file dir/foo.

COMP 4/6262: Programming UNIX

Unix Introduction to UNIX

The Unix Environment for Programming (COMP433)

CST8207: GNU/Linux Operating Systems I Lab Ten Boot Process and GRUB. Boot Process and GRUB

CSE2031. Lab 2 FALL 2009

Please note that CNE 216 is a brand new course that has never been taught on the George campus; it will be taught for the first time in the fall of

Wildcards and Regular Expressions

Operating Systems and Using Linux. Topics What is an Operating System? Linux Overview Frequently Used Linux Commands

Transcription:

Exercises-Filters2 In this exercise we will practice with the Unix filters cut, and tr. We will also practice using paste, even though, strictly speaking, it is not a filter. In addition, we will expand our use of grep to add the options -c, -v, -e, -i, -n, and -l and the use of simple regular expressions. Last we will do a handful of real-life problems to put these tools together to do real work. There is a lot of work in this exercise set. Do enough of the problems to master a filter, then move on to the next one. Do not ignore the final part where all the filters are used together. This is the most important part. For the duration of these exercises, you will be using test files from the directory samples/data beneath the public data area on hills. You will also need to create temporary files from time to time, so you can either get personal copies of the files you need and work in your own area, or you can work in the Data directory and place your temporary files in your home directory. This is what the answer key does. Instead of data files, some of the problems use the output of various Linux commands as input to work on. Part One - cut/paste Description The filter cut slices its input vertically either by character position or using a delimiter and a field number. cut cannot rearrange fields. If you want to generate some output that takes fields #3 and #5 of one file and displays them as field #5 followed by field #3 you must cut the fields out individually and then use paste to paste them together. If you are using fields, the default delimiter for both cut and paste is a tab. Review your class notes and text for the syntax of the cut and paste commands, or look at their manual pages. Unless otherwise directed, you should just display the results of your commands on the screen. Exercises 1. Look at the output of the date command. Using cut and column positions, create a file that contains the month and another that contains the day of the month. Then output the date as day month with a space between the day and the month. Look at the file samples/data/hired_data beneath the class public directory. Its format is Name:Dept:Job Then use a command to output 2. the Names only 3. the Names and Jobs, separated by a : 4. the Jobs then Names, separated by a : (this takes several commands) 5. Look at the file Emp_Data. Output the Names field as First Last rather than Last, First 6. Look at the files st2 and st3. Note that they are different lengths. Output a list that has the first field of st3 followed by the third field of st2, keeping the same delimiter. How was the difference in length resolved? Part Two - tr Description The tr command translates, deletes, complements and squeezes characters. We will cover all of these except complement. (Complement is very useful, especially in conjunction with the other options, however. Try the following command on a text file: cat file tr -cs '[[:alnum:]]' '[\012*]' ) tr needs one or two character strings to specify the characters that it is to translate. A shorthand to enumerating these characters is to use a character set. Thus, the character string 'abcde' could be Exercises-Filters2 CS160A Page 1 of 5

specified as '[a-e]'. Just like wildcards, you can also use a character class in a character set. Character sets (and classes) are part of modern regular expressions, although there are a few minor differences we will discuss later. Here are the classes: class meaning class meaning upper any uppercase character alpha any alphabetic character lower any lowercase character digit any number alnum any alphabetic or digit punct any punctuation character space any whitespace character ascii any ASCII character print any printable character xdigit any hexadecimal digit Examples: tr -d '[[:alpha:][:punct:]]' - The set contains all characters that are members of either the class alpha or punct. (Delete all control and punctuation characters) tr '[[:upper:]]' '[[:lower:]]' - lowercase all uppercase characters. One more shortcut is helpful: to extend the size of the second set to match the first by duplicating the last element, simply put the last string in a character set and follow it by * tr 'aeiou' '[-*]' - replace all vowels by dashes Exercises 1. Using echo, send tr the string "UPPER lower", telling it to delete all blanks. (Those are blanks between UPPER and lower) 2. Redo the last command, telling tr to squeeze out repeated blanks. 3. Tell it to squeeze out repeated blanks and change those remaining to pound symbols (#) 4. Last, squeeze out leading blanks, then translate uppercase characters to lowercase characters. (this is making use of the fact that tr can translate a sequence of characters to another sequence using a 1-for-1 substitution) 5. Examine the file sorttest. Output the file on the screen after changing the # delimiter to a comma. Look at an ls -l listing of a directory. You want to cut out the size and name field, but cut relies on a single delimiter between fields. Let's design this solution: 6. take the ls -l output and squeeze successive blanks to a single one. 7. now add cut so that only size and name is output. Part Three - grep This part covers the grep command, adding options -e -n and -l The later ones use simple regular expressions. The files can be found in the directory samples/data beneath the public data area. Using grep only, display the lines in the file u2 that 1. start with cow 2. start with the word It 3. contain exactly (consist of) cow 4. contain either cow or animal 5. contain both cow and animal, anywhere on the line. 6. output the lines in u2 that start with cow with the line number in the file 7. output the number of lines in u2 that contain cow 8. output the names of the files in the Data directory that contain cow Exercises-Filters2 CS160A Page 2 of 5

9. output the number of lines in each file in Data that contain cow 10.output the number of lines in u2 that don't contain cow 11.output the lines of the passwd file whose shell field (the last field) is /usr/bin/bash 12.output the lines of passwd whose shell field is neither /usr/bin/bash nor /usr/bin/ksh 13.output the lines of passwd whose login field (the first field) is three characters long. 14.output the lines of st.bad that have an empty field Part Four This last section has problems that put all the filters together to solve real-world problems. Some of them are challenging and are good sources of questions for the Google Group. Note that we are only using simple regular expressions in this exercise set, so you must do a bit more work on some of the problems. 1. Output the login (field #1) of all entries in passwd whose default group (field 4) is 200. 2. Output a list of the different shells used in the file passwd (the last field) 3. how many different default groups (field #4) are there in passwd? 4. How many members (members are in field #4, separated by commas) are there in the line in the group file whose group id (field #3) is 3021? 5. Create a file (in your home directory) named E14dept with only the names (field 2) of each person in the file Emp_Manager1 whose dept field (field 3) is E14. The list should be sorted by the person's id number (field 1) 6. Student accounts on hills have the default group of 506. Output the number of lines of /etc/passwd whose default group (field 4) is not 506 (i.e., who are not students). Compare this to the number of students in the file. Any predictions? In the directory /pub/cs/gboyd/cs160a/filters2 there is a set of dummy files and directories. Use it for the following problems: 7. List the names of all objects that are readable by group. 8. List the names of the files (only) that are both readable by group and by other. 9. List the names of the directories (only) that are not executable by other. Answers Part One 1. date cut -c5-7 > ~/mon date cut -c9-10 > ~/day paste -d' ' ~/day ~/mon rm ~/day ~/mon 2. cut -d: -f1 Hired_Data 3. cut -d: -f1,3 Hired_Data 4. cut -d: -f1 Hired_Data > ~/names cut -d: -f3 Hired_Data >~/jobs paste -d: ~/jobs ~/names rm ~/jobs ~/names 5. cut -d, -f1 Emp_Data > ~/last cut -d: -f1 Emp_Data cut -d, -f2 > ~/first paste -d' ' ~/first ~/last rm ~/first ~/last 6. cut -d# -f1 st3 > ~/1 Exercises-Filters2 CS160A Page 3 of 5

cut -d# -f3 st2 > ~/3 paste -d# ~/1 ~/3 rm ~/[13] Part Two 1. echo "UPPER lower" tr -d ' ' 2. echo "UPPER lower" tr -s ' ' 3. echo "UPPER lower" tr -s ' ' tr ' ' '#' 4. echo "UPPER lower" tr -s ' ' tr '[[:upper:]]' '[[:lower:]]' 5. tr '#' ',' < sorttest 6. ls -l tr -s ' ' 7. ls -l tr -s ' ' cut -d' ' -f5,9- (the trailing - gets filenames with blanks but has a problem with symlinks) Part Three 1. grep "^cow" u2 2. grep "^It" u2 3. grep "^cow$" u2 4. grep -e "cow" -e "animal" u2 5. grep "cow" u2 grep "animal" 6. grep -n "^cow" u2 7. grep -c "cow" u2 8. grep -l "cow" * 9. grep -c "cow" * 10. grep -cv "cow" u2 11. grep ':/usr/bin/bash$' passwd 12. grep -v -e ':/usr/bin/bash$' -e ':/usr/bin/ksh' passwd 13. grep '^...:' passwd 14. There are three possibilities for an empty field: the first field (^#), the last field(#$), or a middle field(##): grep -e '^#' -e '#$' -e '##' st.bad Part Four 1. To do this with simple REs, you first must remove all non-pertinent information. All we need to answer the question is the default group and the login. Then look for the correct group, and extract the logins: cut -d: -f1,4 passwd grep ':200$' cut -d: -f1 2. cut -d: -f7 passwd sort -u 3. cut -d: -f4 passwd sort -u wc -l 4. cut -d: -f3,4 group grep '^3021:' cut -d: -f2 tr ',' '\n' wc -l 5. sort -k1,1n Emp_Manager1 cut -d: -f2,3 grep ':E14$' cut -d: -f1 > ~/E14dept 6. cut -d: -f4 /etc/passwd grep -c '^506$' compare this to wc -l /etc/passwd. At the time of this writing (end of summer), there were about 8900 students and 200 non-students Exercises-Filters2 CS160A Page 4 of 5

The tricky part of these last ones is preparing the data. You only need the permissions fields and the names field, but to cut them out you must fix the delimiters by sqeezing out extra spaces like this: ls -l tr -s ' ' cut -d' ' -f1,9- Note that we added 9 through the end in case a filename contained spaces. All of the commands below have this sequence as part: 7. ls -l tr -s ' ' cut -d' ' -f1,9- grep '^...r' cut -d' ' -f2-8. ls -l tr -s ' ' cut -d' ' -f1,9- grep '^-...r..r' cut -d' ' -f2-9. ls -l grep '^d' tr -s ' ' cut -d' ' -f1,9- grep -v '^...x' cut -d' ' -f2- Exercises-Filters2 CS160A Page 5 of 5