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

Combine two lines of text sequentally

Status
Not open for further replies.

kb0rhe

Technical User
Dec 25, 2006
16
US
I have a file that has multiple lines exactly 4 lines per product.

I would like to combine every two lines into one line.

E BRG 100023 0000 00000545
Z BRG 100023 GASOHOL TEST KIT

The two lines below - The actual file has about 24,000 lines.
I have been working in this for a while now. And I boils down to me just being stupid.

I was thinking of a counter and reset every two lines.
Any help would be greatly appreciated!
 
post some sample lines from the file and explain in detail what you want to combine and exlain how you want to combine them.




- Kevin, perl coder unexceptional!
 
Below is a sample of the file I am importing. (FILE 1)
SAMPLE FILE 2 is what I am asking for help on.
SAMPLE FILE 2 is a representative of what I am asking for help on.
FILE 1 has six lines of data I want three.
Combine Line 1 with Line 2, Line 3 with Line 4, Line 5 with line 6.

This file is huge - 24,000 lines of data.
I want to combine into one line and I will be extracting some data from each line to simplify the single line even more. (See Sample File 3)

I was thinking of a 'for' loop, and counting by two's, but I am at a loss here. I can't figure this one out.




************SAMPLE FILE 1 **************************
E BRG 100023 0000 00000545
Z BRG 100023 GASOHOL TEST KIT
E BRG 100024 0000 00014145
Z BRG 100024 THREAD KIT-MASTER
D BRG 100025 0000 00004780
Z BRG 100025 HOURMETER
***********END FILE 1 ******************************

************SAMPLE FILE 2 **************************
E BRG 100023 0000 00000545 Z BRG 100023 GASOHOL TEST KIT
************END FILE 2****The above is one line now**

Utlimately I want the this as the final output

BRG 100023 GASOHOL TEST KIT $5.45

 
a couple of ways to combine the two lines:

Code:
open(IN,'<file1.txt') or die "$!";
open(OUT,'>file2.txt') or die "$!";
while (<IN>) {
   chomp;
   print OUT;
   print OUT "\n" unless ($. % 2);
}
close(IN);
close(OUT);
print "finished";

or as a one-liner:

Code:
perl -ne "chomp; print; print \"\n\" unless ($. % 2)" file1.txt >file2.txt

24,000 lines is not a huge file. The above one-liner should take only few seconds to complete. The other code might take a little more time to complete but not much.

Then work on your final output and post your code after you get it working or if you get stuck.

- Kevin, perl coder unexceptional!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top