Solved

How to parse a string in unix and get  a value

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

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…
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 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 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.:

744 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

12 Experts available now in Live!

Get 1:1 Help Now