Regular Expression needed for the xml


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regular Expression needed for the xml
# 1  
Old 07-03-2013
Regular Expression needed for the xml

I would like to match the regular below xml snippet by using the following grep command. I want match the lines as well could some please help me ..
grep -il "<tokenValue>.*.</tokenValue>\n...*.amey.*.</userName>"


<tokenValue>kfqcU9+akXJxFbPvh8x5EyLyu9dbNBg5bC0l8HlJA3ic7KnBnrYZ1aW7kArruzQhG+5lucFE</tokenValue>
<sessionStatus>Active</sessionStatus>
<loginTime>2013-07-03T09:36:18.378Z</loginTime>
</sessionToken>
<userName>amey</userName>
# 2  
Old 07-03-2013
Hello,

Could you please paste the expected output as requirement is not pretty clear.


Thanks,
R. Singh
# 3  
Old 07-03-2013
Hi Ravi,

Expected o/p is the same xml, after running the grep command.

<tokenValue>kfqcU9+akXJxFbPvh8x5EyLyu9dbNBg5bC0l8HlJA3ic7KnBnrYZ1aW7kArruzQhG+5lucFE</tokenValue>
<sessionStatus>Active</sessionStatus>
<loginTime>2013-07-03T09:36:18.378Z</loginTime>
</sessionToken>
<userName>amey</userName>
# 4  
Old 07-03-2013
Code:
sed -n '/<tokenValue>.*<\/tokenValue>/,/balaje<\/userName>/p' file

# 5  
Old 07-03-2013
Hello,

lets say we have a file named test1212 which have the data and we want to match data as per your requirement, so we can try the following code.

But this following code will search only words starting and ending.

Code:
 
awk '/\<sessionStatus\>/,/\<\/sessionStatus\>/' test1212

Similarly you can use <loginTime> and other tags to seacrh them in a specific file. Please let me know if you have any queries.




Thanks,
R. Singh
# 6  
Old 07-03-2013
Thanks Ravi and Balaji.

Its working but I have pretty lenghty xml and it matches everything, where ever the string occurs. I want these specific 5 lines in my output and not rest.

How that can be done? Also could you please what is does ' , ' coma means ? Is it a new line ?

---------- Post updated at 06:05 AM ---------- Previous update was at 05:27 AM ----------
Code:
awk '/\<tokenValue\>/,/\<sessionStatus\>Active\<\/sessionStatus\>/,/\<loginTime\>.*.\<\/loginTime\>/,/\<\/sessionToken\>/,/\<userName\>amey<\/userName\>/'  filename

I tried this its giving me syntax error. Could you please correct this ?
Code:
awk: /\<tokenValue\>/,/\<sessionStatus\>Active\<\/sessionStatus\>/,/\<loginTime\>.*.\<\/loginTime\>/,/\<\/sessionToken\>/,/\<userName\>amey<\/userName\>/
awk:                                                              ^ syntax error
awk: /\<tokenValue\>/,/\<sessionStatus\>Active\<\/sessionStatus\>/,/\<loginTime\>.*.\<\/loginTime\>/,/\<\/sessionToken\>/,/\<userName\>amey<\/userName\>/
awk:                                                                                                                     ^ syntax error


Last edited by Franklin52; 07-03-2013 at 08:28 AM.. Reason: Please use code tags
# 7  
Old 07-04-2013
/start_pattern/,/end_pattern/ -> tells the interpreter to match all the lines from (and including) the line matching start_pattern through to (and including) the line matching end_pattern.

Therefore, multiple /.../,/.../,/.../,/... is wrong

As an alternative, you can also use the -A switch of grep (available in GNU grep). The number after -A switch, is the number of lines to be displayed from file after the line that matches the pattern.
Code:
[user@host ~]$ cat file
<tokenValue>kfqcU9+akXJxFbPvh8x5EyLyu9dbNBg5bC0l8HlJA3ic7KnBnrYZ1aW7kArruzQhG+5lucFE</tokenValue>
<sessionStatus>Active</sessionStatus>
<loginTime>2013-07-03T09:36:18.378Z</loginTime>
</sessionToken>
<userName>amey</userName>
[user@host ~]$ grep -A2 "tokenValue" file
<tokenValue>kfqcU9+akXJxFbPvh8x5EyLyu9dbNBg5bC0l8HlJA3ic7KnBnrYZ1aW7kArruzQhG+5lucFE</tokenValue>
<sessionStatus>Active</sessionStatus>
<loginTime>2013-07-03T09:36:18.378Z</loginTime>
[user@host ~]$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regular expression needed

Hi Folks i have text file which have oracle table creation script for ex create table schema.table_name1 (col1 number,col2 number); create table schema.table_name2 (col3 number,col4 number); like that. here i want only 'table_name' . how can i find ? can you pls help the output... (3 Replies)
Discussion started by: coolboy98699
3 Replies

2. Shell Programming and Scripting

Regular expression for XML request and response

Hi Gents, I am having a xml request, I need to match the entire paragraph in unix regex. Can some one please provide me the regex for unix. I need to embed this in a shell script. Below is the XML. I am bit weak in regular expression. Regex *testameykamble* here testameykamble is my key... (8 Replies)
Discussion started by: ameyrk
8 Replies

3. UNIX for Advanced & Expert Users

sed: -e expression #1, char 0: no previous regular expression

Hello All, I'm trying to extract the lines between two consecutive elements of an array from a file. My array looks like: problem_arr=(PRS111 PRS213 PRS234) j=0 while } ] do k=`expr $j + 1` sed -n "/${problem_arr}/,/${problem_arr}/p" problemid.txt ---some operation goes... (11 Replies)
Discussion started by: InduInduIndu
11 Replies

4. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

5. UNIX for Dummies Questions & Answers

Regular expression help needed

Hi I need to simplify this reg expression is this right or thanks. (1 Reply)
Discussion started by: drew211
1 Replies

6. Shell Programming and Scripting

Integer expression expected: with regular expression

CA_RELEASE has a value of 6. I need to check if that this is a numeric value. if not error. source $CA_VERSION_DATA if * ] then echo "CA_RELESE $CA_RELEASE is invalid" exit -1 fi + source /etc/ncgl/ca_version_data ++ CA_PRODUCT_ID=samxts ++ CA_RELEASE=6 ++ CA_WEEK_NO=7 ++... (3 Replies)
Discussion started by: ketkee1985
3 Replies

7. Linux

Regular expression to extract "y" from "abc/x.y.z" .... i need regular expression

Regular expression to extract "y" from "abc/x.y.z" (2 Replies)
Discussion started by: rag84dec
2 Replies

8. Programming

help in regular expression

Hi all, I'm working with flex (version 2.5.4a) on GNU/linux. I need to frame a regular expression which would match cases where word "file" does not occur. Negated character class wont work for me because they enforce "or" clause between different chars (so something like wont work). I would like... (5 Replies)
Discussion started by: Rakesh Ranjan
5 Replies

9. Shell Programming and Scripting

regular expression help

hello all.. I'm a bit new to this site.. and I hope to learn alot.. but I've been having a hard time figuring this out. I'm horrible with regular expressions.. so any help would be greatly appreciated. I have a file with a list of names like this: LASTNAME, FIRSTNAME, MIDDLEINITIAL how can... (5 Replies)
Discussion started by: mac2118
5 Replies

10. Shell Programming and Scripting

Regular Expression + Aritmetical Expression

Is it possible to combine a regular expression with a aritmetical expression? For example, taking a 8-numbers caracter sequece and casting each output of a grep, comparing to a constant. THX! (2 Replies)
Discussion started by: Z0mby
2 Replies
Login or Register to Ask a Question