Extract or replace keyword using AIX shell script

I have a string like this:
/home/op/grib/data F~NDATE-1~*C034P??.grb

I want to replace the string NDATE-1, with the execution result of :

/home/util/ngdate -1

that mean if the string change to
/home/op/grib/data F~NDATE-3~*C034P??.grb

the substring NDATE-3 is replaced by the execution result of:
/home/util/ngdate -3

Moreover,if the string change to
/home/op/grib/data F~NDATE+1~*C034P??.grb
the substring NDATE+1 is replaced by the execution result of:
/home/util/ngdate +1

is this possible to do the task?

thank you very much
LVL 7
KNVB HKSITOAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

murugesandinsC C++ Shell script AIX CYGWIN_NT HP-UX Linux MINGW SunOSCommented:
Here goes the script:

#!/bin/ksh
string1="/home/op/grib/data F~NDATE-1~*C034P??.grb"
echo resunt of string1
echo $string1 | sed 's/\(\/home\/op\/grib\/data \)\(.*NDATE\)\([+|-]\)\([0-9]*\)\(.*\)/\/home\/util\/ngdate \3\4/'
string2="/home/op/grib/data F~NDATE+1~*C034P??.grb"
echo resunt of string2
echo $string2 | sed 's/\(\/home\/op\/grib\/data \)\(.*NDATE\)\([+|-]\)\([0-9]*\)\(.*\)/\/home\/util\/ngdate \3\4/'
string3="/home/op/grib/data F~NDATE+3~*C034P??.grb"
echo resunt of string3
echo $string3 | sed 's/\(\/home\/op\/grib\/data \)\(.*NDATE\)\([+|-]\)\([0-9]*\)\(.*\)/\/home\/util\/ngdate \3\4/'
string4="/home/op/grib/data F~NDATE+66~*C034P??.grb"
echo resunt of string4
echo $string4 | sed 's/\(\/home\/op\/grib\/data \)\(.*NDATE\)\([+|-]\)\([0-9]*\)\(.*\)/\/home\/util\/ngdate \3\4/'
string4="/home/op/grib/data F~NDATE+123456~*C034P??.grb"
echo resunt of string4
echo $string4 | sed 's/\(\/home\/op\/grib\/data \)\(.*NDATE\)\([+|-]\)\([0-9]*\)\(.*\)/\/home\/util\/ngdate \3\4/'

Open in new window


and the output:
./test.sh
resunt of string1
/home/util/ngdate -1
resunt of string2
/home/util/ngdate +1
resunt of string3
/home/util/ngdate +3
resunt of string4
/home/util/ngdate +66
resunt of string4
/home/util/ngdate +123456

Open in new window

0
murugesandinsC C++ Shell script AIX CYGWIN_NT HP-UX Linux MINGW SunOSCommented:
Updated script with changes for
1. spell mistakes (resunt=>result)
2. string5 variable name.

#!/bin/ksh
string1="/home/op/grib/data F~NDATE-1~*C034P??.grb"
echo result of string1
echo $string1 | sed 's/\(\/home\/op\/grib\/data \)\(.*NDATE\)\([+|-]\)\([0-9]*\)\(.*\)/\/home\/util\/ngdate \3\4/'
string2="/home/op/grib/data F~NDATE+1~*C034P??.grb"
echo result of string2
echo $string2 | sed 's/\(\/home\/op\/grib\/data \)\(.*NDATE\)\([+|-]\)\([0-9]*\)\(.*\)/\/home\/util\/ngdate \3\4/'
string3="/home/op/grib/data F~NDATE+3~*C034P??.grb"
echo result of string3
echo $string3 | sed 's/\(\/home\/op\/grib\/data \)\(.*NDATE\)\([+|-]\)\([0-9]*\)\(.*\)/\/home\/util\/ngdate \3\4/'
string4="/home/op/grib/data F~NDATE+66~*C034P??.grb"
echo result of string4
echo $string4 | sed 's/\(\/home\/op\/grib\/data \)\(.*NDATE\)\([+|-]\)\([0-9]*\)\(.*\)/\/home\/util\/ngdate \3\4/'
string5="/home/op/grib/data F~NDATE+123456~*C034P??.grb"
echo result of string5
echo $string5 | sed 's/\(\/home\/op\/grib\/data \)\(.*NDATE\)\([+|-]\)\([0-9]*\)\(.*\)/\/home\/util\/ngdate \3\4/'

Open in new window

and the corresponding output:

test.sh
result of string1
/home/util/ngdate -1
result of string2
/home/util/ngdate +1
result of string3
/home/util/ngdate +3
result of string4
/home/util/ngdate +66
result of string5
/home/util/ngdate +123456

Open in new window

0
murugesandinsC C++ Shell script AIX CYGWIN_NT HP-UX Linux MINGW SunOSCommented:
Updated the script for the following output:
result of string1
/home/util/ngdate -1
result of string2
/home/util/ngdate +1
result of string3
/home/util/ngdate +3
[b]
result of string4
/home/util/ngdate +6
result of string5
/home/util/ngdate +1
[/b]

Open in new window


#!/bin/ksh
string1="/home/op/grib/data F~NDATE-1~*C034P??.grb"
echo result of string1
echo $string1 | sed 's/\(\/home\/op\/grib\/data \)\(.*NDATE\)\([+|-]\)\([0-9]\)\(.*\)/\/home\/util\/ngdate \3\4/'
string2="/home/op/grib/data F~NDATE+1~*C034P??.grb"
echo result of string2
echo $string2 | sed 's/\(\/home\/op\/grib\/data \)\(.*NDATE\)\([+|-]\)\([0-9]\)\(.*\)/\/home\/util\/ngdate \3\4/'
string3="/home/op/grib/data F~NDATE+3~*C034P??.grb"
echo result of string3
echo $string3 | sed 's/\(\/home\/op\/grib\/data \)\(.*NDATE\)\([+|-]\)\([0-9]\)\(.*\)/\/home\/util\/ngdate \3\4/'
string4="/home/op/grib/data F~NDATE+66~*C034P??.grb"
echo result of string4
echo $string4 | sed 's/\(\/home\/op\/grib\/data \)\(.*NDATE\)\([+|-]\)\([0-9]\)\(.*\)/\/home\/util\/ngdate \3\4/'
string5="/home/op/grib/data F~NDATE+123456~*C034P??.grb"
echo result of string5
echo $string5 | sed 's/\(\/home\/op\/grib\/data \)\(.*NDATE\)\([+|-]\)\([0-9]\)\(.*\)/\/home\/util\/ngdate \3\4/'

Open in new window

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

murugesandinsC C++ Shell script AIX CYGWIN_NT HP-UX Linux MINGW SunOSCommented:
Remove "" and "" from "Updated the script for the following output:" posted at
"10/17/11 01:35 PM, ID: 36978132"




result of string1
/home/util/ngdate -1
result of string2
/home/util/ngdate +1
result of string3
/home/util/ngdate +3
result of string4
/home/util/ngdate +6
result of string5
/home/util/ngdate +1
0
KNVB HKSITOAuthor Commented:
Sorry, I want to clarify that I want to replace ~NDATE-x~ with the execution result of
/home/util/ngdate -x.
0
KNVB HKSITOAuthor Commented:
Besides that, how can I know whether the string contain ~NDATE-x~?

thank you very much
0
KNVB HKSITOAuthor Commented:
The following code is done the task well, however, can we make it simpler?
string1="/home/op/grib/data F~NDATE-3~*C034P??.grb"
result=`echo $string1 | sed 's/\(.*\~NDATE\)\([+|-]\)\([0-9]\)\(\~.*\)/\/home\/util\/ngdate \2\3/'`
echo $result
result=`$result 2>/dev/null`
if [ $? -eq 0 ]
then
	string1=`echo $string1 | sed "s/\~NDATE[+|-][0-9]\~/"$result"/"`
	echo $string1
fi

Open in new window

0
murugesandinsC C++ Shell script AIX CYGWIN_NT HP-UX Linux MINGW SunOSCommented:
Here it goes:
#/bin/ksh
string1="/home/op/grib/data F~NDATE-3~*C034P??.grb"
result=`echo $string1 | sed 's/\(.*\~NDATE\)\([+|-]\)\([0-9]\)\(\~.*\)/\/home\/util\/ngdate \2\3/'`
echo "Value of result:  "$result

Open in new window

OUTPUT:
./out1.sh
Value of result:        /home/util/ngdate -3

Open in new window



Large lines with exception handling:
#/bin/ksh
testString ()
{
        if [ $# -ne 1 ] || [ "$1" = "" ]
        then
                result="Pass one parameter to testString"
                return 1
        fi
        var1="$1"
        result=`echo $var1 | sed 's/\(.*\~NDATE\)\([+|-]\)\([0-9]\)\(\~.*\)/\/home\/util\/ngdate \2\3/'`
        echo $result | egrep "/home/util/ngdate [+-][0-9]$" > /dev/null 2>&1
        if [ $? -ne 0 ]
        then
                result="Invalid parameter passed to testString:$var1"
                return 2
        fi
        return 0
}
testString "/home/op/grib/data F~NDATE+3999~*C034P??.grb"
ret="$?"
if [ $ret -eq 0 ]
then
        echo output of testString "/home/op/grib/data F~NDATE+3999~*C034P??.grb"
        echo $result
elif [ $ret -eq 1 ]
then
        echo output of testString "/home/op/grib/data F~NDATE+3999~*C034P??.grb"
        echo $result
elif [ $ret -eq 2 ]
then
        echo output of testString "/home/op/grib/data F~NDATE+3999~*C034P??.grb"
        echo $result
fi
testString "/home/op/grib/data F~NDATE-3~*C034P??.grb"
ret="$?"
if [ $ret -eq 0 ]
then
        echo output of testString "/home/op/grib/data F~NDATE+3999~*C034P??.grb"
        echo $result
elif [ $ret -eq 1 ]
then
        echo output of testString "/home/op/grib/data F~NDATE+3999~*C034P??.grb"
        echo $result
elif [ $ret -eq 2 ]
then
        echo output of testString "/home/op/grib/data F~NDATE+3999~*C034P??.grb"
        echo $result
fi

Open in new window


OUTPUT:
./out2.sh
output of testString /home/op/grib/data F~NDATE+3999~*C034P??.grb
Invalid parameter passed to testString:/home/op/grib/data F~NDATE+3999~*C034P??.grb
output of testString /home/op/grib/data F~NDATE+3999~*C034P??.grb
/home/util/ngdate -3

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.