[Info] String manipulation in Bash

String Length

${#string}

expr length $string

expr "$string" : '.*'

Example:

stringZ=abcABC123ABCabc

echo ${#stringZ} # 15
echo `expr length $stringZ` # 15
echo `expr "$stringZ" : '.*'` # 15

 

Length of Matching Substring at Beginning of String

expr match "$string" '$substring'
$substring is a regular expression.

expr "$string" : '$substring'
$substring is a regular expression.

Example:

stringZ=abcABC123ABCabc
# |------|
# 12345678

echo `expr match "$stringZ" 'abc[A-Z]*.2'` # 8
echo `expr "$stringZ" : 'abc[A-Z]*.2'` # 8

 

Index

expr index $string $substring
Numerical position in $string of first character in $substring that matches.

Example:
stringZ=abcABC123ABCabc
# 123456 ...
echo `expr index "$stringZ" C12` # 6
# C position.

echo `expr index "$stringZ" 1c` # 3
# 'c' (in #3 position) matches before '1'.


Substring Extraction

${string:position}
Extracts substring from $string at $position.

If the $string parameter is "*" or "@", then this extracts the positional parameters, [1] starting at $position.

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

expr substr $string $position $length
Extracts $length characters from $string starting at $position.

expr match "$string" '\($substring\)'
Extracts $substring at beginning of $string, where $substring is a regular expression.

expr "$string" : '\($substring\)'
Extracts $substring at beginning of $string, where $substring is a regular expression.

expr match "$string" '.*\($substring\)'
Extracts $substring at end of $string, where $substring is a regular expression.

expr "$string" : '.*\($substring\)'
Extracts $substring at end of $string, where $substring is a regular expression.

 

Substring Removal

${string#substring}
Deletes shortest match of $substring from front of $string.

${string##substring}
Deletes longest match of $substring from front of $string.

${string%substring}
Deletes shortest match of $substring from back of $string.

${string%%substring}
Deletes longest match of $substring from back of $string.

 

Substring Replacement

${string/substring/replacement}
Replace first match of $substring with $replacement. [2]

${string//substring/replacement}
Replace all matches of $substring with $replacement.

${string/#substring/replacement}
If $substring matches front end of $string, substitute $replacement for $substring.

${string/%substring/replacement}
If $substring matches back end of $string, substitute $replacement for $substring.

 

Manipulating strings using awk


A Bash script may invoke the string manipulation facilities of awk as an alternative to using its built-in operations.

Example: Alternate ways of extracting and locating substrings

#!/bin/bash
# substring-extraction.sh

String=23skidoo1
# 012345678 Bash
# 123456789 awk
# Note different string indexing system:
# Bash numbers first character of string as 0.
# Awk numbers first character of string as 1.

echo ${String:2:4} # position 3 (0-1-2), 4 characters long
# skid

# The awk equivalent of ${string:pos:length} is substr(string,pos,length).
echo | awk '
{ print substr("'"${String}"'",3,4) # skid
}
'
# Piping an empty "echo" to awk gives it dummy input,
#+ and thus makes it unnecessary to supply a filename.

echo "----"

# And likewise:

echo | awk '
{ print index("'"${String}"'", "skid") # 3
} # (skid starts at position 3)
' # The awk equivalent of "expr index" ...

exit 0

 

 

References:

http://tldp.org/LDP/abs/html/string-manipulation.html

Attached Files
There are no attachments for this article.
Comments
There are no comments for this article. Be the first to post a comment.
Name
Email
Security Code Security Code
Related Articles RSS Feed
MENU