Solved

How to find a value from a variable string in korn shell script

Posted on 2014-04-30
2
453 Views
Last Modified: 2014-05-15
I am writing a ksh script and the above solutions as mentioned in the my previous question How to parse a string in unix and get a value
 are not working for dynamic value search. I have modified the question to suit my requirement.

I have a string as below

MON_DT ="BD0 $DATE_BD0|BD1 $DATE_BD1|BD2 $DATE_BD2|BD3 $DATE_BD3|BD4 $DATE_BD4|BD5 $DATE_BD5"MON_DT

search string
CHK_DT='jun 25 2013'

Values of the Variables are like this.

DATE_BD0 = "apr 1 2014"
DATE_BD1 = "mar 31 2103"
DATE_BD2 = "Feb 3 2012"
DATE_BD3 = "Jan 22 2013"
DATE_BD4 = "may 21 2013"
DATE_BD5 = "jun 25 2013"
DATE_BD6 = "jul 27 2013"

Now I need to search $CHK_DT form the $MON_DT string and get the value BD5.
as mentioned in the example, As matter $CHK_DT dynamically changes we need to get the appropriate BD values accordingly.

As in the I want the Result as BD5.

Can some one Answer this query, this is very urgent for me. Thanks in advance.
0
Comment
Question by:welcome 123
2 Comments
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 250 total points
ID: 40032374
My first solution presented in the other Q needs just small modifications to work with the new data:

echo $MON_DT | awk -v S="$CHK_DT" -F'|' '{for(n=1;n<=NF;n++) if($n~S) print substr($n,1,index($n," "))}'

Or, if your shell supports it:

awk -v S="$CHK_DT" -F'|' '{for(n=1;n<=NF;n++) if($n~S) print substr($n,1,index($n," "))}' <<< $MON_DT

Please note the double quotes around "$CHK_DT" and the modified "print" part.
0
 
LVL 19

Accepted Solution

by:
simon3270 earned 250 total points
ID: 40032379
Assuming that you have set $MON_DT, and the date you are looking for is in $CHK_DT, then this will get the matching tag in $MATCH_TAG:
MATCH_TAG=
OIFS=$IFS
IFS='|'
for ent in $MON_DT; do
  IFS=$OIFS
  ent_tag=`echo $ent | sed 's/ .*//'`
  ent_val=`echo $ent | sed 's/^[^ ]* //'`
  if [ "$ent_val" = "$CHK_DT" ]; then
    MATCH_TAG="$ent_tag"
    break
  fi
done
IFS=$OIFS

Open in new window

If it doesn't find the date, $MATCH_TAG will be empty.

note that the date must match exactly (be the same case).  if the match should not be case sensitive, let me know and I'll add that.

(edit to correct typo in $CHK_DT)
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

786 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