Solved

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

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
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…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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…

706 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

16 Experts available now in Live!

Get 1:1 Help Now