Strings in BASH.

Slides:



Advertisements
Similar presentations
Strings in BASH. Variables are strings by default We are going to look at some ways to manipulate strings in BASH.
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Macro simple idea of textual substitution useful when you need a group of instructions or directives frequently.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Strings.
Java Programming Strings Chapter 7.
Day Six. SR 769 Day Six: Leaving a Legacy Clue 1: It’s a palindrome - sort of.
Day Six Day Six: Leaving a Legacy Clue #1: It’s a palindrome—sort of.
Chapter 18 Recursion "To iterate is human, to recurse divine.", L. Peter Deutsch.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Bash, part 2 Prof. Chris GauthierDickey COMP Unix Tools.
CS 497C – Introduction to UNIX Lecture 34: - Shell Programming Chin-Chih Chang
1.10 Strings academy.zariba.com 1. Lecture Content 1.What is a string? 2.Creating and Using strings 3.Manipulating Strings 4.Other String Operations 5.Building.
Strings. Sentences in English are implemented as strings in the C language. Computations involving strings are very common. E.g. – Is string_1 the same.
UNIX Filters.
Shell Script Examples.
Methods. Why methods? A method gives a name to something that you want to do, so you don’t have to think about how to do it, you just do it The name should.
Advanced Shell Programming. 2 Objectives Use techniques to ensure a script is employing the correct shell Set the default shell Configure Bash login and.
Strings and Arrays. String Is a sequence of characters. Example: “hello”, “how are you?”, “123”,and are all valid string values.
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.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 13 - Recursion.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Recursion A method is recursive if it makes a call to itself. A method is recursive if it makes a call to itself. For example: For example: public void.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Web Database Programming Week 3 PHP (2). Functions Group related statements together to serve “a” specific purpose –Avoid duplicated code –Easy maintenance.
Chapter 2 User_defined Function. Chapter Goals In this chapter, you’ll learn all about PHP functions, including how : to create and invoke them, pass.
CSC 352– Unix Programming, Spring 2015 April 28 A few final commands.
208 New Book Review. String Type #include Assignment – string m=“Hi Mom”; – m=“Goodbye”; Concatenate - + – Can use variables or literals. Can use [ ]
What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports.
Project 1: Using Arrays and Manipulating Strings Essentials for Design JavaScript Level Two Michael Brooks.
Hossain Shahriar Announcement and reminder! Tentative date for final exam need to be fixed! We have agreed so far that it can.
Xuan Guo Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael.
String Processing Word processing term papers, writing memoirs, sending messages, responding to surveys, placing online orders and registering products.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
LING 408/508: Programming for Linguists Lecture 5 September 9 th.
INLS 560 – S TRINGS Instructor: Jason Carter. T YPES int list string.
Shell Scripting What is Shell Programming? Putting UNIX commands in a file Seldom used where speed is important Often used to manipulate files To create.
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Lesson 4 String Manipulation. Lesson 4 In many applications you will need to do some kind of manipulation or parsing of strings, whether you are Attempting.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Enumerated Types and Strings Types.
Chapter 8 Strings and Vectors 1
Chapter 8 Strings and Vectors
Chapter 7 Pointers and C-Strings
Shell Scripting March 1st, 2004 Class Meeting 7.
CIRC Summer School 2017 Baowei Liu
SAS in Data Cleaning.
Chapter 7: Strings and Characters
Copyright © – Curt Hill Bash Scripting Fundamentals Copyright © – Curt Hill.
String Manipulation Part 2
CSC 352– Unix Programming, Spring 2016
LING 408/508: Computational Techniques for Linguists
Passing Parameters by value
CSC 221: Introduction to Programming Fall 2018
What's wrong with Easter jokes? They crack you up
Day Six: 6-1.
Shell Control Structures
String Processing 1 MIS 3406 Department of MIS Fox School of Business
Introduction to Computer Science
Chapter 5 The Bourne Shell
CST8177 Scripting 2: What?.
Lecture 5 SQL FUNCTIONS.
Python Strings.
Exam Prep.
Review The Unix Shells Graham Glass and King Ables,
What We Want To Do User enters: Mary Smith
Presentation transcript:

Strings in BASH

Variables are strings by default We are going to look at some ways to manipulate strings in BASH

String Length ${#string} expr length $string These are the equivalent of strlen() in C. expr "$string" : '.*' stringZ=abcABC123ABCabc echo ${#stringZ} # 15 echo `expr length $stringZ` # 15 echo `expr "$stringZ" : '.*'` # 15

Index expr index $string $substring Numerical position in $string of first character in $substring that matches. stringZ=abcABC123ABCabc # 0123456 ... echo `expr index "$stringZ" C12` # 5 # C position. echo `expr index "$stringZ" c` #2

Substring Extraction ${string:position} Extracts substring from $string at $position to the end of the string. ${string:position:length} Extracts $length characters of substring from $string at $position.

stringZ=abcABC123ABCabc # 0123456789..... # 0-based indexing. echo ${stringZ:0} # abcABC123ABCabc echo ${stringZ:1} # bcABC123ABCabc echo ${stringZ:7} # 23ABCabc echo ${stringZ:7:3} # 23A # Three characters of substring. # Is it possible to index from the right end of the string? echo ${stringZ:-4} # abcABC123ABCabc # Defaults to full string, as in ${parameter:-default}. # However . . . echo ${stringZ:(-4)} # Cabc echo ${stringZ: -4} # Cabc The position and length arguments can be "parameterized," that is, represented as a variable, rather than as a numerical constant.

Substring Removal ${string#substring} Deletes shortest match of $substring from front of $string. ${string##substring} Deletes longest match of $substring from front of $string. stringZ=abcABC123ABCabc # |----| shortest # |----------| longest echo ${stringZ#a*C} # 123ABCabc # Strip out shortest match between 'a' and 'C'. echo ${stringZ##a*C} # abc # Strip out longest match between 'a' and 'C'.

Substring Replacement ${string/substring/replacement} Replace first match of $substring with $replacement. ${string//substring/replacement} Replace all matches of $substring with $replacement.

stringZ=abcABC123ABCabc echo ${stringZ/abc/xyz} # xyzABC123ABCabc # Replaces first match of 'abc' with 'xyz'. echo ${stringZ//abc/xyz} # xyzABC123ABCxyz # Replaces all matches of 'abc' with # 'xyz'. echo "$stringZ" # abcABC123ABCabc # The string itself is not altered! # Can the match and replacement strings be parameterized? match=abc repl=000 echo ${stringZ/$match/$repl} # 000ABC123ABCabc # ^ ^ ^^^ echo ${stringZ//$match/$repl} # 000ABC123ABC000 # Yes! ^ ^ ^^^ ^^^ echo # What happens if no $replacement string is supplied? echo ${stringZ/abc} # ABC123ABCabc echo ${stringZ//abc} # ABC123ABC # A simple deletion takes place.

Palindrome Assignment Write a BASH shell script to determine if a word, supplied as input from the user, is a palindrome or not. Remember a palindrome is a word that is the same backwards as it is forwards. For example: racecar, noon, mom are palindromes. Once you have accomplished this you can determine if a sentence is a palindrome – you have to strip off punctuation and spaces and then test to see if it is a palindrome. For example, Madam, I’m Adam, and Go hang a salami, I’m a lasagna hog, are both palindromes. Have fun!!