extracting substring at bash

June 28th, 2007 mysurface Posted in Bash, Text Manipulation | Hits: 128736 | 14 Comments »

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.

14 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

  13. testcase.xml:

    123456789
    Toto

    sed -n ‘s/.*\([^<]*\).*/\1/p’ testcase.xml

  14. Hi

    Hello experts. I am a new bee, I don’t know much about Unix, if my question looks silly please bear with me.

    I have a question, if someone can assist, I really appreciate.

    Scenario :

    We get 3 .txt files and we merge it into one file on a daily basis from a script. On all the files the date is suffixed (eg. ab_tran_bai_20110816.txt, ab_depchks_bai_20110816.txt). The filename remains same except the date portion.

    Expected Solution :

    In the merge script we change the date and run the script

    DT=20110816

    TRANSFILE=ab_tran_bai_${DT}.txt
    DEPCHECKS=ab_depchks_bai_${DT}.txt
    MERGEDFILE=ab_merge_${DT}.txt
    ACHFILE=ab_tran_bai2ach_${DT}.txt

    Instead of changing the date manually every day, I want to read from the date portion from the file name.

    $ xy=ab_depchks*
    $ echo $xy
    ab_depchks_bai_20110816.txt
    $ echo ${xy:15:8} — it does work, when we do the echo it shows complete filename, when we do the substring it takes only “ab_depchks”

    Is there a way to take only the date (20110816) using the wildcard. If we give the complete file name xy=ab_depchks_bai_20110816.txt, we can get the date portion using substring.

    Thanks in advance,

    Datta

Leave a Reply