Live Chat!

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.

12 Responses to “extracting substring at bash”

  1. deepak shenoy Says:

    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.

  2. You can use grep or awk before you extract a substring. It depends on how you manipulate it.

  3. “bellow”?

    ffs, it’s “below”. go check what “bellow” means in dict.org.

  4. sorry, typo :S

  5. 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’.

  6. Example for the above:

    substr “hello World” “el” “or”
    gives “lo W”

  7. 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. ;)

  8. 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}

  9. 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

  10. 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

  11. Can be simpler:

    #!/bin/bash
    if [ $# == 3 ]
    then
    nopref=”${1#*${2}}”;
    echo “${nopref%${3}*}”;
    else
    echo “Usage: substr string prefix suffix”
    fi

  12. stringZ=abcABC123ABCabc # 0-based indexing.

    echo ${stringZ:7} # 23ABCabc

    # 3 chars of substring
    echo ${stringZ:7:3} # 23A

Leave a Reply