Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can I use regex while define $/ ?

Status
Not open for further replies.

frankli

Technical User
Apr 6, 2005
44
CA
Hello List,

I m trying to print the multiple lines to one base on certain pattern e.g.

Source:
Sun Feb 8 line1
line2
line3
Mon Feb 9 line4
line5
Tue Feb 10 line6
line 7
line 8

Target
Sun Feb 8 line1 line2 line3
Mon Feb 9 line4 line5
Tue Feb 10 line6 line7 line8

I can accomplish this using gawk b/c gawk allows regex to define RS, but can I use the same concept in PERL?

a2p translate it into below but it does not work,

$/ = '(Mon|Tue|Wed|Thu|Fri|Sat|Sun) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Dec|No
v) ( 1| 2| 3| 4| 5| 6| 7| 8| 9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|2
6|27|28|29|30|31)';

Please comment. Thanks!
 
As Franco pointed out, the input separator cannot contain a regex, but the results you're looking for can be accomplished pragmatically.

Code:
{
	my @parts;
	while (<DATA>) {
		chomp;
		if (/^\w{3}\s+\w{3}\s+\d+/) {
			if (@parts) {
				print join(' ', @parts), "\n";
				@parts = ();
			} 
		}
		push @parts, $_;
	}
	# Print last record;
	print join(' ', @parts), "\n";
}

__DATA__
Sun Feb  8 line1
line2
line3
Mon Feb  9 line4
line5
Tue Feb 10 line6
line 7
line 8
Code:
Sun Feb  8 line1 line2 line3
Mon Feb  9 line4 line5
Tue Feb 10 line6 line 7 line 8
With the data you supplied, the above regex will work. If you need all the days / months spelled out, the regex will need to be modified.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top