“Ganti Substring di Bash” Kode Jawaban

bash menggantikan substring

echo [string] | sed "s/[original]/[target]/g"
Curious Cobra

Ganti Substring di Bash

#To replace the first occurrence of a pattern with a given string,
#use ${parameter/pattern/string}:

#!/bin/bash
firstString="I love Suzi and Marry"
secondString="Sara"
echo "${firstString/Suzi/$secondString}"    

# prints 'I love Sara and Marry'

#To replace all occurrences, use ${parameter//pattern/string}:

message='The secret code is 12345'
echo "${message//[0-9]/X}"           
# prints 'The secret code is XXXXX'


#(This is documented in the Bash Reference Manual, §3.5.3 "Shell Parameter Expansion".)
Mckynde

bash ganti awal string

$ cat shortest.sh
#! /bin/bash

filename="bash.string.txt"

echo ${filename#*.}
echo ${filename%.*}

$ ./shortest.sh
After deletion of shortest match from front: string.txt
After deletion of shortest match from back: bash.string
Zac

Jawaban yang mirip dengan “Ganti Substring di Bash”

Pertanyaan yang mirip dengan “Ganti Substring di Bash”

Lebih banyak jawaban terkait untuk “Ganti Substring di Bash” di Shell/Bash

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya