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!

combining 2 lines into one 5

Status
Not open for further replies.

rao12

IS-IT--Management
Jan 10, 2001
9
0
0
US
Hi ,

I have an input file with entries in each lines
aaaaa bbbbb
cccccc dddddddd
eeee fffff gggg
hhhh iiii jjjj
..... .... ....
..... .... ....

i would like to have an output file with contents

aaaaa bbbbb cccccc dddddddd
eeee fffff gggg hhhh iiii jjjj

i.e., 1st and 2nd line to be in one line
3 and 4 lines in 2 line an so on...

Could anyone help how to do that.

Thanks
Rao
 
This is off the top of my head and untested, so be sure to test it! This example assumes that FILE is a filehandle that you opened for reading your input file, and OUT_FILE is a filehandle that you opened for writing to:

$ct = 0;
while(<FILE>) {
$ct++;
chomp;
$combined_str .= $_;
if ($ct == 2) { ### if you're on an even row
print OUT_FILE $combined_str . &quot;\n&quot;;
$combined_str = &quot;&quot;;
$ct = 0;
}
}

### if an odd numbered row was the last row, there
### will still be some unwritten data in $combined_str
if ($combined_str ne &quot;&quot;) {
print OUT_FILE $combined_str . &quot;\n&quot;;
}

------------------------------------
I'm sure there's an easier way to do this.

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
another approach......, tmtowtdi ;-)
There must be a final \n on the end of the last line or this will leave the last two lines separate.

GIVEN this input file, 'lines'.
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10

# end 'lines'


THIS CODE:
Code:
#!/usr/local/bin/perl
open(IPF,&quot;<lines&quot;);
while (<IPF>) { $buffer .= $_; }
close IPF;

$buffer =~ s/(.*?)\n(.*?)\n/$1 $2\n/gs;
print &quot;$buffer\n&quot;;

GIVES THIS OUTPUT:
line 1 line 2
line 3 line 4
line 5 line 6
line 7 line 8
line 9 line 10



# kinda' neat, maybe....
Oh yeah, if you have DOS line termination, add \r to the regex...


keep the rudder amid ship and beware the odd typo
 
Or here's one:
[tt]
my $txt;
open(TXT, &quot;<text.txt&quot;) || die &quot;Bad File: $!&quot;;
while ( <TXT> ) {
chomp $_;
$txt .= ($_) . <TXT>;
}
print $txt;
close TXT;
[/tt]
Since it accesses the file twice each loop, it cycles through two lines, and only gets to chomp the odd ones.
&quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
TMTOWTDI -- it seems

how about...

while(<>){
chomp;
$_ .= <>;
print;
}
Mike
michael.j.lacey@ntlworld.com
 
Hi all,
You can simplify hmerrill's approach a little:

$ct = 0;
while(<FILE>) {
$ct++;
chomp;
$combined_str .= $_;
if ($ct % 2) { ### if you're on an even row
print OUT_FILE $combined_str . &quot;\n&quot;;
$combined_str = &quot;&quot;;
}
}

and $ct will give you the total # of lines crunched (of course, div by 2 for total combined lines).

Ben
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top