Combine 2 files by rows


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combine 2 files by rows
# 1  
Old 06-11-2012
Question Combine 2 files by rows

Hi,
I have 2 files, each files contain about 1 millions lines and I want to joint them to build the new files which contain each 2 lines of file1 and 1 lines of file2.
Newfiles:
line1-file1
line2-file1
line1-file2
line3-file1
line4-file1
line2-file2
.....

Could someone help me?SmilieSmilieSmilieSmilie
# 2  
Old 06-11-2012
Code:
exec 3< "$file1"
exec 4< "$file2"

while :
do
  {
    read a || break
    read b || break
  } <&3
  read c <&4  || break

  printf "%s\n" "$a" "$b" "$c"

done

# 3  
Old 06-11-2012
Nice solution cfajohnson, however will truncate output if number of records in file1 is not exactly twice number in file2.

This slight change should cater for this, but the solution isn't quite so pretty:

Code:
exec 3< "file1"
exec 4< "file2"
while [ -z "$F1" ] || [ -z "$F2" ]
do
  [ -z "$F1" ] && {
    read a && printf "%s\n" "$a"
    read a && printf "%s\n" "$a" || F1=1
  } <&3
  [ -z "$F2" ] && read b <&4 && printf "%s\n" "$b" || F2=1
done


Last edited by Chubler_XL; 06-11-2012 at 09:10 PM..
# 4  
Old 06-12-2012
Quote:
Originally Posted by Chubler_XL
Nice solution cfajohnson, however will truncate output if number of records in file1 is not exactly twice number in file2.
[/code]

If that is the case, then the requirement cannot be met as posted and requires further specification.
# 5  
Old 06-12-2012
How about:
Code:
while read a
      read b
      read c <&3
do    
  printf "%s\n" "$a" "$b" "$c"
done < file1 3<file

Or truncated:
Code:
while read a &&
      read b &&
      read c <&3
do    
  printf "%s\n" "$a" "$b" "$c"
done < file1 3<file


Last edited by Scrutinizer; 06-12-2012 at 04:25 PM..
# 6  
Old 06-12-2012
awk:
Code:
awk '1; !(NR%2) && getline<f' f=file2 file1

# 7  
Old 06-12-2012
For the shell scripts, I would suggest setting IFS to an empty string and using read's -r option, so that lines with leading/trailing whitespace and a trailing backslash are not modified.

Regards,
Alister

---------- Post updated at 04:22 PM ---------- Previous update was at 04:15 PM ----------

A paste solution:
Code:
paste -d '\n' - - file2 < file1

Regards,
Alister
These 2 Users Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Automate splitting of files , scp files as each split completes and combine files on target server

i use the split command to split a one terabyte backup file into 10 chunks of 100 GB each. The files are split one after the other. While the files is being split, I will like to scp the files one after the other as soon as the previous one completes, from server A to Server B. Then on server B ,... (2 Replies)
Discussion started by: malaika
2 Replies

2. Shell Programming and Scripting

Combine multiple rows based on selected column keys

Hello I want to collapse a file with multiple rows into consolidated lines of entries based on selected columns as the 'key'. Example: 1 2 3 Abc def ghi 1 2 3 jkl mno p qrts 6 9 0 mno def Abc 7 8 4 Abc mno mno abc 7 8 9 mno mno abc 7 8 9 mno j k So if columns 1, 2 and 3 are... (6 Replies)
Discussion started by: linuxlearner123
6 Replies

3. Shell Programming and Scripting

Combine rows with awk

I have a file of 100,000 entries that look like: chr1 980547 980667 + chr1:980547-980667 chr1 980728 980848 + chr1:980728-980848 chr1 980793 980913 + chr1:980793-980913 I am trying to reformat them to into 5 columns that are tab delineated: chr1 980547 980667 + ... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. Shell Programming and Scripting

How to find DISTINCT rows and combine in one row?

Hi , i need to display only one of duplicated values and merged them in one record only when tag started with 3100.2.128.8 3100.2.97.1=192.168.0.12 3100.2.128.8=418/66/03e9/0044801 3100.2.128.8=418/66/03ea/0044601 3100.2.128.8=418/66/03e9/0044801 3100.2.128.8=418/66/03ea/0044601... (5 Replies)
Discussion started by: OTNA
5 Replies

5. Shell Programming and Scripting

Combine rows from two files

I have two files, called "File A" and "File B". File A hdisk120: iprod_0000151 dprod_0000135 iprod_0000148 dprod_0000148 iprod_0000157 iprod_0000155 hdisk118: dprod_0000141 dprod_0000134 dprod_0000155 iprod_0000166 dprod_0000129 lprod_0000017 iprod_0000146 (5 Replies)
Discussion started by: Daniel Gate
5 Replies

6. Shell Programming and Scripting

combine multiple files by column into one files already sorted!

I have multiple files; each file contains a certain data in a column view simply i want to combine all those files into one file in columns example file1: a b c d file 2: 1 2 3 4 file 3: G (4 Replies)
Discussion started by: ahmedamro
4 Replies

7. Shell Programming and Scripting

Deleting specific rows in large files having rows greater than 100000

Hi Guys, I need help in modifying a large text file containing more than 1-2 lakh rows of data using unix commands. I am quite new to the unix language the text file contains data in a pipe delimited format sdfsdfs sdfsdfsd START_ROW sdfsd|sdfsdfsd|sdfsdfasdf|sdfsadf|sdfasdf... (9 Replies)
Discussion started by: manish2009
9 Replies

8. Shell Programming and Scripting

3 files combine into one help please

Ok here is what I have file a {{BEGIN}} {{FAX File b 5555464584 5555465292 5555465828 5555485930 5555474939 File C }} ON ORDERS LESS THAN 70 LBS AND THE PACKAGE IS A COMBINED LENGTH AND GIRTH EQUAL TO OR LESS THAN 108" PLEASE UTILIZE UPS. ... (4 Replies)
Discussion started by: sctxms
4 Replies

9. HP-UX

How to combine 2 different files

Hi : I have a file containing the print queues with their IP address. I wanted to combine the 'lpstat' output with their respective IP address. For example : zebhtrmb-6078 lgonzale priority 0 Mar 17 11:50 on zebhtrmb with zebhtrmb-6078 lgonzale priority 0 ... (1 Reply)
Discussion started by: rdasari
1 Replies

10. Shell Programming and Scripting

combine two files

I have two files: file1 and file2 the content of files1 is: 13 22 333 42 56 55 ... the content of file2 is: aa dd cc ee ff gg ... (1 Reply)
Discussion started by: fredao1
1 Replies
Login or Register to Ask a Question