1

I have this file:

CLUSTERS=SP1,SP2,SP3

FNAME_SP1="REWARDS_BTS_SP1_<GTS>.dat"
FNAME_SP2="DUMP_LOG_SP2_<GTS>.dat"
FNAME_SP3="TEST_CASE_TABLE_SP3_<GTS>.dat"

What I want to get from these are:

REWARDS_BTS_SP1_
DUMP_LOG_SP2_
TEST_CASE_TABLE_SP3_

I loop through the CLUSTERS field, get the values, and use it to find the appropriate FNAME_<CLUSTERNAME> value. Basically, the CLUSTERS value are ALWAYS before the _<GTS> part of the string. Any string pattern will do, provided that the CLUSTERS value come before the _<GTS> at the end of the string.

Any suggestions? Here's a part of the script.

    function loadClusters() {
        for i in `echo ${!CLUSTER*}`
        do
            CLUSTER=`echo ${i} | grep $1`
            if [[ -n ${CLUSTER} ]]; then
                CLUSTER=${!i}
                break;
            fi
        done

        echo -e ${CLUSTER}
    }

    function loadClustersCampaign() {
        for i in `echo ${!BPOINTS*}`
            do
                BPOINTS=`echo ${i} | grep $1`
                if [[ -n ${BPOINTS} ]]; then
                    BPOINTS=${!i}
                    break;
                fi
        done

        for i in `echo ${!FNAME*}`
            do
                FNAME=`echo ${i} | grep $1`
                if [[ -n ${FNAME} ]]; then
                    FNAME=${!i}
                    break;
                fi
        done

        echo -e ${BPOINTS}"|"${FNAME}
    }

    #get clusters
    clusters=$(loadClusters $1)

    for i in `echo $clusters | sed 's/,/ /g'`
        do
            file=$(loadClustersCampaign ${i/-/_} | awk -F"|" '{print $2}') ;

            echo $file;

            #then get the part of the $file variable
        done

2 Answers 2

3

Fun with Shell Parameter Expansions

You can use matching-prefix notation and indirect expansion to get at the variables you want, and use the "remove suffix" expansion on each result to collect just the portions of the filename that you want. For example:

FNAME_SP1='REWARDS_BTS_SP1_<GTS>.dat'
FNAME_SP2='DUMP_LOG_SP2_<GTS>.dat'
FNAME_SP3='TEST_CASE_TABLE_SP3_<GTS>.dat'

for cluster in "${!FNAME_SP@}"; do
    echo ${!cluster%%<GTS>*}
done

This will print out the following:

REWARDS_BTS_SP1_
DUMP_LOG_SP2_
TEST_CASE_TABLE_SP3_

but you could issue any valid shell command inside the loop instead of using echo.

See Also

http://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html

Sign up to request clarification or add additional context in comments.

5 Comments

but what if I want to get just the REWARDS_BTS_, DUMP_LOG_, TEST_CASE_TABLE_ without the cluster name?
@MichaelGerona Just strip off more suffix. For example, "${!cluster%%SP?_<GTS>*}" will give you results without the cluster name.
i tried hardcoding the cluster value SP2, ${!cluster%%SP2_<GTS>*} but the second value won't show up. Cluster names can differ. It can be of any value. FIRE,STAT,BALL for example. I want to remove the FIRE from A_FILE_NAME_FIRE_<GTS>.dat so I can get A_FILE_NAME_ only. Any thoughts?
You can try ${!cluster%%${NAME}_<GTS>*} with NAME is everything you want (I've even tested with NAME="SP?" and it's ok). Works well on my cygwin.
You can trim from the second to last underscore, ${!cluster%_*_<GTS>.dat}. Maybe you need to escape or quote the wedges.
0

If you like an awk solution for this ,may be below will be useful.

> echo 'FNAME_SP1="REWARDS_BTS_SP1_<GTS>.dat"' | awk -F"<GTS>" '{split($1,a,"=\"");print substr(a[2],2)}'
REWARDS_BTS_SP1_

Furthur more detail below:

> cat temp
LUSTERS=SP1,SP2,SP3

    FNAME_SP1="REWARDS_BTS_SP1_<GTS>.dat"
    FNAME_SP2="DUMP_LOG_SP2_<GTS>.dat"
    FNAME_SP3="TEST_CASE_TABLE_SP3_<GTS>.dat"

> awk -F"<GTS>" '/FNAME_SP/{split($1,a,"=");print substr(a[2],2)}' temp
REWARDS_BTS_SP1_
DUMP_LOG_SP2_
TEST_CASE_TABLE_SP3_
>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.