LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-09-2012, 02:38 PM   #1
dazdaz
Member
 
Registered: Aug 2003
Location: Europe
Distribution: RHEL, CentOS, Ubuntu
Posts: 333

Rep: Reputation: 17
newbie perl question


Hi,

How can I write a perl regex to extract the data between : <string1>data</string1>

This may be spread over multiple lines too, depending upon the size of the data entry.

Quote:
<string1>data</string1>
Quote:
<string1>dataandyetmoredata
</string1>
Out of curiosity, how would you approach this in sed too.

Last edited by dazdaz; 10-09-2012 at 02:50 PM.
 
Old 10-10-2012, 01:00 AM   #2
ip_address
Member
 
Registered: Apr 2012
Distribution: RedHat
Posts: 42

Rep: Reputation: 2
you can try this ..

# Input file

Code:
$ cat patterns1.txt 

<string1>data</string1>
# sed commands

Code:
$ sed -n '/<string1>/,/<\/string1>/p' patterns1.txt | sed -e 's/<string1>//g' -e 's/<\/string1>//g' | sed '/^$/d'
#Output

Code:
data
# Input file

Code:
cat patterns2.txt 

<string1>fdjfsfdjkjfdsal
fajsklsdjflk
</string1>
# sed commands

Code:
sed -n '/<string1>/,/<\/string1>/p' patterns2.txt | sed -e 's/<string1>//g' -e 's/<\/string1>//g' | sed '/^$/d'
#Output

Code:
fdjfsfdjkjfdsal
fajsklsdjflk
 
Old 10-10-2012, 03:48 AM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by dazdaz View Post
How can I write a perl regex to extract the data between : <string1>data</string1>
That looks like XML, consider using an XML parser.
Quote:
Originally Posted by Jamie Zawinski
Some people, when confronted with a problem, think
“I know, I'll use regular expressions.” Now they have two problems.
 
1 members found this post helpful.
Old 10-10-2012, 01:17 PM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Perl-related questions are probably better handled at http://www.perlmonks.org ...
 
Old 10-10-2012, 01:45 PM   #5
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
In order to parse tags over multiple lines you can undef the $/ variable.
Code:
undef $/ ;
Parsing can be done in the following way:
Code:
if ( $string =~ m/<string1>(.*?)<\/string1>/i ) {
    say $1 ;
}
where $1 holds the data between the tags.

Markus
 
1 members found this post helpful.
Old 10-11-2012, 02:43 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
If you are doing XML in perl I recommend http://xmltwig.org/
 
Old 10-11-2012, 05:44 AM   #7
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
In my opinion there are several cases when one doesn't need an XML-parser. If you only have one type of xml-files and only want to extract a few data where the tag-names are known, it is relatively easy in perl to do this with simple regex like I posted above.

I'd recommend to take a look at the sources of geolog http://geolog.sourceforge.net/geolog.html which helped me not to use a parser for a much too simple task.

Markus
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl newbie question about system command Guttorm Programming 8 06-30-2011 08:43 PM
newbie question: calling perl script from cron hattori.hanzo Programming 4 09-02-2010 02:21 PM
How to program the CGI module in Perl (newbie question) resetreset Programming 3 12-01-2008 02:08 AM
A newbie perl question KClaisse Programming 5 05-13-2008 05:41 PM
Perl array (newbie) question Seventh Programming 5 09-02-2004 12:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration