[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to parse a string in unix and get  a value

Posted on 2014-04-27
5
Medium Priority
?
382 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
[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
  • 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’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
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 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.:
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…

656 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