Grep Issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep Issue
# 1  
Old 04-11-2010
Grep Issue

Code:
<record>
<set>
<termId>1234</termId>
<termType>First</termType>
</set>
<set>
<termId>5678</termId>
<termType>Second</termType>
</set>
</record>

This is saved in record.xml

Hi

I have this sample XML that i am grepping using a shell program.

The objective of the task is - based on the <termType>, its corresponding <termId> should be grepped.

So, to get the value of <termId> 5678 when <termType> is Second.

However, I am getting 1234 along with 5678

How can I get only 5678 as desired output

This is how I am doing it in shell script

egrep "<termId>|<termType>Second" record.xml

I want this to just show 5678 and not 1234

Please advise with suggestion

Thanks a lot

Last edited by Franklin52; 04-12-2010 at 03:48 AM.. Reason: Please use code tags!
# 2  
Old 04-11-2010
Hi
I dont think you can get this using egrep. Try the one shown below:

Code:
sed -ne '/<termType>Second/{x;1!p;}' -e h record.xml


Last edited by Franklin52; 04-12-2010 at 03:47 AM.. Reason: Please use code tags, thank you!
# 3  
Old 04-11-2010
First of all - Thanks a ton here..

I can see it working with "Second" hard coded - However, if i have it passed as a variable, I am not getting the results back

Tried these -
Code:
pattern="Second";
sed -ne '/<termType>$pattern/{x;1!p;}' -e h record.xml

sed -ne '/<termType>"$pattern"/{x;1!p;}' -e h record.xml

sed -ne '/<termType>{$pattern}/{x;1!p;}' -e h record.xml


Last edited by Franklin52; 04-12-2010 at 03:47 AM.. Reason: Please use code tags!
# 4  
Old 04-11-2010
You can use the grep (or egrep) -B argument to to look one line above the search string and only search for <termType>Second". The downside is that you get the "<termType>Second</termType>" line and you didn't specify whether that's ok or not.


Code:
# echo '<record>
<set>
<termId>1234</termId>
<termType>First</termType>
</set>
<set>
<termId>5678</termId>
<termType>Second</termType>
</set>
</record>' | egrep -B 1 "<termType>Second"
<termId>5678</termId>
<termType>Second</termType>
#

# 5  
Old 04-11-2010
Anytime you start embedding values into a regular expression, you need to keep in mind that the regular expression will break if special characters are present and unescaped. In cases like these, if possible, it's best to use simple string comparisons when dealing with such values (k==$2 in the code that follows).

My personal preference would be:
Code:
$ key=Second
$ awk -F'</?[^>]*>' '/^<termId>/ {id=$2} /^<termType>/ && k==$2 {print id; exit}' k="$key" record.xml 
5678

Regards,
Alister

Last edited by alister; 04-11-2010 at 08:15 PM..
# 6  
Old 04-11-2010
Hi All Unix Guru's

Thanks a ton here.. both above solutions work here - Thanks to alister and fubaya - You are awesome - appreciate feedback from other contributors on this thread

The problem seems to be resolved
# 7  
Old 04-12-2010
For your question,

Tried these -
Code:
pattern="Second";
sed -ne '/<termType>$pattern/{x;1!p;}' -e h record.xml

sed -ne '/<termType>"$pattern"/{x;1!p;}' -e h record.xml

sed -ne '/<termType>{$pattern}/{x;1!p;}' -e h record.xml

You could have overcome this by using like this:

Code:
sed -ne '/<termType>'$pattern'/{x;1!p;}' -e h record.xml


Thanks
Guru.

Last edited by Franklin52; 04-12-2010 at 03:47 AM.. Reason: Please use code tags, thank you!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Issue with Grep

Hi guys, Hope someone can help me with this - I'm sure it's fairly simple but it's driving me mad! (forgive the coding - still new on scripting - come from Windows) I have the following coding for checking whether I want to include a line in a file:- EXTRACT_Date=$(date --date="${PERIOD}"... (6 Replies)
Discussion started by: NickF
6 Replies

2. Shell Programming and Scripting

Issue with grep

Hello, I have an input file that looks like so: LDLR LDLRAD4 VLDLR when I grep "LDLR" I get an output of: LDLR LDLRAD4 VLDLR Since all names have "LDLR" included within them, but all I want the output to be is LDLR I know it can work if I surround the words with pipes for... (5 Replies)
Discussion started by: Rabu
5 Replies

3. UNIX for Dummies Questions & Answers

Grep issue

HI, I have a command to check a license file. License_print. In that file you get the headlines and all different licenses. Now i want to have things extracted from it. so i do like following: license_print | grep -iw -e "user" -e "admin" But i donīt want all lines where user is... (11 Replies)
Discussion started by: Tzwaj
11 Replies

4. Shell Programming and Scripting

Grep issue

Hi Guys, I am new to shell scripting. Need help on grep command. I had a file called file.log which contain below statements. 12 Nov 2013 14:12:17,756 INFO security - Userid: raja, Saved File Instance, Name: , Registry: 23 Nov 2013 14:14:11,777 INFO security - Userid: raja, Saved... (7 Replies)
Discussion started by: Vinoth Kumar G
7 Replies

5. Shell Programming and Scripting

Issue in grep

i have following pattern in file s6:s2 s2:s4 s1:s2:s3:s4:s5:s6 s1 . . Now i want to find occurence of each record in file like s6:s2 occurs twice {once in first record and both occur in 3 record as well} so output should be s6:s2 2 s2:s4 2 s1:s2:s3:s4:s5:s6 :1 s1 : 2 ... (7 Replies)
Discussion started by: sharad.40216
7 Replies

6. Shell Programming and Scripting

Grep issue

Hi All I have a file containing following records: $HEW_TGT_DB2_USER=hbme_bi2 $prmAttunityUser=ais $DS_USER=hbme_bi2 $prmStgUser=hbme_bi2 $prmuser=hbme_bi2 $prmStgPass=hbme_bi2 $prmpwd=hbme_bi2 $prmAttunityUser=ais Say suppose the name of the file is test4.txt When i fire this... (2 Replies)
Discussion started by: vee_789
2 Replies

7. UNIX for Dummies Questions & Answers

Grep issue

more Hello.txt it was a sunny way and i was about to go home. I need to grep and redirect to a new file all the text between 'sunny' and 'go' string above. Note: There may be multiple lines in between the string i need to grep between. If there are multiple 'go' strings it should grep till... (9 Replies)
Discussion started by: mohtashims
9 Replies

8. Shell Programming and Scripting

grep issue

The below command is not working stackmem="$(pmap $1 | grep -i '' | awk '{print $2}'| tr -d ' K')" I need to grep strictly for ----> Regards, Mohtashim (2 Replies)
Discussion started by: mohtashims
2 Replies

9. UNIX for Dummies Questions & Answers

Issue with grep

I have a file that has the following: 591066 100.0 591066 100.0 591066 100.0 591066 100.0 591066 100.0 591066 100.0 591066 100.0 591066 100.0 591066 100.0 591066 100.0 591066 100.0 ... (5 Replies)
Discussion started by: Pablo_beezo
5 Replies

10. UNIX for Dummies Questions & Answers

issue with grep

using grep, i have a file emp.lst, and i want all those records where "S" or "s" (capital or small) is not there i used this grep emp.lst when i use grep emp.lst i am getting rows with S..but why negate (^) is not working? (3 Replies)
Discussion started by: soujanya_srk
3 Replies
Login or Register to Ask a Question