merging two .txt files by alternating x lines from file 1 and y lines from file2


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting merging two .txt files by alternating x lines from file 1 and y lines from file2
# 1  
Old 05-26-2011
Lightbulb merging two .txt files by alternating x lines from file 1 and y lines from file2

Hi everyone,

I have two files (A and B) and want to combine them to one by always taking 10 rows from file A and subsequently 6 lines from file B. This process shall be repeated 40 times (file A = 400 lines; file B = 240 lines).

Does anybody have an idea how to do that using perl, awk or sed?

Thank you so much in advance!
Iris

------------------------------------------------------------------------------
File A
------------------------------------------------------------------------------
Code:
0000 0    BIROB    18    brb    PSI    2    99
0001 17    ROBER    9    rbr    PSI    2    99
0002 5   TASOT    16    tst    PSI    2    99
0003 78    KASEK    6    ksk    PSI    2    99
...

-------------------------------------------------------------------------------
File B
-------------------------------------------------------------------------------
Code:
0000 N    NNNNN    N    N    CCCCC    NNN    2
0001 P    PPPPP    P    P    CCCCC    PPP    1
0002 P    PPPPP    P    P    CCCCC    PPP    1
0003 P    PPPPP    P    P    CCCCC    PPP    1
0004 N    NNNNN    N    N    CCCCC    NNN    2


Last edited by Franklin52; 05-26-2011 at 12:47 PM.. Reason: Please use code tags
# 2  
Old 05-26-2011
Could this help you? (Code not tested)
Code:
#!/bin/sh
i=0
j=6
k=0
while read line1
do
    echo $line1
    i=$i+1
    if [ $i -eq 10 ]
    then
        awk -v start=$k -v end=$j  'NR > start && NR <= end' file2
        k=$j
        j=$j+6
        i=0
    fi
done < file1


Last edited by pravin27; 05-26-2011 at 07:09 AM..
# 3  
Old 05-26-2011
Thanks!
Unfortunately, it doesn't read or print the lines from file2 so that it just takes all lines from file1 and prints those.

Any idea why it doesn't do the if loop?
# 4  
Old 05-26-2011
Try this,
Code:
#!/bin/sh
i=0;j=6;k=0
while read line1
do
    echo $line1
    i=$(($i+1))
    if [ $i -eq 10 ]
    then
        awk -v start=$k -v end=$j  'NR > start && NR <= end' file2
        k=$j;j=$(($j+6));i=0
    fi
done < file1

This User Gave Thanks to pravin27 For This Post:
# 5  
Old 05-26-2011
Thank you so much! It now works perfectly!
# 6  
Old 05-26-2011
A Perl:
Code:
#!/usr/bin/perl
use strict;

my $fileA=shift;
my $fileB=shift;
my $fileR=shift;

open (FA,"<",$fileA ) || die "Can't open $fileA\n";
my @txtA=<FA>;
close(FA);

open (FB,"<",$fileB ) || die "Can't open $fileB\n";
my @txtB=<FB>;
close(FB);

open (FR,">",$fileR ) || die "Can't open $fileR\n";

my ($contA,$a,$contB,$b)=0;

for my $i ( 1 .. 40 ) {
   for $a ( 1 .. 10 ) {
      $a+=$contA;
      print FR $txtA[$a];
      }
   $contA+=10;
   for $b ( 1 .. 6 ) {
      $b+=$contB;
      print FR $txtB[$b];
      }
   $contB+=6;
   }    

close(FR);
    
exit;

Usage:
Code:
script.pl filea fileb result

This User Gave Thanks to Klashxx For This Post:
# 7  
Old 05-27-2011
By shell script:
Code:
i=1;j=1;k=10;l=6
line=`cat file1 |wc -l`

while (true)
do
    m=`expr $i + $k - 1`
    n=`expr $j + $l - 1`
    sed -n "${i},${m}p" file1
    sed -n "${j},${n}p" file2
    i=`expr $i + $k`
    j=`expr $j + $l`
    if [ $i -gt $line ]; then
      exit
    fi
done

One-line awk (speed faster, because file1 and file2 are only read by one time.)

Code:
awk 'NR%10{print;next} {print;for (i=1;i<=6;i++) {getline < "file2";print} }' file1


Last edited by rdcwayx; 05-27-2011 at 02:41 AM..
This User Gave Thanks to rdcwayx For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merging two files each contain 16 lakh lines on HP-UX 11.11 system

Hello All , I am trying to merge two files each contain 16 lakh lines ..My requirement is i have merge after every 14 lines of each file . Like from file1 14 lines then after after 14 lines form file2 ..so i wrote below script . It is working for small files ,but large files script not... (10 Replies)
Discussion started by: Phani369
10 Replies

2. UNIX for Beginners Questions & Answers

Merging specifc lines of three files into one?

Recall that nc-county-pop.dat has 100 lines, and each line corresponds to a county. The files girls.dat and boys.dat each has 50 lines. Assume that the girls come from the first 50 counties and each county has only one girl. That is the first girl comes from the first county, the second girl comes... (1 Reply)
Discussion started by: novicep11
1 Replies

3. Shell Programming and Scripting

Merging multiple lines to columns with awk, while inserting commas for missing lines

Hello all, I have a large csv file where there are four types of rows I need to merge into one row per person, where there is a column for each possible code / type of row, even if that code/row isn't there for that person. In the csv, a person may be listed from one to four times... (9 Replies)
Discussion started by: RalphNY
9 Replies

4. Shell Programming and Scripting

Merging multiple files using lines from one file

I have been working of this script for a very long time and I have searched the internet for direction but I am stuck here. I have about 3000 files with two columns each. The length of each file is 50000. Each of these files is named this way b.4, b.5, b.6, b.7, b.8, b.9, b.10, b.11, b.12... (10 Replies)
Discussion started by: iconig
10 Replies

5. UNIX for Dummies Questions & Answers

find lines in file1.txt not found in file2.txt memory problem

I have a diff command that does what I want but when comparing large text/log files, it uses up all the memory I have (sometimes over 8gig of memory) diff file1.txt file2.txt | grep '^<'| awk '{$1="";print $0}' | sed 's/^ *//' Is there a better more efficient way to find the lines in one file... (5 Replies)
Discussion started by: raptor25
5 Replies

6. Shell Programming and Scripting

sed to cp lines x->y from 1.txt into lines a->b in file2.txt

I have one base file, and multiple target files-- each have uniform line structure so no need to use grep to find things-- can just define sections by line number. My question is quite simple-- can I use sed to copy a defined block of lines (say lines 5-10) from filename1.txt to overwrite an... (3 Replies)
Discussion started by: czar21
3 Replies

7. Shell Programming and Scripting

Matching lines across multiple csv files and merging a particular field

I have about 20 CSV's that all look like this: "","","","","","","","","","","","","","","",""What I've been told I need to produce is the exact same thing, but with each file now containing the start_code from every other file where the email matches. It doesn't matter if any of the other... (1 Reply)
Discussion started by: Demosthenes
1 Replies

8. Shell Programming and Scripting

Merging lines in a file

Hi, I want to merge the lines starting with a comma symbol with the previous line of the file. Input : cat file.txt name1,name2 ,name3,name4 emp1,emp2,emp3 ,emp4 ,emp5 user1,user2 ,user3 Output name1,name2,name3,name4 emp1,emp2,emp3,emp4,emp5 (9 Replies)
Discussion started by: mohan_tuty
9 Replies

9. Shell Programming and Scripting

Urgent Need Help! Merging lines in .txt file

I need to write a script that reads through an input .txt file and replaces the end value with the end value of the next line for lines that have distance <=4000. The first label line is not actually in the input. In the below example, 3217 is the distance from the end of the first line to the... (12 Replies)
Discussion started by: awknerd
12 Replies

10. Shell Programming and Scripting

Merging files with AWK filtering and counting lines

Hi there, I have a couple of files I need to merge. I can do a simple merge by concatenating them into one larger file. But then I need to filter the file to get a desired result. The output looks like this: TRNH 0000000010941 ORDH OADR OADR ORDL ENDT 1116399 000000003... (2 Replies)
Discussion started by: Meert
2 Replies
Login or Register to Ask a Question