Sed. Class Issues vSphere Issues – root only until lab 3.

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

CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Find Command Characteristics –Locate files descending from multiple starting points –Employs regular expressions Examples On entire system: >find / -name.
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
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Introduction to Linux and Shell Scripting Jacob Chan.
Shell Programming. Shell Scripts (1) u Basically, a shell script is a text file with Unix commands in it. u Shell scripts usually begin with a #! and.
Unix/Linux basics 0011 Operating systems lab Gergely Windisch room 4.12
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
1 Day 16 Sed and Awk. 2 Looking through output We already know what “grep” does. –It looks for something in a file. –Returns any line from the file that.
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Introduction to Unix (CA263) File Processing. Guide to UNIX Using Linux, Third Edition 2 Objectives Explain UNIX and Linux file processing Use basic file.
Bash shell programming Part II – Control statements Deniz Savas and Michael Griffiths November 2005 Corporate Information and Computing Services The University.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
CS 403: Programming Languages Fall 2004 Department of Computer Science University of Alabama Joel Jones.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Session 2 Wharton Summer Tech Camp Basic Unix. Agenda Cover basic UNIX commands and useful functions.
Lecture 4  C Shell Scripts(Chapter 10). Shell script/program  Shell script: a series of shell commands placed in an ASCII text file  Commands include.
Linux+ Guide to Linux Certification, Third Edition
Linux Operations and Administration
CMPSC 60: Week 6 Discussion Originally Created By: Jason Wither Updated and Modified By: Ryan Dixon University of California Santa Barbara.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 Unit 11: Shell.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections.
Introduction to Awk Awk is a convenient and expressive programming language that can be applied to a wide variety of computing and data manipulation tasks.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
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 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
BY A Mikati & M Shaito Awk Utility n Introduction n Some basics n Some samples n Patterns & Actions Regular Expressions n Boolean n start /end n.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
LIN Unix Lecture 5 Unix Shell Scripts. LIN Command Coordination ; && || command1 ; command2 Interpretation: Do command 1. Then do command.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
CS252: Systems Programming Ninghui Li Slides by Prof. Gustavo Rodriguez-Rivera Topic 7: Unix Tools and Shell Scripts.
Linux Commands C151 Multi-User Operating Systems.
Awk- An Advanced Filter by Prof. Shylaja S S Head of the Dept. Dept. of Information Science & Engineering, P.E.S Institute of Technology, Bangalore
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
1 P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Awk Programming Ruibin Bai (Room AB326) Division of Computer Science The University.
1 Lecture 10 Introduction to AWK COP 3344 Introduction to UNIX.
Linux+ Guide to Linux Certification, Second Edition
2 nd Intro to Shell Scripting. Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by midnight Labs 7 & 8 or late ones – 8 is extra credit – Due.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
By Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Linux Administration Working with the BASH Shell.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Tutorial of Unix Command & shell scriptS 5027
Arun Vishwanathan Nevis Networks Pvt. Ltd.
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CSC 352– Unix Programming, Fall 2012
C151 Multi-User Operating Systems
Shell Script Assignment 1.
PROGRAMMING THE BASH SHELL PART IV by İlker Korkmaz and Kaya Oğuz
Tutorial of Unix Command & shell scriptS 5027
Tutorial of Unix Command & shell scriptS 5027
What is Bash Shell Scripting?
LING 408/508: Computational Techniques for Linguists
Tutorial of Unix Command & shell scriptS 5027
Linux Shell Script Programming
CSE 303 Concepts and Tools for Software Development
CSC 352– Unix Programming, Fall, 2011
Awk.
Introduction to Bash Programming, part 3
Presentation transcript:

sed

Class Issues vSphere Issues – root only until lab 3

sed sed = stream editor Allows you make changes to text files on the fly on a line by line basis Basic syntax is: sed ‘s/pattern/replacement/’ filename Works on files, default output is to the screen

Basic Examples Replace: –sed ‘s/1/ONE/g’ file.txt (would replace the first 1 with ONE in file.txt) Swap: – sed 'y/abc/ABC' file.txt (replace a with A, b with B, c with C) Show line numbers then lines: –sed ‘=‘ file.txt Lowercase: sed 's/[A-Z]/\l&/‘ file.txt

Special Replacements \L - turn the replacement to lowercase until a \U or \E is found, \l - turn the next character to lowercase \U - turn the replacement to uppercase until a \L or \E is found \u - turn the next character to uppercase, \E - stop case conversion started by \L or \U.

Remember the g? First Character Lower Case: –sed 's/[A-Z]/\l&/' addresses.txt All Characters Lower Case: –sed 's/[A-Z]/\l&/g' addresses.txt 5th Character Lower Case: –sed 's/[A-Z]/\l&/5' addresses.txt

Useful sed command line -f means read sed commands from file –sed –f mysed.txt

Gotchas Don’t do: –sed 's/[A-Z]/\l&/[3-5]' addresses.txt > addresses.txt Create a new file and mv it back.

awk

Programming language for data streams; usually files or subsets of files Developed at Bell Labs in the ’70’s by: Alfred Aho, Peter Weinberger, and Brian Kernighan Like sed but does the following as well: –specify particular fields of a matching line to manipulate and/or output –use variables to store values temporarily –perform calculations on those variables.

How to run? Usually: awk ‘ ’ files to parse Example (a cat replacement): awk ‘{print $0}’ *

Fields in awk By default lines are delimited by white space: space characters or tabs $0 – whole line $1 – is first field in line $2 – is 2 nd field in line NF – number of fields on line No error reported if field doesn’t exist, simply outputs a blank line

Basic Pattern Matching /pattern/ { do something } Example: /A/ { print “This line has an A”} No pattern means do the work on every line

Multiple patterns Just list them: awk ‘ /A/ { print “this line has an A”} /B/ { print “this line has a B”} { print “this gets printed always” } ‘ my_file Don’t forget the closing ‘

Forming Patterns Can use most of the extended regular expressions, except {} Can do the relational expressions from other languages:, ==, !=, =

Comparisons Numeric example: ls -l | awk '$5 >= 512 {print $0}' String example: ls -l | awk '$6 == “Sep” {print $0}'

Variables Variables are allowed: ls -l | awk '$5 >= 512 {size += $5}‘ Variables don’t have to be initialized However, we won’t ever see size output!

BEGIN/END BEGIN – run before any lines are read END – run after every line has been read You can have one, both, neither Example: ls -l | awk '$5 >= 512 {size += $5} END { print size}‘

Begin Example awk ‘BEGIN { printf(“I’m pretending to be cat\n”) } { print $0}’ my_file printf is C/Java like

Useful BEGIN command Remember BEGIN goes before reading of lines begins Want the first line of your file as a header? Use getline, but no pattern matching or processing of the lines read awk ‘BEGIN { getline; print $0}’ my_file

Useful awk variables FILENAME – current file being parsed NF – number of fields on the current line FNR – current record in the file (usually means current line) NR – current record in the stream There are many, many others as well, read the man page

Standalone awk scripts Add the following to top of file of awk commands: #!/usr/bin/awk –f Don’t forget to chmod +x Example: #!/usr/bin/awk -f /^.\// { dots += 1 } /^$/ { blank += 1 } /^l/ { sym += 1 } /^d/ { dir += 1 } /^-/ { file += 1 } { total += 1 } END { printf("Dots=%d\nBlank Lines=%d\nSym Links=%d\nDirectories=%d\nFiles=%d\nTotal=%d\n",do ts, blank, sym, dir, file, total)}

Intro to Shell Scripting

Shell Script 101 List of commands interpreted by a shell cd /var/tmp mkdir boks cd boks gzip -dc /var/tmp/boks_install.tar.gz | tar xf -./install.jvss0001 cd /var/tmp rm -r boks* cd /etc/rc2.d mv S99boksm _S99boksm

Why need? Add users List bad perms Parsing log files A menu system for users Startup of system (.bashrc is a script) etc

Tests (if then else) if [ $count –ne 0 ] then avg=$((sum/count)) else avg=0 fi

Looping (for) for var in do echo $var done

Variables in Scripts I=0 J=$I E=$(($C+$D)) X=$(($X+1)) Notice the lack of spaces!

Input/Output/Assignment Input –read Output –echo – printf

Comparisions Numeric –-eq, -ne, -lt, -le, -gt, -ge Strings ==, != And is && Or is ||

Comparisions II File testing: –-d – is the item a directory? –-e – does the file already exist? –-f – does the file exist and is it a regular file? –-h (or –L) – is the item a symbolic link? (current user) –-r – does the file exist and is it readable? –-w – does the file exist and is it writable? –-x – does the file exist and is it executable?

Sample Comparisons [ -f my_file ] (don’t forget spaces) [ $x –ge 0 ] [ $1 = start ] [ “$1” = “” ] – empty string test! [ “$1x” = “x” ] – better empty string test!

Command Line Parameters Accessed via $# parameters –$0 is script name –$1, $2 … $9 = parameters on the command line or $* are all parameters –$# is total number passed Assignments say receive as parameter, assume it’s this one!

Accessing Parameters #!/bin/bash echo The command name is: $0. echo The number of command line arguments passed as parameters are $#. echo Yet another way to show total number: $*. echo The value of the command line arguments are: $1 $2 $3 $4 $5 $6 $7 $8 $9. echo Another way to display values of all of the arguments: exit 0

Debugging? Set -vx shows info -v = verbose -x = lines after run