[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Extract or replace keyword using AIX shell script

Posted on 2011-10-16
8
Medium Priority
?
356 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:cstsang
  • 5
  • 3
8 Comments
 
LVL 10

Expert Comment

by:Murugesan Nagarajan
ID: 36978113
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
 
LVL 10

Expert Comment

by:Murugesan Nagarajan
ID: 36978122
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
 
LVL 10

Expert Comment

by:Murugesan Nagarajan
ID: 36978132
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 10

Expert Comment

by:Murugesan Nagarajan
ID: 36978145
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
 
LVL 7

Author Comment

by:cstsang
ID: 36983435
Sorry, I want to clarify that I want to replace ~NDATE-x~ with the execution result of
/home/util/ngdate -x.
0
 
LVL 7

Author Comment

by:cstsang
ID: 36983483
Besides that, how can I know whether the string contain ~NDATE-x~?

thank you very much
0
 
LVL 7

Author Comment

by:cstsang
ID: 36983554
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
 
LVL 10

Accepted Solution

by:
Murugesan Nagarajan earned 2000 total points
ID: 36983873
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses
Course of the Month18 days, 19 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question