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

Slides:



Advertisements
Similar presentations
CST8177 sed The Stream Editor. The original editor for Unix was called ed, short for editor. By today's standards, ed was very primitive. Soon, sed was.
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.
1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
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.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Strings.
Java Programming Strings Chapter 7.
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
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
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
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.
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 © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
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
Agenda Regular Expressions (Appendix A in Text) –Definition / Purpose –Commands that Use Regular Expressions –Using Regular Expressions –Using the Replacement.
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.
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.
ABAP/4 Defining Data Defining Data: –Data statement –Parameters statement –Tables statement –Constants statement –Data statement to define field strings.
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.
Unit – 3 Control structures. Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
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.
Chapter 8 Strings and Vectors 1
Chapter 8 Strings and Vectors
Chapter 7 Pointers and C-Strings
CIRC Summer School 2017 Baowei Liu
CIRC Summer School 2017 Baowei Liu
Strings in BASH.
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
CSC 221: Introduction to Programming Fall 2018
What's wrong with Easter jokes? They crack you up
Shell Control Structures
String Processing 1 MIS 3406 Department of MIS Fox School of Business
Introduction to Computer Science
Chapter 5 The Bourne Shell
Lecture 5 SQL FUNCTIONS.
Python Strings.
Exam Prep.
Review The Unix Shells Graham Glass and King Ables,
Manufacture Part Search Template Overview
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 # echo `expr index "$stringZ" C12` # 6 # C position. echo `expr index "$stringZ" c` # 3

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

stringZ=abcABC123ABCabc # # 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!!