Using Sed to do a substitution inside ksh

I'm trying to do an ls from inside of a ksh script. I loop through the results one line at a time and attempt to do a substitution using sed to convert YYYYMMDD from the older files into the newer files. Basically sometimes the ETL load runs over midnight and half the files are off by one day which breaks later stuff.

Anyway I fought through multiple problems with terminating and regex until I finally got everything running except it doesn't actually replace. Originally I had too many values in the PRE/POST vars and fixed that using the head-1 but still no luck as you can see from the output. I'm assuming that since if i replace the PRE/POST with 20111013 and 20111014 respectively that it does the substitution properly that there is some issue with the variable interpolation. I've tried using double quotes and the ${VAR} and various things and either there's no change or I get the s command not terminated.

I figured instead of fighting with it since most of the examples I Googled led me here that I would just register an account and ask.

Thanks in advance

-Cal

[petersoj@udytfx1t ~]$ vi test.ksh
#!/usr/bin/ksh

#Added
PREMID_DT_TM=""
POSTMID_DT_TM=""
TEMP=""

cd data

PREMID_DT_TM=`ls -pcr | head -1 | cut -d"/" -f2 | cut -d. -f3 | cut -c1-8`
echo "Pre ${PREMID_DT_TM}"

POSTMID_DT_TM=`ls -pc | head -1 | cut -d"/" -f2 | cut -d. -f3 | cut -c1-8`
echo "Post: ${POSTMID_DT_TM}"

echo "Starting loop"

for i in `ls -pcr`
do
echo "PRE; $PREMID_DT_TM and Post; $POSTMID_DT_TM"
        if [[ -e ${i} ]]
        then
        echo "Processing file ${i}"
        TEMP=`echo "$i" | sed -e 's/$PREMID_DT_TM/$POSTMID_DT_TM/'`
        # echo "Replacing $PREMID_DT_TM with $POSTMID_DT_TM = $TEMP"
        echo "$TEMP"
        fi
done

-------------------


[petersoj@udytfx1t ~]$ ./test.ksh
Pre 20111013
Post: 20111014
Starting loop
PRE; 20111013 and Post; 20111014
Processing file sigmatest.csv.2011101344524
sigmatest.csv.2011101344524
PRE; 20111013 and Post; 20111014
Processing file thetatest.csv.2011101345321
thetatest.csv.2011101345321
PRE; 20111013 and Post; 20111014
Processing file zed12test.csv.2011101454321
zed12test.csv.2011101454321
[petersoj@udytfx1t ~]$

Change teh "TEMP" assignment to:

TEMP=$(echo "${i}" | sed -e "s/$PREMID_DT_TM/$POSTMID_DT_TM/")
1 Like

well that was painless. I thought you needed the ` to run a command. Thanks!

edited