Searching a delimited Key value pairs in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching a delimited Key value pairs in shell script
# 1  
Old 09-27-2010
Searching a delimited Key value pairs in shell script

Hello,

I have property file with key value pairs separated by pipe , I am trying to write a script which reads the property file and search and print value of specific key. I tried with Sed, I am successfull.

The file is as follows

SVR_NAME=server-1|SVR_DOMAIN=CELLDUMMY|SVR_HOST=server-1.tst.com|SVR_LISTENPORT=5000|SVR_VERSION=10.6

SVR_NAME=server-2|SVR_DOMAIN=CELLDUMMY|SVR_HOST=server-2.tst.com|SVR_LISTENPORT=5000|SVR_VERSION=10.6

let 's say I need to search SVR_HOST and print the value of SVR_HOST for server-2 (which is server-2.tst.com) . How can I do the in the script.

I tried to first search a line containing server-2 ,but not able to print "server-2.tst.com",it priniting whole line or priting SVR_NAME=server-2,I know I am doing mistake in trimming. I am not sure how to fix it.. Could you please help me on this ?

I am trying to write the script in UNIX,looking for shell script

Thanks,
ANK

Last edited by ANK; 09-27-2010 at 10:38 PM..
# 2  
Old 09-27-2010
Code:
$ ruby -F"\|" -ane 'puts $F[2] if $F[0] =~ /^.*server-2$/' file
SVR_HOST=server-2.tst.com

# 3  
Old 09-27-2010
is that in RUBY?

Quote:
Originally Posted by kurumi
Code:
$ ruby -F"\|" -ane 'puts $F[2] if $F[0] =~ /^.*server-2$/' file
SVR_HOST=server-2.tst.com

# 4  
Old 09-27-2010
Quote:
Originally Posted by ANK
is that in RUBY?
yes
# 5  
Old 09-28-2010
Code:
awk 'BEGIN{RS="|";FS="="} /SVR_HOST/ {print $2}' infile

server-1.tst.com
server-2.tst.com

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Match tab-delimited files based on key

I thought I had this figured out but was wrong so am humbly asking for help. The task is to add an additional column to FILE 1 based on records in FILE 2. The key is in COLUMN 1 for FILE 1 and in COLUMN 1 OR COLUMN 2 for FILE 2. I want to add the third column from FILE 2 to the beginning of... (8 Replies)
Discussion started by: andmal
8 Replies

2. Shell Programming and Scripting

Shell Script @ Find a key word and If the key word matches then replace next 7 lines only

Hi All, I have a XML file which is looks like as below. <<please see the attachment >> <?xml version="1.0" encoding="UTF-8"?> <esites> <esite> <name>XXX.com</name> <storeId>10001</storeId> <module> ... (4 Replies)
Discussion started by: Rajeev_hbk
4 Replies

3. Shell Programming and Scripting

Extracting key/value pairs in awk

I am extracting a number of key/value pairs in awk using following: awk ' /xyz_session_id/ { n=index($0,"xyz_session_id"); id=substr($0,n+15,25); a=$4; } END{ for (ix in a) { print a } }' I don't like this Index + substr with manually calculated... (5 Replies)
Discussion started by: migurus
5 Replies

4. Shell Programming and Scripting

Query related to the SSH key pairs

Hi All, I have question , How to generate the SSH keys for the two servers in the pair. So if the server will be replicated , same SSH key pairs will work on each server. No need to generate the SSH keys for the second server. I have created the SSH keys for the single server. I was... (1 Reply)
Discussion started by: sharsour
1 Replies

5. Shell Programming and Scripting

Parsing line with name:value pairs in shell script

HI , I have the following type of lines in a file and need to create a csv file that can be bcp'ed into a db The problem that I have is the delimited of the <name :value> is a space but some of the values in the pairs have space . eg msg_src_time:03/05/13 10:40:17.919 Need sugesstions on... (9 Replies)
Discussion started by: tasmac
9 Replies

6. UNIX for Advanced & Expert Users

Parse (delimited string) key-value pairs in a column into separate lines

Hi experts, e.g. i/p data looks like 0000xm7zcNDIkP888vRqGv93xA7:176n00qql||9700005405552747,9700005405717924,9700005405733788|unidentified,unidentified,unidentified|| o/p data should like - row1: 0000xm7zcNDIkP888vRqGv93xA7:176n00qql||9700005405552747|unidentified ... (1 Reply)
Discussion started by: sumoka
1 Replies

7. Shell Programming and Scripting

Shell Script help for Quotation Delimited File

I need help extracting a column in a file separated by quotations. I would like to extract the first column of data and write it to a flat text file. EXAMPLE of data: "c6e181396a1100ba19c53a6757a845c4","2012-05-18","email" "70879000563753bb9051b4ab8df43ac4","2012-05-18","email"... (5 Replies)
Discussion started by: nickytcom
5 Replies

8. Shell Programming and Scripting

Parsing /proc/net/dev into key:value pairs (self-answered)

Hi all, I need some help with using sed/awk/other linux tools to meet the following goal: I'm trying to take the output of /proc/net/dev: Inter-| Receive | Transmit face |bytes packets errs drop fifo frame compressed multicast|bytes ... (0 Replies)
Discussion started by: felbane
0 Replies

9. UNIX for Dummies Questions & Answers

Searching for text in a Space delimited File

Hi I am trying to search a firewall syslog space delimeted file for all of the different tcp and udp destination ports. I know that grep will find lines that contain specific text. And I have tried using the the the cut command to cut out of the file certain colums. However the test I am... (6 Replies)
Discussion started by: andyblaylock
6 Replies

10. UNIX for Dummies Questions & Answers

Searching for key word within a file

Hello, I have a directory that holds all of my matlab codes. I am trying to run a searh on all of the matlab files that have the word "while" written inside the sytax of the code. Looking for all of the times that I did a while loop. Can someone help me do this? Thanks in advance. ... (1 Reply)
Discussion started by: moradwan
1 Replies
Login or Register to Ask a Question