Help using perl commandline for XML matching


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help using perl commandline for XML matching
# 1  
Old 03-27-2009
Help using perl commandline for XML matching

I have a file that contains this.

<NAME>/bob</NAME>

I'm trying to print just the /bob part to my screen. I have a command line example I really think should work. Keep in mind that the content between the <NAME> </NAME> is always changing.

$/tmp> perl -ne 'print /<NAME>($.)<\/NAME>/' file

Expected output: /bob

I'm not sure what I'm doing wrong. Many thanks for help.

-x96riley3
# 2  
Old 03-28-2009
This works for me:

perl -ne "print /<NAME>(.*)<\/NAME>/" file.txt
/bob

In general regular expressions are poor at parsing XML & HTML, but for a one-liner hack, whatever works for you is ok.
# 3  
Old 03-28-2009
Quote:
Originally Posted by x96riley3
[...]
$/tmp> perl -ne 'print /<NAME>($.)<\/NAME>/' file
[...]
$. counts how many lines of input have gone by. Sometimes it's also seen as "match the first character after a newline", but that never works as intended, too.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with XML tag value extraction based on matching condition

sample xml file part <DocumentMinorVersion>0</DocumentMinorVersion> <DocumentVersion>1</DocumentVersion> <EffectiveDate>2017-05-30T00:00:00Z</EffectiveDate> <FollowOnFrom> <ContractRequest _LoadId="export_AJ6iAFoh6g0rE9"> <_LocalId>CRW2218451</_LocalId> ... (4 Replies)
Discussion started by: paul1234
4 Replies

2. Shell Programming and Scripting

How can I extract XML block around matching search string?

I want to extract XML block surrounding search string Ex: print XML block for string "myapp1-ear" surrounded by "<application> .. </application>" Input XML: <?xml version="1.0" encoding="UTF-8"?> <deployment-request> <requestor> <first-name>kchinnam</first-name> ... (16 Replies)
Discussion started by: kchinnam
16 Replies

3. Shell Programming and Scripting

Splitting xml file into several xml files using perl

Hi Everyone, I'm new here and I was checking this old post: /shell-programming-and-scripting/180669-splitting-file-into-several-smaller-files-using-perl.html (cannot paste link because of lack of points) I need to do something like this but understand very little of perl. I also check... (4 Replies)
Discussion started by: mcosta
4 Replies

4. Shell Programming and Scripting

Quick Perl "s2p" Commandline Q . . .

Greetings! Looking to clear up a rather simple matter which seems to nonetheless elude me: How does one call Perl's s2p from the commandline? I've tried such as this:perl -e s2p '-i 's/first/last/g' ./somefile.txt'...to convert a similarly-formed GNU sedline over to the Perl equivalent (without... (2 Replies)
Discussion started by: LinQ
2 Replies

5. Shell Programming and Scripting

How to add a line in every file with perl commandline?

Hi, i want to add a line at the beginning of every file in a directory. perl -i.bkp -p -e 'print "#include /verif/pdd1/exu/sxs/6sTest/reset/dfu/top_level.reset\n" if $. == 1' *.reset But this command is updating only the first file in the directory. I think this is because $. is not resetting... (3 Replies)
Discussion started by: twistedpair
3 Replies

6. Shell Programming and Scripting

Hidden Characters in Regular Expression Matching Perl - Perl Newbie

I am completely new to perl programming. My father is helping me learn said programming language. However, I am stuck on one of the assignments he has given me, and I can't find very much help with it via google, either because I have a tiny attention span, or because I can be very very dense. ... (4 Replies)
Discussion started by: kittyluva2
4 Replies

7. Shell Programming and Scripting

Perl XML, find matching condition and grep lines and put the lines somewhere else

Hi, my xml files looks something like this <Instance Name="New York"> <Description></Description> <Instance Name="A"> <Description></Description> <PropertyValue Key="false" Name="Building A" /> </Instance> <Instance Name="B"> ... (4 Replies)
Discussion started by: tententen
4 Replies

8. Shell Programming and Scripting

perl regexp: matching the parent xml tag

Hi folks. I would like to remove the full parent (outer) xml tag from a file given a matching child (inner) tag, in a bash shell. To be more specific, this is what I have so far: $ cat myFile.xml <Sometag></Sometag> <Outer> <Inner>1</Inner> </Outer> <Outer> <stuff>alot</stuff> ... (3 Replies)
Discussion started by: BatManWSL
3 Replies

9. Shell Programming and Scripting

How to determine if a script (perl) was called from a CRON job or commandline

Hi, Is there a way to determine if a Script is called from a CRON job or from a commandline Gerry. (2 Replies)
Discussion started by: jerryMcguire
2 Replies

10. Shell Programming and Scripting

How to parse a XML file using PERL and XML::DOm

I need to know the way. I have got parsing down some nodes. But I was unable to get the child node perfectly. If you have code please send it. It will be very useful for me. (0 Replies)
Discussion started by: girigopal
0 Replies
Login or Register to Ask a Question