many rows to one row :-)

I need to have all text beetween:
<url  and </url> to be in one long row.. Any simple perl solution?
niferAsked:
Who is Participating?
 
alamoConnect With a Mentor Commented:
Here's how'd I do it. The string is in $str.

local($s);
$str =~ s!(<url>.*?</url>)!{($s=$1)=~s/\n+/ /sg; $s;}!segi;

To explain it a little: this is a substitution inside a substitution. The outer subsitution is

$str =~ s!(<url>.*?</url>)!{expression}!segi;

where $1 is set to each <url> tag in turn and then replaced by the string produced by the expression evaluated in the {}.

The inner substitution copies $s to $1 (since $1 is read-only) and then does
s/\n+/ /sg;
which replaces one or more \n's with a space, thus combining the text into a single line.

Hope this does what you want!
0
 
niferAuthor Commented:
Edited text of question
0
 
alamoCommented:
When you say "in one long row" do you mean there are now multiple lines inside the <url> tags and you want a single line?

And what is "<url>" used for? I don't recognize that tag.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
niferAuthor Commented:
Yes there could be multiple lines inside the <url> tag, but not allways.

<url> are just some thing I use to make some splits later..




0
 
alamoCommented:
Umm, should be "copies $1 to $s" not "copies $s to $1" in my description.
0
 
niferAuthor Commented:
And if i want it to be a simple script let say:

scriptname textfile > textfile2
0
 
alamoCommented:
Piece of cake, perl is cool:-)

$str = join('',<>);
local($s);
$str =~ s!(<url>.*?</url>)!{($s=$1)=~s/\n+/ /sg; $s;}!segi;
print $str;
0
 
alamoCommented:
Of course if you are on a unix-like system, you'll need to include a first line of something like

#!/bin/perl

so it knows to run perl.
0
 
niferAuthor Commented:
worked well ;-)
0
All Courses

From novice to tech pro — start learning today.