Join every two lines of a file

N

Ninja Li

Hi,

I am trying to join every two lines of the following using perl:
<td align="center" width="50"><nobr>AWR
 </nobr></td>
<td align="center" width="50"><nobr>ANH
 </nobr></td>
<td align="center" width="50"><nobr>ACAP
 </nobr></td>

The output should be as follows:
<td align="center" width="50"><nobr>AWR</nobr></td>
<td align="center" width="50"><nobr>ANH</nobr></td>
<td align="center" width="50"><nobr>ACAP</nobr></td>

Is there an easy way to join every two lines while at the same time
remove the " " characters in the line?

Thanks.

Nick
 
J

Josef Moellers

Ninja said:
Hi,

I am trying to join every two lines of the following using perl:
<td align="center" width="50"><nobr>AWR
 </nobr></td>
<td align="center" width="50"><nobr>ANH
 </nobr></td>
<td align="center" width="50"><nobr>ACAP
 </nobr></td>

The output should be as follows:
<td align="center" width="50"><nobr>AWR</nobr></td>
<td align="center" width="50"><nobr>ANH</nobr></td>
<td align="center" width="50"><nobr>ACAP</nobr></td>

Is there an easy way to join every two lines while at the same time
remove the " " characters in the line?

Yes. What have you tried so far? Where did it not meet your expectations.
You're supposed to try first and *then* come here for help.

But then ... it's pretty simple:

while (my $line = <$src>) {
chomp $line;
$line .= <$src>;
$line =~ s/ //;
print $dst $line;
}
 
J

Jürgen Exner

Ninja Li said:
Hi,

I am trying to join every two lines of the following using perl:
<td align="center" width="50"><nobr>AWR
 </nobr></td>
<td align="center" width="50"><nobr>ANH
 </nobr></td>
<td align="center" width="50"><nobr>ACAP
 </nobr></td>

The output should be as follows:
<td align="center" width="50"><nobr>AWR</nobr></td>
<td align="center" width="50"><nobr>ANH</nobr></td>
<td align="center" width="50"><nobr>ACAP</nobr></td>

Is there an easy way to join every two lines while at the same time
remove the " " characters in the line?

Pretty trivial. Which part do you have problems with? Designing the
algorithm? Translating the algorithm into Perl? Debugging the Perl
program? ...?

jue
 
N

Ninja Li

I can join the lines by doing the following in awk:

awk '{ if ( ( NR % 2 ) == 0 ) { printf("%s\n",$0) } else { printf("%s
",$0) } }' filename

I am wondering if there is any one-liner similar to this in perl to
accomplish it.

Thanks.

Nick
 
J

John W. Krahn

Ninja said:
I can join the lines by doing the following in awk:

awk '{ if ( ( NR % 2 ) == 0 ) { printf("%s\n",$0) } else { printf("%s
",$0) } }' filename

I am wondering if there is any one-liner similar to this in perl to
accomplish it.

perl -lpe'$\ = $. % 2 ? "" : "\n"' filename




John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top