How to merge two files?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to merge two files?
# 1  
Old 09-08-2016
How to merge two files?

Dear Frens,

I have two files and need to merge into one file. Like

File_1:
Code:
Field1 Field2
1         4

File_2:
Code:
Field1 Field2
3         5

I need one single output as
File_1:
Code:
Field1 Field2
1         4
3         5

This means taking header from either file.

Please help me. Your small help could save my lots of time

Thank you so much

Last edited by Don Cragun; 09-08-2016 at 09:32 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 09-08-2016
What have you tried? We are here to help you learn; not to act as your unpaid programming staff.

What operating system are you using?

What shell are you using?
# 3  
Old 09-08-2016
Thank you for a prompt response. I have used cat command but this simply merged two files. I need to get header+ data from one file and only data from a second file. I am using Window 7.
# 4  
Old 09-08-2016
Assuming that you have loaded something like Cygwin on your Windows system and are using the shell in that package, try:
Code:
head -n +2 File_2 >> File_1

which will add everything starting with the 2nd line in File_2 to the end of File_1.
# 5  
Old 09-08-2016
Thank you again. I tried the mentioned code but this code merged first two line from the second file i.e. header and extra one record.
I have used the same code, only file name is different.

---------- Post updated at 08:30 PM ---------- Previous update was at 08:14 PM ----------

Hi Don,

I used -- sed '1d' test2.txt >> test1.txt

It worked.

Thank you so much for your help. AtleastI learned how we can append data from one file to another.

Last edited by Don Cragun; 09-08-2016 at 11:17 PM.. Reason: Add ICODE tags.
# 6  
Old 09-08-2016
Please show us the output you get (in CODE tags) from the command:
Code:
od -bc File_2

where File_2 is replaced with the real name of your 2nd file. And please show us the EXACT command line you used.

Working on Windows systems, we always have to be concerned about text file formats. On UNIX and similar systems, lines are terminated by a <newline> character. On Windows, lines are separated by the character pair <carriage-return><newline>. Standard UNIX system text handling utilities assume that files being processed are UNIX format text files.
# 7  
Old 09-08-2016
Hi,
when i test od -bc test1.txt, the result is

Code:
0000300 063 062 174 061 065 063 062 174 103 064 174 060 063 012 060 060
       3   2   |   1   5   3   2   |   C   4   |   0   3  \n   0   0
0000320 060 064 066 070 070 104 174 064 066 070 070 174 064 066 070 070
       0   4   6   8   8   D   |   4   6   8   8   |   4   6   8   8
0000340 174 105 064 174 061 070 012 060 060 060 071 071 061 061 102 174
       |   E   4   |   1   8  \n   0   0   0   9   9   1   1   B   |
0000360 071 071 061 061 174 071 071 061 061 174 101 064 174 060 071 012
       9   9   1   1   |   9   9   1   1   |   A   4   |   0   9  \n

Moderator's Comments:
Mod Comment Please use CODE tags (as required by forum rules) when displaying sample input, sample output, and code segments.

Last edited by Don Cragun; 09-08-2016 at 11:11 PM.. Reason: Add CODE and ICODE tags, again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merge files and generate a resume in two files

Dear Gents, Please I need your help... I need small script :) to do the following. I have a thousand of files in a folder produced daily. I need first to merge all files called. txt (0009.txt, 0010.txt, 0011.txt) and and to output a resume of all information on 2 separate files in csv... (14 Replies)
Discussion started by: jiam912
14 Replies

2. Shell Programming and Scripting

Checking in a directory how many files are present and basing on that merge all the files

Hi, My requirement is,there is a directory location like: :camp/current/ In this location there can be different flat files that are generated in a single day with same header and the data will be different, differentiated by timestamp, so i need to verify how many files are generated... (10 Replies)
Discussion started by: srikanth_sagi
10 Replies

3. UNIX for Dummies Questions & Answers

Merge files

Hi, I would like to know how can I merge files based on their coordinates, but mantaining the score of each file in the output file like: Note: 1st column is for chromosome, 2nd for start, 3rd for end of segment, 4th for score file1: 1 200 300 20 1 400 500 30 file2: 1 200 350 30 1... (1 Reply)
Discussion started by: fadista
1 Replies

4. Shell Programming and Scripting

MERGE FILES

Hi all! How could I merge all the text files (in format xml) of a single folder, after having deleted from each of them all the text from its beginning up to a specific string: "<body>" ? Thanks a lot! mjomba (4 Replies)
Discussion started by: mjomba
4 Replies

5. Shell Programming and Scripting

Merge 2 files

Hi , This might be the stupidest question I am asking, but I am struck with this problem. I am trying to merge 2 files, file1 has header and file2 has contents. while I merge them , it merges from the 1st line of file1. for ex: file1 col1|col2|col3| file2 123|234|456|... (2 Replies)
Discussion started by: rashmisb
2 Replies

6. Shell Programming and Scripting

How can i merge these two files into several...

Given are File A and File B File A has for example 5 lines: AAA BBB CCC DDD EEE File B has 3 lines: 111 222 333 How can i merge A and B into: 111 222 333 AAA (first line from A) then a new file: (4 Replies)
Discussion started by: Y-T
4 Replies

7. Shell Programming and Scripting

Merge 2 files

Hello, i'd like a bash script to merge 2 files without duplicate lines. Example : file1 : toto titi file2 : toto tata Expected result, file3 : toto (5 Replies)
Discussion started by: Celmar
5 Replies

8. Shell Programming and Scripting

Merge files of differrent size with one field common in both files using awk

hi, i am facing a problem in merging two files using awk, the problem is as stated below, file1: A|B|C|D|E|F|G|H|I|1 M|N|O|P|Q|R|S|T|U|2 AA|BB|CC|DD|EE|FF|GG|HH|II|1 .... .... .... file2 : 1|Mn|op|qr (2 Replies)
Discussion started by: shashi1982
2 Replies

9. Shell Programming and Scripting

merge files

Hi, i have the files f1 and f2 like: files f1: c1 a1 c2 a2 c3 a3 file f2: c1 b1 c2 b2 c3 b3 i want merge the f1 and f2 file to f3 file like: c1 a1 b1 c2 a2 b3 c3 a3 b3........ .... . . please help me onthis..... (5 Replies)
Discussion started by: koti_rama
5 Replies

10. Shell Programming and Scripting

How to merge files

Hello guys, I gotta question, i have a lot of log files (simple text) and i need to merge them in group of 10 files, one next to the other, that have sense? For example, i have the files: File1 File2 File3 File4 . . File100 I need to merge the contents of each file into a new file... (3 Replies)
Discussion started by: lestat_ecuador
3 Replies
Login or Register to Ask a Question