Solved

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

Posted on 2014-04-30
2
452 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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.

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now