Solved

How to parse a string in unix and get  a value

Posted on 2014-04-27
5
370 Views
Last Modified: 2014-05-03
I have a string as below

colors="BD0 BLUE|BD1 RED|BD2 YELLOW |BDX PINK"

Now I need to search for BD2 in the string and get the value Yellow, or BD0 and get BLUE


How can I do this using unix script using awk or sed?

I know we can store as key value pairs in perl, is there any way we do this in Unix also, so that I can store the above values as key values instead of string?
0
Comment
Question by:welcome 123
  • 2
  • 2
5 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40025590
I'm not sure what you're really after, but here is an awk script:

colors="BD0 BLUE|BD1 RED|BD2 YELLOW |BDX PINK"
S="BD2"
echo $colors | awk -F'|' -v S=$S '{for(n=1;n<=NF;n++) {if($n~S) print substr($n,index($n," ")+1)}}'
-- Alternatively, if your awk supports it:
echo $colors |awk -F'[ |]' -v S=$S '{for(n=1;n<=NF;n++) if($n~S) print $(n+1)}'

The variable "S" contains the search key. Fill in the desired value.
0
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 40025591
Not sure if your awk supports -v or not. If yes, please try the following:

awk -v var=$1 'BEGIN { FS="|"; } $1==var { print $2; }'

The first $1 is the input string you want to search for.
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 400 total points
ID: 40025767
You asked for "sed"? Here you go:

S="BD2"
echo $colors | sed 's/.*'$S' *\([a-zA-Z0-9]*\).*/\1/'
-- Alternatively, if your shell supports it (bash does):
sed 's/.*'$S' *\([a-zA-Z0-9]*\).*/\1/' <<< $colors
0
 

Author Comment

by:welcome 123
ID: 40032109
I am writing a ksh script and the above solutions don't work in ksh.
Actually the MON_DT DATE_BD0 , DATE_BD1  etc as variables containing dates with spaces.

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"


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

CHK_DT='jun 25 2013'

and I want the Result as BD5.

Can some one Answer this query, this is very urgent for me. Thanks in advance.
0
 
LVL 16

Assisted Solution

by:Peter Kwan
Peter Kwan earned 100 total points
ID: 40033786
You may try the following

IFS="|"
set -A array $MON_DT
for A in ${array[*]}; do 
   echo $A | grep $CHK_DT | awk '{ print $1 }'
done

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
nodeip 9 80
Upgrade RHEL from 6.7 to 6.8.... any benefit to having installed 6.8 to begin with? 2 876
MarkLogic 1 79
Linux "time" command output redirection 16 148
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
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…
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…
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.

803 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