Merge two files line by line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Merge two files line by line
# 1  
Old 04-09-2013
Merge two files line by line

Hello,

i want to merge two files via terminal. After three lines of file one i will append a line of file two:

Example:
Code:
File 1:
@chr1_15953153_15953093_1_0_0_0_1:0:0_0:0:0_0/1
TGTCGTAAAATCACAACGTCCCCATCTTTCAAGCCATAAGCAGCCAATGATCTGTGGTTGTCTGTGAGAGGTCTTTCCGCATAGACGATCTACAACAAGA
+
@chr1_45250695_45250663_1_0_0_0_2:0:0_1:0:0_1/1
GCTGGCTGGCTCAAGGTCGGCACTGCAGATGGGCATGCCAGCCTGCTCTGTGGAGATGTGGGCAGAGGGGCTGCCCCGACTTTGGGGAAAAACAGTGGGG
+

File 2: 
HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHEHHHHHHBGHHHHCGHHHGHF?HHGGFHEHGBGGGHFGHEE?FFFFGGBEBEE/=CACADEEE5E#
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFEF>8A?C@FDFE>FFFFFDFFFFEFEBF<B@;@###############################

Output:
Code:
@chr1_15953153_15953093_1_0_0_0_1:0:0_0:0:0_0/1
TGTCGTAAAATCACAACGTCCCCATCTTTCAAGCCATAAGCAGCCAATGATCTGTGGTTGTCTGTGAGAGGTCTTTCCGCATAGACGATCTACAACAAGA
+ 
HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHEHHHHHHBGHHHHCGHHHGHF?HHGGFHEHGBGGGHFGHEE?FFFFGGBEBEE/=CACADEEE5E#
@chr1_45250695_45250663_1_0_0_0_2:0:0_1:0:0_1/1
GCTGGCTGGCTCAAGGTCGGCACTGCAGATGGGCATGCCAGCCTGCTCTGTGGAGATGTGGGCAGAGGGGCTGCCCCGACTTTGGGGAAAAACAGTGGGG
+
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFEF>8A?C@FDFE>FFFFFDFFFFEFEBF<B@;@###############################

Plz help me out

Regards,

S.S

Last edited by vbe; 04-09-2013 at 08:16 AM.. Reason: code tags please next time for your data and code, thanks
# 2  
Old 04-09-2013
Code:
#!/bin/bash
{
i=0
while read line
do
  printf "%s\n" "$line"
  i=$(( $i + 1 ))
  if [ $i -eq 3 ]; then
    i=0
    read line <&3
    printf "%s\n" "$line"
  fi
done < file1
} 3< file2

The while block reads from file1, the outer { } block reads from file2 using the helper descriptor &3.

Last edited by MadeInGermany; 04-09-2013 at 08:48 AM..
This User Gave Thanks to MadeInGermany For This Post:
# 3  
Old 04-09-2013
It works perfect!

Thanks a lot!!
# 4  
Old 04-09-2013
OR..

Code:
awk '{print}NR%3==0{getline<"file_2";print}' file_1

This User Gave Thanks to pamu For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Merge files with multi-line stanzas

Hello. I try to merge few /etc/qconfig files in AIX (from few servers into one file on new server). Could you please help me what would be the best way to merge files that contain multi-line stanzas, like this: stanza1: attr1 = value1 attr2 = value2 attr3 = value3 stanza2: attr1 =... (7 Replies)
Discussion started by: kareem33
7 Replies

2. Shell Programming and Scripting

Merge multiple lines to one line when line starts with and ends with

example: comment Now_TB.table column errac is for error messages 1 - first 2 - second 3 -third ; in this example I need to be able to grab the comment as first word and ; as the last word and it might span a few lines. I need it to be put all in one line without line breaks so I can... (4 Replies)
Discussion started by: wambli
4 Replies

3. Shell Programming and Scripting

Merge two files line by line and column by column

Hi All, I have two files having oracle query result. I want to merge to files line by line and also with column File1 23577|SYNC TYPE 23578|Order Number|ConnectionState 23585|Service State|Service NameFile2 23577|AR Alarm Sync 23578|A5499|9 23585|7|test_nov7Result... (18 Replies)
Discussion started by: Harshal22
18 Replies

4. Shell Programming and Scripting

Take out First Line and merge all files

Hi Gurus, I have n number of files. Data which is in the files have column headers. I need to take them out and merge into one file. Can you help please? I need to do that little urgent. Thanks (4 Replies)
Discussion started by: raopatwari
4 Replies

5. Shell Programming and Scripting

Merge multi-line output into a single line

Hello I did do a search and the past threads doesn't really solve my issue. (using various awk commands) I need to combine the output from java -version into 1 line, but I am having difficulties. When you exec java -version, you get: java version "1.5.0_06" Java(TM) 2 Runtime... (5 Replies)
Discussion started by: flagman5
5 Replies

6. UNIX for Dummies Questions & Answers

Merge files and add file name to the end of each line

Hello everybody, I'm trying to merge a lot of files, but I want to include the filename to the end of each line. I've tried to use cat, but I got stuck. My files are for example: file01.001 123456 aaa ddd ee 458741 eee fff ee file02.003 478596 uuu ddd ee 145269 ttt fff ee ... (4 Replies)
Discussion started by: ernesto561
4 Replies

7. Shell Programming and Scripting

Merge 2 files, line by line

Hi all, I've been looking for a way to merge 2 simple text files, line by line using the shell: For example: File1: aaaa bbbb cccc File2: 1111 2222 3333 The 2 files have the same number of lines. I'm trying to obtain: aaaa 1111 bbbb 2222 cccc 3333 (0 Replies)
Discussion started by: venezia
0 Replies

8. Shell Programming and Scripting

merge files based on line by line

Hi, lets assume the following details.. file 1 has below details abc|1234|xyz abc1|1234|xyz1 abc2|1234|xyz2 and file 2 has below details pqr|124|lskd ebwer|325|dfb wf|234|sdb I need out put shown below abc|1234|xyz pqr|124|lskd abc1|1234|xyz1 ebwer|325|dfb abc2|1234|xyz2... (4 Replies)
Discussion started by: alnhk
4 Replies

9. Shell Programming and Scripting

How to use Perl to merge multi-line into single line

Hi, Can anyone know how to use perl to merge the following multi-line information which beginning with "BAM" into one line. For each line need to delete the return and add a space. Please see the red color line. ******Org. Multi-line) BAM admin 101.203.57.22 ... (3 Replies)
Discussion started by: happyday
3 Replies

10. Shell Programming and Scripting

compare the column from 3 files and merge that line

I have 3 file, each of has got 80000 records. file1.txt ----------------------- ABC001;active;modify;accept; ABC002;notactive;modify;accept; ABC003;notactive;no-modify;accept; ABC004;active;modify;accept; ABC005;active;no-modify;accept; file2.txt ---------------------------... (8 Replies)
Discussion started by: ganesh_mak
8 Replies
Login or Register to Ask a Question