Solved

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

Posted on 2014-04-30
2
462 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
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…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.

690 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