Filtering Tools. Filtering Tools and a Bit More About I/O Redirection

Similar documents
grep, awk and sed three VERY useful command-line utilities Matt Probert, Uni of York grep = global regular expression print

Thirty Useful Unix Commands

Hands-On UNIX Exercise:

Cisco Networking Academy Program Curriculum Scope & Sequence. Fundamentals of UNIX version 2.0 (July, 2002)

INASP: Effective Network Management Workshops

PHP Debugging. Draft: March 19, Christopher Vickery

USEFUL UNIX COMMANDS

SEO - Access Logs After Excel Fails...

Tutorial 0A Programming on the command line

A Crash Course on UNIX

SerialMailer Manual. For SerialMailer 7.2. Copyright Falko Axmann. All rights reserved.

Lecture 4. Regular Expressions grep and sed intro

Unix Sampler. PEOPLE whoami id who

The Linux Operating System and Linux-Related Issues

HP-UX Essentials and Shell Programming Course Summary

Unix the Bare Minimum

Basic C Shell. helpdesk@stat.rice.edu. 11th August 2003

AN INTRODUCTION TO UNIX

Unix Shell Scripts. Contents. 1 Introduction. Norman Matloff. July 30, Introduction 1. 2 Invoking Shell Scripts 2

CS10110 Introduction to personal computer equipment

Source Code Management/Version Control

Command Line Crash Course For Unix

Unix Scripts and Job Scheduling

Command Line - Part 1

ICS 351: Today's plan

Introduction to Shell Scripting

Tutorial Guide to the IS Unix Service

Monitoring a Linux Mail Server

Running your first Linux Program

Fred Hantelmann LINUX. Start-up Guide. A self-contained introduction. With 57 Figures. Springer

UNIX - Command-Line Survival Guide

UNIX, Shell Scripting and Perl Introduction

Shell Scripts (1) For example: #!/bin/sh If they do not, the user's current shell will be used. Any Unix command can go in a shell script

Learning Objective. Purpose The purpose of this activity is to give you the opportunity to learn how to set up a database and upload data.

Copyright Texthelp Limited All rights reserved. No part of this publication may be reproduced, transmitted, transcribed, stored in a retrieval

File Transfer Examples. Running commands on other computers and transferring files between computers

PROGRAMMING FOR BIOLOGISTS. BIOL 6297 Monday, Wednesday 10 am -12 pm

Lab 1: Introduction to C, ASCII ART and the Linux Command Line Environment

L01 Introduction to the Unix OS

Secure Shell Demon setup under Windows XP / Windows Server 2003

Security Correlation Server Quick Installation Guide

Introduction to Big Data Analysis for Scientists and Engineers

CS Unix Tools & Scripting Lecture 9 Shell Scripting

Using SVN to Manage Source RTL

Lecture 18 Regular Expressions

Linux command line. An introduction to the Linux command line for genomics. Susan Fairley

Introduction to Operating Systems

CLC Server Command Line Tools USER MANUAL

Top 10 Things to Know about WRDS

Answers to Even- Numbered Exercises

Introduction to UNIX and SFTP

Adding a DNS Update Step to a Recovery Plan VMware vcenter Site Recovery Manager 4.0 and later

An A-Z Index of the Apple OS X command line (TERMINAL) The tcsh command shell of Darwin (the open source core of OSX)

How to use the UNIX commands for incident handling. June 12, 2013 Koichiro (Sparky) Komiyama Sam Sasaki JPCERT Coordination Center, Japan

3.GETTING STARTED WITH ORACLE8i

Open Source Computational Fluid Dynamics

Introduction to Shell Programming

Encoding Text with a Small Alphabet

Hadoop Hands-On Exercises

University of Pennsylvania Department of Electrical and Systems Engineering Digital Audio Basics

Forensic Analysis of Internet Explorer Activity Files

Hypercosm. Studio.

Bitrix Site Manager 4.0. Quick Start Guide to Newsletters and Subscriptions

Version Control Using Subversion. Version Control Using Subversion 1 / 27

A UNIX/Linux in a nutshell

Chapter 4: Computer Codes

My name is Robert Comella. I am a SANS Technology Institute (STI) student who is nearly finished with my master of science in engineering degree.

Lab 2: Secure Network Administration Principles - Log Analysis

AWK: The Duct Tape of Computer Science Research. Tim Sherwood UC Santa Barbara

Prescribed Specialised Services 2015/16 Shadow Monitoring Tool

2: Entering Data. Open SPSS and follow along as your read this description.

Manual Prepared by GalaxyVisions Customer Care Team

Configuring Keystone in OpenStack (Essex)

Make a folder named Lab3. We will be using Unix redirection commands to create several output files in that folder.

WebSphere Application Server security auditing

Using the JNIOR with the GDC Digital Cinema Server. Last Updated November 30, 2012

Lecture 4: Writing shell scripts

Extending Remote Desktop for Large Installations. Distributed Package Installs

ez Service Portal User Guide version 2.5.1

Cisco Setting Up PIX Syslog

SECTION 5: Finalizing Your Workbook

10. Exercise: Automation in Incident Handling

Chapter 24: Creating Reports and Extracting Data

Regular Expressions. In This Appendix

NØGSG DMR Contact Manager

IBM Emulation Mode Printer Commands

PHI Audit Us er Guide

Network Security In Linux: Scanning and Hacking

SPSS: Getting Started. For Windows

EE984 Laboratory Experiment 2: Protocol Analysis

This presentation explains how to monitor memory consumption of DataStage processes during run time.

Mastering Mail Merge. 2 Parts to a Mail Merge. Mail Merge Mailings Ribbon. Mailings Create Envelopes or Labels

Networks. Inter-process Communication. Pipes. Inter-process Communication

Hodor and Bran - Job Scheduling and PBS Scripts

Symantec Critical System Protection Agent Guide

Useless Use of * Slide 1. Useless Use of * Jan Schaumann jschauma@netmeister.org PGP: 136D 027F DC B42 47D6 7C5B 64AF AF22 6A4C

Security Correlation Server Quick Installation Guide

PharmaSUG Paper AD11

LECTURE-7. Introduction to DOS. Introduction to UNIX/LINUX OS. Introduction to Windows. Topics:

Eventia Log Parsing Editor 1.0 Administration Guide

Transcription:

Filtering Tools Filtering Tools and a Bit More About I/O Redirection Copyright 2006 2009 Stewart Weiss

Input redirection operator < The input redirection operator '<', replaces the standard input of a command by the file that follows it. It is not very useful for many commands because many commands let you put the names of files on the command line as their arguments, so there is no need to use this operator for most commands. cat < myfile for example, is the same as cat myfile 2 CSci 132 Practical UNIX with Perl

Other redirection operators There are two other redirection operators, '<< ' and '>>'. We will start with '>>': The output append operator, >>, which you saw in an earlier lesson, forces standard output to be appended to a file instead of replacing the file's contents. This is very useful; it gives you the means to add new lines to files. 3 CSci 132 Practical UNIX with Perl

An example using >> To illustrate, suppose that story is a file with a single line of text as shown below. We'll use >> to lengthen the story. $ cat story Once upon a time $ echo "there was no UNIX." >> story $ cat story Once upon a time there was no UNIX. $ You can see that the output of echo was added as a new line in story, instead of replacing its contents. 4 CSci 132 Practical UNIX with Perl

Another example of >> There are many system programs that maintain log files, i.e., files that record the program's activities. Many of these programs are shell scripts. When they need to append information to their log files, they use the append operator. I could create a log file to record whenever I started a bash shell by adding this to my.bashrc file: echo "bash started on `tty` at `date`" >> ~/.mylog This would add a line to the file.mylog in my home directory each time I ran a bash shell. The expression `...` is explained next. 5 CSci 132 Practical UNIX with Perl

And another example I keep track of my own logins by adding the following line to my.bash_profile: echo "Login into `hostname` on `date`" >>.loghist The.bash_profile file is executed only on logins, not when a non-login shell runs, so this does not add a line each time I start a shell. This line appends the name of the host and the time and date of login to my.loghist file. The backquoted expressions `hostname` and `date` cause the hostname and date commands to run and their output to be substituted for their backquoted names. 6 CSci 132 Practical UNIX with Perl

The pipe operator We have been using the pipe operator, ' ', already so this is a review. The pipe operator connects the output of the command on its left to the input of the command on its right. It is a very powerful tool. For example ypcat passwd awk -F: '{print $5}' displays the real names of everyone with an account on the computer. man -k grep ' file ' grep ' write ' looks for commands with the words file and write in the description. 7 CSci 132 Practical UNIX with Perl

Pipes connecting filters Commands can be composed upon each other using the pipe operator. The power of pipes was demonstrated in the first lesson with the following example: who awk '{print $1}' sort uniq In this example, the output of who is the input of awk, whose output is the input of sort, whose output is the input of uniq. The output is a list of usernames of people currently logged in, one per line. (There is a system command, users, that creates this list also, so we really didn't need to do all that work!) 8 CSci 132 Practical UNIX with Perl

Filters The awk, sort, and uniq commands are examples of a class of UNIX programs called filters. A filter is a UNIX command whose input and output are ordinary text, and that expects its input from standard input and puts its output on standard output. Filters transform their input in some way, such as by sorting it, removing words or lines based on a pattern or on their position in the line or file (e.g., remove every 3rd word in a line, or remove every 4th line, or remove any line that has a curse word.) 9 CSci 132 Practical UNIX with Perl

Filtering standard input Filters may have file name arguments on the command line, but when they have no arguments, they read from standard input (the keyboard) instead: grep 'a clue' thehouse searches for 'a clue' in thehouse, whereas in cat thehouse grep 'a clue' grep searches through its standard input stream for lines with 'a clue'. 10 CSci 132 Practical UNIX with Perl

Some useful filters (1) Some of the most useful filters are: grep family: grep, egrep, fgrep sort uniq awk cut global regular expression parsers sorts based on several criteria removes adjacent identical lines full-fledged programming language for field-oriented pattern matching removes pieces of each line based on positions 11 CSci 132 Practical UNIX with Perl

More filters These are filters too: head, tail cat tac fold -w<n> sed wc display just top or bottom lines of files null filter -- shows everything in order shows lines in reverse order display output in width of N columns stream editor -- very powerful filter not exactly a filter, display count of chars, words, and lines 12 CSci 132 Practical UNIX with Perl

Selected filters In the next chapter you will learn about patterns (called regular expressions). Then you will learn how to use the family of filters that use these patterns. First we will look a few of the filters that do not require patterns as arguments. 13 CSci 132 Practical UNIX with Perl

Selected filters: sort The sort program can be used for sorting one or more text files in sequence. It can also merge files. In its simplest form, sort filename will sort filename using the first field of the line (first chars up to the first white space) in ASCII collating order, displaying the sorted file on the screen (standard output). sort will ignore case by default in some versions of UNIX, whereas in others, uppercase and lowercase letters will be treated as different. 14 CSci 132 Practical UNIX with Perl

Selected filters: sort The sort program will not sort numbers properly unless you tell it to sort numerically. It treats them like letters, by default. For example, if scores contains the two lines 5 10 then we get $ sort scores 10 5 because "1" precedes "5", so "10" precedes "5". 15 CSci 132 Practical UNIX with Perl

More on Sorting To sort numerically, you give sort the -n flag: sort -n filename as in: $ sort -n scores 5 10 To reverse the order of the sort use the -r flag 16 CSci 132 Practical UNIX with Perl

Sorting by fields To sort using the second field of the line, sort +1 filename To sort using the third field of the line, sort +2 filename and so on. To use the first field as the primary key, then the second field as secondary key, use sort +0 +1 filename 17 CSci 132 Practical UNIX with Perl

Selected filters: uniq This is the last filter in that pipeline I showed you earlier. The uniq command removes a line from a file if it is identical to the one preceding it. If you sort a file that has duplicate lines, and pipe it through uniq, the duplicates will be removed. You could do the same thing by using the -u option with sort though, so this is not why uniq is unique. Sometimes there are files that are not sorted but have "runs" of the same lines. You could sort them using sort -u, but it is fast to just run uniq on them. 18 CSci 132 Practical UNIX with Perl

Selected filters: fold The fold filter breaks each line at a fixed number of characters, so that each line is at most a certain length. fold -w8 myfile will break each line of myfile into 8-character wide lines except those with fewer characters. Suppose dnastring has the line agatggcggc fold -c4 dnastring agat ggcg gc produces 19 CSci 132 Practical UNIX with Perl

Selected Filters: wc The wc command, by default, displays the numbers of lines, words, and characters in one or more files given on the command line, or in its input stream if not given any arguments. $ wc /etc/passwd 55 99 2678 /etc/passwd tells me how many lines, words, and characters are in the passwd file. I can give it -m, -w, or -l for chars, words, and lines to restrict its output. 20 CSci 132 Practical UNIX with Perl

Uses of wc For example $ who wc -l 2 displays the number of users currently logged in, and $ ps -ef grep bash wc -l displays how many people are running bash at the moment. 21 CSci 132 Practical UNIX with Perl

Things to try Read the man pages for sort, fold, and wc. Familarize yourself with them because they are very useful. Read about cut; it is also useful. There is a file in the cs132/data directory named directors. Sort it by movie title, then by director last name. Print out just the movie titles. Print out just the director names. Download any spreadsheet and save as a CSV (comma separated values) file, using tabs to separate the fields. Experiment with sort to see how you can sort this file in different ways. 22 CSci 132 Practical UNIX with Perl