Using Sed to do a substitution inside ksh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using Sed to do a substitution inside ksh
# 1  
Old 10-17-2011
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

Code:
[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 ~]$

# 2  
Old 10-17-2011
Change teh "TEMP" assignment to:
Code:
TEMP=$(echo "${i}" | sed -e "s/$PREMID_DT_TM/$POSTMID_DT_TM/")

This User Gave Thanks to Shell_Life For This Post:
# 3  
Old 10-17-2011
well that was painless. I thought you needed the ` to run a command. Thanks!
# 4  
Old 10-18-2011
edited

Last edited by Calbrenar; 10-19-2011 at 11:45 AM.. Reason: made new thread since different problem
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command not working inside ksh script but works fine outside

Hi, I am a bit confused ,why would a sed command work fine outside of ksh script but not inside. e.g I want to replace all the characters which end with a value and have space at end of it. so my command for it is : sed -i "s/$SEPARATOR /$SEPARATOR/g" file_name This is working fine in... (8 Replies)
Discussion started by: vital_parsley
8 Replies

2. Shell Programming and Scripting

Command substitution inside of a variable expression (AIX, KORN)

Hello all. This is my first post/question on this site. I’m a new Systems Analyst with previous experience with BASH. Although now I'm using AIX, and I’m trying to get a feel for the Korn shell (for those of you that don’t know AIX only uses the KORN shell). I hope I put this into the correct... (10 Replies)
Discussion started by: sydox
10 Replies

3. UNIX for Dummies Questions & Answers

Variable inside command substitution

Hello people. Part of my script: SUBSETID=`echo $PMFILE |sed 's/pmresult_//' | sed 's/_*//'` MAPFILE=`find /huawei/cell /huawei/nodeb /huawei/rnc -name 'mapping_$SUBSETID.txt' |grep -v backup` Unfortunatelly variable $SUBSETID in the MAPFILE declaration will not return the... (3 Replies)
Discussion started by: drbiloukos
3 Replies

4. Shell Programming and Scripting

Using commands inside sed substitution

Dear Friends, Please let me know how to use the date command inside the substitution flag replacement string. echo "01 Jan 2003:11:00:06 +0100" | sed 's/\(.*\)/`date -d \1 "+%Y%m%d%H%M%S"`/' I want to supply \1 string to Here mention below as part of replacement string, date -d <Here>... (5 Replies)
Discussion started by: tamil.pamaran
5 Replies

5. Shell Programming and Scripting

KSH: substitution character, AWK or SED?

Hi Gurus, I am working with a korn shell script. I should replace in a very great file the character ";" with a space. Example: 2750;~ 2734;~ 2778;~ 2751;~ 2751;~ 2752;~ what the fastest method is? Sed? Awk? Speed is dead main point, Seen the dimensions of the files Thanks (6 Replies)
Discussion started by: GERMANICO
6 Replies

6. Shell Programming and Scripting

variable substitution in ksh

Hi I have a variable BIT1 which holds some value. Is there a way to retrieve the value of this variable indirectly via another variable, lets say SUBSET_BIT_NUM=1, so the call will look something like this: sundev1 $ echo ${BIT${SUBSET_BIT_NUM}} ksh: ${BIT${SUBSET_BIT_NUM}}: bad substitution ... (3 Replies)
Discussion started by: aoussenko
3 Replies

7. Shell Programming and Scripting

String substitution on find results inside exec/xargs

What I'm trying to do is perform a copy, well a ditto actually, on the results of a find command, but some inline string substitution needs to happen. So if I run this code find ./ -name "*.tif" I get back these results. .//1234567.tif .//abcdefg.tif Now the action from exec or xargs I... (2 Replies)
Discussion started by: myndcraft
2 Replies

8. Solaris

Substitution not working in ksh

Hi, Following code is working in bash but not in ksh. Can someone please send me an alternative? #!/bin/ksh fname="EOA.dmp" echo $fname logname=${fname/.dmp/.log} echo $logname I am getting below error in ksh "testcmd: logname=${fname/.dmp/.log}: 0403-011 The specified substitution... (3 Replies)
Discussion started by: arsheshadri
3 Replies

9. Shell Programming and Scripting

KSH variable substitution

Hi folks Please let me know if anyone knows how to handle this. My KSH script -> testscript.ksh cmd=$1 ENV="devl" echo $cmd This is how I call the script ./testscript.ksh 'ps -ef | grep br$ENV' How do I get this to print the below text i.e $ENV should be substituted with the value... (5 Replies)
Discussion started by: tipsy
5 Replies

10. Shell Programming and Scripting

ksh substitution

Hello, I thought it was possible to use several time a #! entry on a script but it doesn't seems to work. My need is to have a part of a ksh script without substitution so it would look like #!/bin/ksh -- first part --- #!/bin/ksh -f -- part without substitution -- #!/bin/ksh --... (2 Replies)
Discussion started by: solea
2 Replies
Login or Register to Ask a Question