Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม Bash

เชี่ยวชาญ Bash String Manipulation: ความยาว Substring Find & Replace

ใน Bash Shell เมื่อคุณใช้ Dollar Sign ตามด้วยชื่อตัวแปร Shell จะขยายตัวแปรด้วยค่าของมัน ฟีเจอร์นี้เรียกว่า Parameter Expansion

แต่ Parameter Expansion มีรูปแบบอื่นๆ อีกมากมาย ที่ช่วยให้คุณขยาย Parameter และแก้ไขค่า หรือแทนที่ค่าอื่นในกระบวนการขยาย ในบทความนี้ เราจะทบทวนการใช้ Parameter Expansion สำหรับ String Manipulation Operations

บทความนี้เป็นส่วนหนึ่งของชุดบทช่วยสอน Bash ต่อเนื่อง โปรดดูบทความก่อนหน้าเกี่ยวกับ Bash { } Expansion

1. หาความยาว String ภายใน Bash Shell Script

${#string}

รูปแบบข้างต้นใช้สำหรับหาความยาวของ Bash Variable ที่กำหนด

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

เพื่อเข้าใจมากขึ้นเกี่ยวกับ Bash Variables โปรดอ่าน 6 Practical Bash Global and Local Variable Examples

2. ดึง Substring จาก Variable ภายใน Bash Shell Script

Bash มีวิธีดึง Substring จาก String ตัวอย่างต่อไปนี้อธิบายวิธี Parse N Characters เริ่มจากตำแหน่งเฉพาะ

${string:position}

ดึง Substring จาก $string ที่ $position

${string:position:length}

ดึง $length Characters ของ Substring จาก $string เริ่มจาก $position ในตัวอย่างด้านล่าง Echo Statement แรกคืนค่า Substring เริ่มจากตำแหน่งที่ 15 Echo Statement ที่สองคืนค่า 4 Characters เริ่มจากตำแหน่งที่ 15 Length ต้องเป็นตัวเลขที่มากกว่าหรือเท่ากับศูนย์

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

อ่านเพิ่มเติมเกี่ยวกับ Bash Special Parameters $*, $@, $#, $$, $!, $?, $-, $_ ได้ที่บทความก่อนหน้า

3. Shortest Substring Match (การจับคู่ Substring สั้นที่สุด)

Syntax ต่อไปนี้ลบ Shortest Match ของ $substring จากหน้า $string

${string#substring}

Syntax ต่อไปนี้ลบ Shortest Match ของ $substring จากหลัง $string

${string%substring}

Shell Script ตัวอย่างด้านล่างอธิบายแนวคิด Shortest Substring Match ทั้งสอง

$ 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

ใน Echo Statement แรก Substring '*.' ตรงกับ Characters และ Dot และ # ตัดจากหน้า String ดังนั้นจึงตัด Substring "bash." ออกจาก Variable ชื่อ filename ใน Echo Statement ที่สอง Substring '.*' ตรงกับ Substring ที่เริ่มด้วย Dot และ % ตัดจากหลัง String ดังนั้นจึงลบ Substring '.txt'

4. Longest Substring Match (การจับคู่ Substring ยาวที่สุด)

Syntax ต่อไปนี้ลบ Longest Match ของ $substring จากหน้า $string

${string##substring}

Syntax ต่อไปนี้ลบ Longest Match ของ $substring จากหลัง $string

${string%%substring}

Shell Script ตัวอย่างด้านล่างอธิบายแนวคิด Longest Substring Match ทั้งสอง

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

ในตัวอย่างข้างต้น ##*. ตัด Longest Match สำหรับ '*.' ซึ่งตรงกับ "bash.string." หลังตัดแล้วเหลือ txt และ %%.* ตัด Longest Match สำหรับ .* จากหลัง ซึ่งตรงกับ ".string.txt" หลังตัดแล้วคืนค่า "bash"

5. Find และ Replace String Values ภายใน Bash Shell Script

Replace เฉพาะ Match แรก

${string/pattern/replacement}

จับคู่ Pattern ใน Variable $string และ Replace เฉพาะ Match แรกของ Pattern ด้วย Replacement

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

Replace ทุก Match

${string//pattern/replacement}

Replace ทุก Match ของ Pattern ด้วย Replacement

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

เกี่ยวกับ Find และ Replace โปรดดูบทความก่อนหน้าเกี่ยวกับ sed substitute examples และ Vim find and replace

Replace ที่จุดเริ่มต้นและจุดสิ้นสุด

${string/#pattern/replacement}

Syntax ต่อไปนี้ Replace ด้วย Replacement String เฉพาะเมื่อ Pattern ตรงกับจุดเริ่มต้นของ $string

${string/%pattern/replacement}

Syntax ต่อไปนี้ Replace ด้วย Replacement String เฉพาะเมื่อ Pattern ตรงกับจุดสิ้นสุดของ $string ที่กำหนด

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

Recommended Reading

Bash 101 Hacks, by Ramesh Natarajan ผมใช้เวลาส่วนใหญ่กับ Linux Environment จึงเป็นแฟนใหญ่ของ Bash Command Line และ Shell Scripting 15 ปีก่อน เมื่อทำงานบน *nix หลายรสชาติ เคยเขียน Code มากบน C Shell และ Korn Shell ต่อมาเมื่อเริ่มทำงานบน Linux เป็น System Administrator ได้ทำ Automate ทุกอย่างที่เป็นไปได้ด้วย Bash Shell Scripting จากประสบการณ์ Bash ของผม จึงเขียน Bash 101 Hacks eBook ที่มี 101 ตัวอย่างปฏิบัติทั้ง Command Line และ Shell Scripting หากคิดจะ Master Bash ให้ทำความดีใจตัวเองและอ่านหนังสือเล่มนี้ จะช่วยให้ควบคุม Bash Command Line และ Shell Scripting ได้อย่างเต็มที่