[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to parse a string in unix and get  a value

Posted on 2014-04-27
5
Medium Priority
?
385 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 1600 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 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
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…
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.
Suggested Courses

873 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