Cutting a string using more than one character as delimiter

Hi ,

I have a set of files in a folder which i need to cut in to two parts....

Sample files

touch AE_JUNFOR_2014_MTD_2013-05-30-03-30-02.TXT
touch AE_JUNFOR_2014_YTD_2013-05-30-03-30-02.TXT
touch temp_AE_JUNFOR_2014_MTD_2013-05-30-03-30-02.TXT
touch AE_ACTUAL_2013_APR_MTD_2013-05-03-09-59-44.TXT
touch AE_ACTUAL_2013_APR_YTD_2013-05-03-09-59-44.TXT
touch temp_AE_ACTUAL_2013_APR_MTD_2013-05-03-09-59-44.TXT

I need the file name in the above files to be sent to two variables.

Only temp* files are considered for the cut.

Sample file :- "temp_AE_ACTUAL_2013_APR_MTD_2013-05-03-09-59-44.TXT"

var1 = AE_ACTUAL_2013_APR
var 2 = 2013-05-03-09-59-44.TXT

I tried the below but Cut takes in only one character as delmiter... I cannot use "M" in MTD as a delimiter because for March files the file name will have MAR before MTD and the result will be wrong.

 echo temp_AE_ACTUAL_2013_APR_MTD_2013-05-03-09-59-44.TXT|cut -d "MTD" -f1

 echo temp_AE_ACTUAL_2013_APR_MTD_2013-05-03-09-59-44.TXT|cut -d "MTD" -f2

Please help me to find a efficient way to cut the file names .

Thanks
Arun

This loop splits apart on both _ and -, then puts back together looking for the token MTD:

OLDIFS="$IFS"
IFS="-_"
for FILE in temp*
do
        STR=""
        set -- $FILE
        shift
        while [ "$1" != "MTD" ]
        do
                STR="${STR}_$1"
                shift
        done
        shift

        STR1="${STR:1}"
        STR2="$*"
        echo "$STR1"
        echo "$STR2"
done
IFS="$OLDIFS"
$ echo temp_AE_ACTUAL_2013_APR_MTD_2013-05-03-09-59-44.TXT| awk -F"_MTD_" ' { print $1 } '
temp_AE_ACTUAL_2013_APR
$ echo temp_AE_ACTUAL_2013_APR_MTD_2013-05-03-09-59-44.TXT| awk -F"_MTD_" ' { print $2 } '
2013-05-03-09-59-44.TXT

Actually it should be...

1 Like

Thank you all ,

The AWK worked as expected.

Thank you all again.