How to mix the contents of 2 files into a new file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to mix the contents of 2 files into a new file?
# 1  
Old 01-25-2010
How to mix the contents of 2 files into a new file?

Hello Everybody!

My question is how can I mix for example file_a with file_b in the following method:

After 2 lines of file_a put 2 lines from file_b to file_c.

For example:


file_a:
1
2
3
4
5
6

file_b:
11
22
33
44
55
66

Then I would like to get a file_c like this:

1
2
11
22
3
4
33
44
5
6
55
66


Thank you for your help!Smilie
# 2  
Old 01-25-2010
Try:
Code:
awk '{getline x<f;print x;getline x<f;print x;print;getline;print}' f=file1 file2

# 3  
Old 01-25-2010
Dear Scrutinizer!

Thanks for your help.
It works.

In this version:

Code:
awk 'function prl() {getline x<f;print x}{prl();prl();prl();print;getline;print;getline;print}' f=file1 file2

Coluld you tell me, how can i make for example, to get 3 lines form file_a and get 5 lines form file_b?

I should use:
prl(); 3 times
and print;getline; 5 times ?

Or there is another way?

Thanks!

Last edited by Levi85; 01-25-2010 at 11:26 AM.. Reason: update
# 4  
Old 01-25-2010
Something like this, probably:
Code:
awk '{for(i=1;i<=3;i++){if (getline x<f){print x}}
         print;for(i=1;i<5;i++){if (getline)print}}' f=file1 file2

# 5  
Old 01-26-2010
Dear Scrutinizer!

Thank you for your answer!
It works!

The only problem that it works correctly for the files with same length.
So it ends if the shorter file ends.

Do you have any idea, how to solve it?
I will try to solve the problem, and post the solution if i find.

All the best!
# 6  
Old 01-26-2010
Code:
nawk '  
NR==FNR{a[++i]=$1  ; next}
{b[++j]=$1 ; next}
END{ if (j>=i) {max=j}else{max=i}
for (z=1;z<=max;z+=2){
     printf "%s\n%s\n%s\n%s\n",a[z],a[z+1],b[z],b[z+1]
  }
}
' File1.txt File2.txt

SmilieSmilieSmilie
# 7  
Old 01-26-2010
Quote:
Originally Posted by Levi85
The only problem that it works correctly for the files with same length. So it ends if the shorter file ends.

Do you have any idea, how to solve it?
Hi, what would you like to happen if the shorter file ends prematurely?

Last edited by Scrutinizer; 01-26-2010 at 09:09 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Output file name and file contents of multiple files to a single file

I am trying to consolidate multiple information files (<hostname>.Linux.nfslist) into one file so that I can import it into Excel. I can get the file contents with cat *Linux.nfslist >> nfslist.txt. I need each line prefaced with the hostname. I am unsure how to do this. --- Post updated at... (5 Replies)
Discussion started by: Kentlee65
5 Replies

2. Shell Programming and Scripting

Join two files combining multiple columns and produce mix and match output

I would like to join two files when two columns in each file matches with each other and then produce an output when taking multiple columns. Like I have file A 1234,ABCD,23,JOHN,NJ,USA 2345,ABCD,24,SAM,NY,USA 5678,GHIJ,24,TOM,NY,USA 5678,WXYZ,27,MAT,NJ,USA and file B ... (2 Replies)
Discussion started by: mady135
2 Replies

3. Shell Programming and Scripting

How to copy all the contents of a list of files present in a folder to a particular file?

Hi All, I want to copy all the contents of a list of files in a folder to a particular file. i am using following command: cat dir/* >> newFile.txtIt's not working. Could you please help? Thanks, Pranav (3 Replies)
Discussion started by: Pranav Bhasker
3 Replies

4. Shell Programming and Scripting

Need Script to copy the contents of two files into one file

Hi i need Script to copy the contents of two files into one file i have 2 fil X1.txt / X2.txt i need script to copy the contents of X1 and X2 In AllXfile X1.txt File X1 X2.txt File X2 AllXfile.txt File X1 File X2 (2 Replies)
Discussion started by: azzeddine2005
2 Replies

5. Shell Programming and Scripting

Concatenate many files which contents the same date as part of name file

Gents, I have lot of files in a folder where each file name includes the date of generation, then I would like to merge all the files for each date in a complete file. list of files in forder. dsd01_121104.txt dsd01_121105.txt dsd01_121106.txt dsd03_121104.txt dsd03_121105.txt... (7 Replies)
Discussion started by: jiam912
7 Replies

6. Shell Programming and Scripting

Redirect all logs files contents into a single log file

Hi , I have a Data cleansing process which creates different log file for each step , when the process runs it creates following log files in below order: p1_tranfrmr_log.txt p1_tranfrmr_stats.txt p2_globrtr_log.txt p2_globrtr_stats.txt p3_cusparse_log.txt p3_cusparse_stats.txt ' '... (8 Replies)
Discussion started by: sonu_pal
8 Replies

7. UNIX for Dummies Questions & Answers

Replacing a particular string in all files in folder and file contents

I need to replace all filesnames in a folder as well as its content from AK6 to AK11. Eg Folder has files AK6-Create.xml, AK6-system.py etc.. the files names as well as contents should be changes to AK9-Create.xml, AK9-system.py etc All files are xml and python scripts. ---------- Post... (0 Replies)
Discussion started by: Candid247
0 Replies

8. Shell Programming and Scripting

Round Robin Distribution of Contents of file to 3 files

Hi I need to create a script that distributes in round robin fashion the contents of a file to 3 files. The number of lines in a content of file can vary from 1-n(Each line is just a one letter word).The entire lines needs to get distributed into 3 files ( The order doesnt matter) , at... (5 Replies)
Discussion started by: police
5 Replies

9. Shell Programming and Scripting

Compare two files and remove all the contents of one file from another

Hi, I have two files, in which the second file has exactly the same contents of the first file with some additional records. Now, if I want to remove those matching lines from file2 and print only the extra contents which the first file does not have, I could use the below unsophisticated... (3 Replies)
Discussion started by: royalibrahim
3 Replies

10. Shell Programming and Scripting

comparing files to contents of a file

Hi I have a problem trying to run a while statement. I have files under one directory that i need to compare to a value in filex and update that file with the result files in the directory are DFC1. DFC5. DFC345. DFC344. DFC9. The program i am trying to run will take the number... (3 Replies)
Discussion started by: SummitElse
3 Replies
Login or Register to Ask a Question