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

Inserting lines in one single file

Status
Not open for further replies.

jupiler

Technical User
Oct 1, 2002
14
BE
Hi,

Four files (one for Dutch, one for French, one for Italian and one for English) need to be merged into one single file.

Dutch file:

<Seg L=NL-BE>Artikel 1
<Seg L=NL-BE>Artikel 2

English file:

<Seg L=EN-GB>ART. 1
<Seg L=EN-GB>ART. 2

Italian file:

<Seg L=IT-IT>Articolo 1
<Seg L=IT-IT>Articolo 2

French file:

<Seg L=FR-BE>Article premier
<Seg L=FR-BE>Article 2

These four files should be merged so that we get the following output file:

**
<Seg L=NL-BE>Artikel 1
<Seg L=EN-GB>ART. 1
<Seg L=IT-IT>Articolo 1
<Seg L=FR-BE>Article premier
**
<Seg L=NL-BE>Artikel 2
<Seg L=EN-GB>ART. 2
<Seg L=IT-IT>Articolo 2
<Seg L=FR-BE>Article 2

Notes:
- &quot;**&quot; should appear on lines 1, 6, 11, ...
- &quot;<Seg L=NL-BE>[...]&quot; should appear on lines 2, 7, 12,...
- &quot;<Seg L=EN-GB>[...]&quot; should appear on lines 3, 8, 13,...
- etc.

Can someone help me with this? Thanks,

Jupiler

 
the 'premier' on the first french line ... can you change this??

write it in awk/perl using arrays,
read the NLfile in a NLarray
...
the ITfile in ITarray
then print the corresponding entries
NLarray[0], ... ITarray[0]\n
...
NLarray[n], ... ITarray[n]\n

something like
NLidx = 0;
while(getline x < filename >0) NLarr[NLidx++] = x;
ITidx= 0;
while(getline x < filename >0) ITarr[ITidx++] = x;

if(NLidx != ITidx) ERROR

for(NLidx = 0; ITidx >NLidx; ++Itidx){
print &quot;**&quot;
print NLarray[NLidx]
....
print ITarray[NLidx]
}
... vox clamantis in deserto.
 
Hi,

The &quot;premier&quot; does not have to be changed. The idea is, irrespective of the content of the lines, to get the output as it is shown in the example above. Would it also be possible to write a (gawk) script in which you state that awk should:

1. first print &quot;**&quot;
2. print the first line of the Dutch file
3. print the first line of the English file
4. print the first line of the Italian file
5. print the first line of the French file
6. print &quot;**&quot;
7. print the second line of the Dutch file
8. print the second line of the English file
9. print the second line of the Italian file
10. print the second line of the French file
11. print &quot;**&quot;
12. etc....

Cheers,

Jupiler
 
Try this.

BEGIN {
fn2 = &quot;file2&quot;
fn3 = &quot;file3&quot;
fn4 = &quot;file4&quot;
print &quot;**&quot;
}
{
print
if ((getline < fn2) > 0) print
if ((getline < fn3) > 0) print
if ((getline < fn4) > 0) print
print &quot;**&quot;
}

Run it by entering

awk -f this-script file1 > merged-file CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top