博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Bash String Manipulation Examples – Length, Substring, Find and Replace
阅读量:4176 次
发布时间:2019-05-26

本文共 5494 字,大约阅读时间需要 18 分钟。

In bash shell, when you use a dollar sign followed by a variable name, shell expands the variable with its value. This feature of shell is called parameter expansion.

But parameter expansion has numerous other forms which allow you to expand a parameter and modify the value or substitute other values in the expansion process. In this article, let us review how to use the parameter expansion concept for string manipulation operations.

This article is part of the on-going bash tutorial series. Refer to our earlier article on .

1. Identify String Length inside Bash Shell Script

${#string}

The above format is used to get the length of the given bash variable.

$ cat len.sh#! /bin/bashvar="Welcome to the geekstuff"echo ${#var}$ ./len.sh24

To understand more about bash variables, read .

2. Extract a Substring from a Variable inside Bash Shell Script

Bash provides a way to extract a substring from a string. The following example expains how to parse n characters starting from a particular position.

${string:position}

Extract substring from $string at $position

${string:position:length}

Extract $length of characters substring from $string starting from $position. In the below example, first echo statement returns the substring starting from 15th position. Second echo statement returns the 4 characters starting from 15th position. Length must be the number greater than or equal to zero.

$ cat substr.sh#! /bin/bashvar="Welcome to the geekstuff"echo ${var:15}echo ${var:15:4}$ ./substr.shgeekstuffgeek

Also, refer to our earlier article to understand more about .

3. Shortest Substring Match

Following syntax deletes the shortest match of $substring from front of $string

${string#substring}

Following syntax deletes the shortest match of $substring from back of $string

${string%substring}

Following sample shell script explains the above two shortest substring match concepts.

$ cat shortest.sh#! /bin/bashfilename="bash.string.txt"echo ${filename#*.}echo ${filename%.*}$ ./shortest.shAfter deletion of shortest match from front: string.txtAfter deletion of shortest match from back: bash.string

In the first echo statement substring ‘*.’ matches the characters and a dot, and # strips from the front of the string, so it strips the substring “bash.” from the variable called filename. In second echo statement substring ‘.*’ matches the substring starts with dot, and % strips from back of the string, so it deletes the substring ‘.txt’

4. Longest Substring Match

Following syntax deletes the longest match of $substring from front of $string

${string##substring}

Following syntax deletes the longest match of $substring from back of $string

${string%%substring}

Following sample shell script explains the above two longest substring match concepts.

$ cat longest.sh#! /bin/bashfilename="bash.string.txt"echo "After deletion of longest match from front:" ${filename##*.}echo "After deletion of longest match from back:" ${filename%%.*}$ ./longest.shAfter deletion of longest match from front: txtAfter deletion of longest match from back: bash

In the above example, ##*. strips longest match for ‘*.’ which matches “bash.string.” so after striping this, it prints the remaining txt. And %%.* strips the longest match for .* from back which matches “.string.txt”, after striping  it returns “bash”.

5. Find and Replace String Values inside Bash Shell Script

Replace only first match

${string/pattern/replacement}

It matches the pattern in the variable $string, and replace only the first match of the pattern with the replacement.

$ cat firstmatch.sh#! /bin/bashfilename="bash.string.txt"echo "After Replacement:" ${filename/str*./operations.}$ ./firstmatch.shAfter Replacement: bash.operations.txt

Replace all the matches

${string//pattern/replacement}

It replaces all the matches of pattern with replacement.

$ cat allmatch.sh#! /bin/bashfilename="Path of the bash is /bin/bash"echo "After Replacement:" ${filename//bash/sh}$ ./allmatch.shAfter Replacement: Path of the sh is /bin/sh

Taking about find and replace, refer to our earlier articles – and .

Replace beginning and end

${string/#pattern/replacement

Following syntax replaces with the replacement string, only when the pattern matches beginning of the $string.

${string/%pattern/replacement

Following syntax replaces with the replacement string, only when the pattern matches at the end of the given $string.

$ cat posmatch.sh#! /bin/bashfilename="/root/admin/monitoring/process.sh"echo "Replaced at the beginning:" ${filename/#\/root/\/tmp}echo "Replaced at the end": ${filename/%.*/.ksh}$ ./posmatch.shReplaced at the beginning: /tmp/admin/monitoring/process.shReplaced at the end: /root/admin/monitoring/process.ksh

Recommended Reading

, by Ramesh Natarajan. I spend most of my time on Linux environment. So, naturally I’m a huge fan of Bash command line and shell scripting. 15 years back, when I was working on different flavors of *nix, I used to write lot of code on C shell and Korn shell. Later years, when I started working on Linux as system administrator, I pretty much automated every possible task using Bash shell scripting. Based on my Bash experience, I’ve written Bash 101 Hacks eBook that contains 101 practical examples on both Bash command line and shell scripting. If you’ve been thinking about mastering Bash, do yourself a favor and read this book, which will help you take control of your Bash command line and shell scripting.

FROM: http://www.thegeekstuff.com/2010/07/bash-string-manipulation/

转载地址:http://hgvai.baihongyu.com/

你可能感兴趣的文章
Apache Maven项目提供的WAR插件详解
查看>>
Apache Maven项目提供的EAR插件详解
查看>>
从JBoss Seam 2.x迁移到JavaEE 7之一:组件模型
查看>>
从JBoss Seam 2.x迁移到JavaEE 7之二:组件的有效范围Scope
查看>>
从JBoss Seam 2.x迁移到JavaEE 7之三:对象的注入
查看>>
从JBoss Seam 2.x迁移到JavaEE 7之四:事件机制
查看>>
Java 1.5并发包之一:Lock
查看>>
Java 1.5并发包之二:任务与线程池
查看>>
Java 1.5并发包之三:线程池实现之Fork/Join框架
查看>>
Java多线程同步方法的概述
查看>>
Java IO概述
查看>>
Java NIO概述
查看>>
Java中synchronized与volatile的区别与联系
查看>>
volatile与synchronized在Java单例模式中的应用
查看>>
Hibernate 5.1概述
查看>>
Hibernate数据类型及JPA的Entity类与Hibernate的Entity类的区别
查看>>
Hibernate中的Entity类未必final
查看>>
Hibernate中的Entity类中的无参数构造函数
查看>>
Hibernate中的Entity类中的getter/setter方法
查看>>
Hibernate中的Entity类的乐观锁配置
查看>>