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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting multiline text to a single line seperated by commas

Status
Not open for further replies.

sappleg

Programmer
Apr 15, 2008
31
CA
Hi Guys,

I am trying to convert a multiple lines into a single line within the text file I have. Currently, I have this in the txt file:

Stores Updated: 626
Stores Created: 0
STATUS:09/01/2009:pASS

I want the output in the same file as (in a single line)

Stores Updated: 626, Stores Created: 0,
STATUS:09/01/2009:pASS

I am novice so don't have any idea how to achieve this? Any help is greatly appreciated.
Thanks very much in advance.
 
Not really an Optimal Sol and makes some assumptions:

Code:
#!/path/to/perl

while(<DATA>) {
        if ($_ =~ /Stores/i) {
                my ($val1,$val2) = split(/\n/,$_);
                print "$val1,$val2";
        } else {
                print "\n$_";
          }
}

__DATA__
Stores Updated: 626
Stores Created:  0
STATUS:09/01/2009:PASS
Stores Updated: 627
Stores Created:  0
STATUS:09/01/2009:PASS
Stores Updated: 628
Stores Created:  0
STATUS:09/01/2009:PASS

RESULTS IN:

Stores Updated: 626,Stores Created: 0,
STATUS:09/01/2009:pASS
Stores Updated: 627,Stores Created: 0,
STATUS:09/01/2009:pASS
Stores Updated: 628,Stores Created: 0,
STATUS:09/01/2009:pASS
 
Thanks max1x,

I will try this and make changes wherever necessary.

 
Based only on the data you've supplied, you could do something like this:
Code:
while (<DATA>) {
	chomp and $_ .= ", " if m/Stores Updated/i;
	print;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top