extracting substring at bash
* * * * ½ 4 votos
June 28th, 2007 mysurface Posted in Bash, Text Manipulation | Hits: 26844 |
Extracting sub string at Bash is very easy, let say you have a phone number 012-4567890 and you just wanna extract 4567890 out, you can do as below.
num="016-4567890";
echo ${num:5:7}
5 is the starting point and 7 is the string length for sub string.
Live Chat!









July 17th, 2007 at 10:34 pm
well,this was good.but i wanna knw if v can extract a substring from a string when the position is not known like when u wanna extract a string from each line of a file and the postion of tht string is not fixed in each of the lines.and u want to write a general code to do tht. is it possible.
July 17th, 2007 at 11:39 pm
You can use grep or awk before you extract a substring. It depends on how you manipulate it.
July 20th, 2007 at 12:50 am
“bellow”?
ffs, it’s “below”. go check what “bellow” means in dict.org.
July 20th, 2007 at 8:16 pm
sorry, typo :S
January 18th, 2008 at 1:58 pm
This may be useful:
To extract a substring from a string between a prefix substring and a suffix substring, one can extend the above method with a script like this:
#!/bin/bash
if [ $# == 3 ]
then
ppref=”${1%${2}*}”;
ssuff=”${1#*${3}}”;
nopref=”${1#${ppref}${2}}”;
echo “${nopref%${3}${ssuff}}”;
else
echo “Usage: substr string prefix suffix”
fi
Save this as ’substr’.
January 18th, 2008 at 2:01 pm
Example for the above:
substr “hello World” “el” “or”
gives “lo W”
January 19th, 2008 at 12:14 am
great, good job, couldn’t understand it thought, I was looking for something similar, here’s the catch, I need to extract a substring from prefix to 3 letters more, since the suffix is variable/unknow, but I know how much letters I want. ;)
January 19th, 2008 at 12:23 am
well I did it! since the substring position is know I use this code ;)
EL_MAP=$(ffmpeg -i “$A_VIEJO” -vcodec copy -vn -y “$A_NOMBRE”_tmp.wav 2>&1 | grep Video:) ; #capturar audio y ver cual es el stream de video
VIDEO_STREAM_MAP=${EL_MAP:11:3}
January 19th, 2008 at 1:37 am
Here is the explanation( for those who may want it)
#!/bin/bash
if [ $# == 3 ]
then
# from string($1) remove everything from the prefix($2) #onwards.
# ppref contains, therefore, everything before the prefix
ppref=”${1%${2}*}”;
# from string($1) remove everything upto and including the suffix($3)
# ssuff contains, therefore,everything after the suffix
ssuff=”${1#*${3}}”;
# ppref+prefix($2) contains all characters upto the first #character of the substring we want to extract
# Therefore, remove them. nopref has all characters starting #from the first character of the substring
# we want to extract
nopref=”${1#${ppref}${2}}”;
#Finally from nopref remove all characters from suffix onwards
echo “${nopref%${3}${ssuff}}”;
else
echo “Usage: substr string prefix suffix”
fi
January 19th, 2008 at 3:49 am
Actually the above can be shortened to
#!/bin/bash
if [ $# == 3 ]
then
nopref=”${1#${1%${2}*}${2}}”;
echo “${nopref%${3}*}”;
else
echo “Usage: substr string prefix suffix”
fi
January 19th, 2008 at 4:13 am
Can be simpler:
#!/bin/bash
if [ $# == 3 ]
then
nopref=â€${1#*${2}}â€;
echo “${nopref%${3}*}â€;
else
echo “Usage: substr string prefix suffixâ€
fi
June 25th, 2008 at 6:49 pm
stringZ=abcABC123ABCabc # 0-based indexing.
echo ${stringZ:7} # 23ABCabc
# 3 chars of substring
echo ${stringZ:7:3} # 23A