Posts tagged substring in bash
How to implement substring() like function in bash
Sep 14th
Look at the following snippet to understand how to extract specified number of characters from a given offset in a string.
[neo@techpulp ~]# cat substr.sh
#!/bin/bash
str="this is a mysubstring test"
subs=${str:10:11}
echo Length of string is ${#str}
echo Sub string starting at offset 10 and len 11 is \"$subs\"
[neo@techpulp ~]#
Here is the output of the above script.
[neo@techpulp ~]# sh substr.sh Length of string is 26 Sub string starting at offset 10 and len 11 is "mysubstring" [neo@techpulp ~]#
Now let us implement a generic function substring:
[neo@techpulp ~]# cat sstr.sh
#!/bin/bash
# Usage: substring <string> <offset> <length>
function substring()
{
local More > 

Recent Comments