Solved

How to parse a string in unix and get  a value

Posted on 2014-04-27
5
373 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 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

Technology Partners: 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

Suggested Solutions

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
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.

735 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