Extracting value from a sting using perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting value from a sting using perl
# 1  
Old 03-02-2009
Extracting value from a sting using perl

Hi Experts,

I want to extract the value of SESSIONID AND USERID from the this string using perl.

Code:
XML Request [<POLICYREQ><MESSAGEID>1</MESSAGEID><CALLERID>4 - MLS - MLS: com.arcot.aok.web.controllers.rf.RFSyncClientDIDController|1:3133050</CALLERID><SESSIONID>1:3133050</SESSIONID><AUTHRESULT>UNSPECIFIED</AUTHRESULT><IDENTITY><USERID>01451651</USERID><GROUP>MLS</GROUP><SUBGROUP>SUBGROUP2</SUBGROUP></IDENTITY><LOCATION><IPADDRESS>67.95.201.98</IPADDRESS></LOCATION><DEVICEID type="http">huRjakT0M1xjmD+Wpwgb+mQFAmFPW+4+76/2vAp46qzN6ThQwfc75AGpDUsxxi8Z</DEVICEID><DEVICESIG>{&quot;DEVICESIG&quot;:{&quot;VERSION&quot;:&quot;1.5&quot;,&quot;OS_BROWSER&quot;:{&quot;browser_ver&quot;:&quot;7.0&quot;,&quot;os&quot;:&quot;Windows NT 5.1&quot;,&quot;browser&quot;:&quot;Microsoft Internet Explorer&quot;,&quot;browser_min_ver&quot;:&quot;0&quot;},&quot;SCREEN&quot;:{&quot;colorDepth&quot;:&quot;32&quot;,&quot;availWidth&quot;:&quot;1280&quot;,&quot;width&quot;:&quot;1280&quot;,&quot;height&quot;:&quot;1024&quot;,&quot;availHeight&quot;:&quot;994&quot;},&quot;HTTP_HEADER&quot;:{&quot;user-agent&quot;:&quot;Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)&quot;},&quot;OPTIONAL&quot;:{&quot;SOFTWARE&quot;:[]},&quot;USER_PREF&quot;:{&quot;sys_lang&quot;:&quot;en-us&quot;,&quot;timezone&quot;:&quot;480&quot;,&quot;user_lang&quot;:&quot;en-us&quot;}}}</DEVICESIG></POLICYREQ>]

The answer should be SessionID=1:3133050 and USERID=01451651
Please advise how to do so, I want to read them in a variable.

Thanks
NT

Last edited by namishtiwari; 03-02-2009 at 10:17 AM.. Reason: Put the desired answer.
# 2  
Old 03-02-2009
Here is one way. There may be better ones. This one is fairly lazy, but will work if your data layout doesn't change.

Code:
cat test.txt | perl -pe 's/^.*<SESSIONID>([^<]+)<.*<USERID>([^<]+)<.*$/$1 $2/'

If you need it to be in separate variables, you can then split on the whitespace (or replace that with a comma and split on that).
# 3  
Old 03-02-2009
Code:
use strict;
my $str='your string here;
print "SESSION_ID=$1 USERID=$2\n" if $str=~/(?:.*<SESSIONID>)(.*?)(?:<\/SESSIONID>.*<USERID>)(.*)(?:<\/USERID>.*$)/;

# 4  
Old 03-03-2009
Thank you very much for the help Summer_Cherry and ShawnMilo.

Thanks
NT
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl script - Help me extracting a string

I have input like this : TNS Ping Utility for Linux: Version 11.2.0.1.0 - Production on 07-FEB-2012 04:19:45 Copyright (c) 1997, 2009, Oracle. All rights reserved. Used parameter files: /t3local_apps/apps/oracle/product/11.2.0/network/admin/sqlnet.ora Used TNSNAMES adapter to resolve... (3 Replies)
Discussion started by: dnam9917
3 Replies

2. Shell Programming and Scripting

Replace sting in a file

Hi Gurus, I need to search and replace string <! in a file with a blank. Some how my command sed -i 's/\<\!\ is not working .. Kindly help (1 Reply)
Discussion started by: r_t_1601
1 Replies

3. UNIX for Dummies Questions & Answers

extracting values from file using perl

I would like to read value after $ symbol from a file. For eg a file will be having the following lines. 5,$0,-32768p,32767p,$400014f8,1,, 5,$0,0,0,$400008ce,1,, 5,$0,0,0,$400008d0,1,, i would like to read 400014f8 then 400014f8 and similar all the value after $ symbol.Can any one help... (1 Reply)
Discussion started by: jagadeeshrr
1 Replies

4. Shell Programming and Scripting

Extracting column value from perl

Hello Kindly help me to find out the first column from first line of a flat file in perl I/P 9869912|20110830|00000000000013009|130|09|10/15/2010 12:36:22|W860944|N|00 9869912|20110830|00000000000013013|130|13|10/15/2010 12:36:22|W860944|N|00... (5 Replies)
Discussion started by: Pratik4891
5 Replies

5. Shell Programming and Scripting

Inserting a string in another sting

Hi Experts, I need to insert a sting into another string at a specified position. Like the below. Regards, Tin-Tin (3 Replies)
Discussion started by: tinufarid
3 Replies

6. Shell Programming and Scripting

Perl: Extracting a char from a string.

I want to extract a character from a string. For a C/C++ programmer like me, this would seem to be the most logical and reasonable way--but it doesn't work: $s = "abcdefg"; $c = $s; This way is documented and it works, but it seems clumsy. Is there any simpler way to grab one character out... (7 Replies)
Discussion started by: KenJackson
7 Replies

7. Shell Programming and Scripting

How to grep sting (*) ?

Hi all, I am trying to grep something from log file. For example. i need to grep sting = "*834*1" My LOG 2010-05-05 09:27:57,649|3|ABCD834|192.168.38.72|2365|444850623031|0|8|-|0|*834*1*1499413*00853662524#|470540.7219101273026475158 2010-05-05... (3 Replies)
Discussion started by: ooilinlove
3 Replies

8. Shell Programming and Scripting

Assign a sting to a name?

i want to assign a anme for a string, i got the things below, but it doesn't work. MParc1=`` if then $MPrac1=`Prac1` # assign $MPrac1 to Prac1 else $MPrac1=`` #assigne $MPrac1 to nothing fi echo "${MPrac1} >>file Output: (if num is 0) Prac1 can you please... (6 Replies)
Discussion started by: mingming88
6 Replies

9. UNIX for Dummies Questions & Answers

extracting selected few lines through perl

How can I extract few lines(like 10 to 15, top 10 and last 10) from a file using perl. I do it with sed, head and tail in unix scripting. I am new to perl. Appreciate your help. (2 Replies)
Discussion started by: paruthiveeran
2 Replies

10. Shell Programming and Scripting

extracting used perl modules

Hi, I am trying to extract the includes from all the perl programs in my application space;ie. i want the list of all used modules in my application. First i did a grep like 'grep -r "^use.*;$" * > ~dileepp/search.txt' which gives me the output in the form "filename:line containing use"... (0 Replies)
Discussion started by: DILEEP410
0 Replies
Login or Register to Ask a Question