merging 2 file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers merging 2 file
# 1  
Old 09-10-2009
merging 2 file

I have 2 files

Code:
file1.txt
a   123   aqsw
c  234    sfdr

Code:
fil2.txt
b  345   hgy
d  4653 jgut

I want to merger in such a manner the the output file should be
Code:
outfile.txt
a   123   aqsw
b  345   hgy
c  234    sfdr
d  4653 jgut

Do we have any command to achive this?
# 2  
Old 09-10-2009
Code:
nawk 'FNR==NR{f1[FNR]=$0;next}{print f1[FNR] ORS $0}' file1.txt file2.txt
OR
paste -d '\n' file1.txt file2.txt

# 3  
Old 09-10-2009
I got your requirement differently, as it is not explicitly told !!

If at all you want both the files to be sorted based up on the first column and displayed, then use the following.

Code:
sort -k 1 file1 file2

# 4  
Old 09-10-2009
Quote:
Originally Posted by thegeek
Code:
sort -k 1 file1 file2

"-k 1" is not really necessary as far as I can tell
# 5  
Old 09-11-2009
Quote:
Originally Posted by Vi-Curious
"-k 1" is not really necessary as far as I can tell
Yes !! As far as it is first field it is not needed.

But that is a generic way i use, that is whichever field i would want to sort based upon i will give that NUMBER.

Anyway thanks for pointing it out.
# 6  
Old 09-11-2009
Thanks for your reply. I don't need to sort the file.
I just want the out file should have first line from file1 and 2nd line from 2nd file and 3rd line from first file and 4th line from second file. i.e. in output file, all odd numbered lines are from first file and all even numbered lines are from second file.
# 7  
Old 02-24-2010
Dear friend,

You use the following way also to add the file content. Using the head and tail you can get the result.

file content
a 123 aqsw
c 234 sfdr

file1 content
b 345 hgy
d 4653 jgut

In the following coding "out" is the new file,which contain the content of the above two files. what you need
Code:
 
head -n 1 file > out 
head -n 1 file1 >> out  
tail -n 1 file >> out 
tail -n 1 file1 >> out

out content
a 123 aqsw
b 345 hgy
c 234 sfdr
d 4653 jgut
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merging the lines of a file

Hello, I have a file with few lines starting with a digit (1-5 only ) followed by a dot (.). Remaining all the lines to be merged with its previous numbered lines. Merging must be done with a space. E.g., Source file: 3. abc def xyz 5. pqr mno def 4. jkl uvw 7. ghi 1. abc xyz 6. mno... (4 Replies)
Discussion started by: magnus29
4 Replies

2. Shell Programming and Scripting

Merging File with headers

Hi I need to merge 4 files. The issue i am facing is all the files have headers and i do not want them in the final output file. Can anybody suggest how to do it? (5 Replies)
Discussion started by: Arun Mishra
5 Replies

3. Shell Programming and Scripting

merging two file

Dear All, I have two file like this: file1: a1234 b1235 c4678 d7859 file2 : e4575 f7869 g7689 h9687 I want output like this: a1234 b1235 c4678 (2 Replies)
Discussion started by: attila
2 Replies

4. UNIX for Dummies Questions & Answers

Merging data in a file

Hello, Firstly I just wanted to say that I'm not a programmer at all and appreciate any help you can give. I am trying to create a shellscript that reformats the file and adding up colums 5 and 6 for those sections that are continuation of the previous line(s) (signified by beginning with '*')... (4 Replies)
Discussion started by: neilh1703
4 Replies

5. Shell Programming and Scripting

Merging Frequencies in a File

hello, I have a file which has the following structure: word <TAB> frequency The same word can have multiple frequencies: John <TAB> 60 John <TAB> 20 John <TAB> 30 Mary <TAB> 1000 Mary <TAB> 800 Mary <TAB> 20 What I need is a script which could merge all these frequencies into one single... (10 Replies)
Discussion started by: gimley
10 Replies

6. Shell Programming and Scripting

Extracting a column from a file and merging with other file using awk

Hi All: I have following files: File 1: <header> text... text .. text .. text .. <\header> x y z ... File 2: <header> text... text .. text .. (4 Replies)
Discussion started by: mrn006
4 Replies

7. Shell Programming and Scripting

Merging lines in a file

Hi, I want to merge the lines starting with a comma symbol with the previous line of the file. Input : cat file.txt name1,name2 ,name3,name4 emp1,emp2,emp3 ,emp4 ,emp5 user1,user2 ,user3 Output name1,name2,name3,name4 emp1,emp2,emp3,emp4,emp5 (9 Replies)
Discussion started by: mohan_tuty
9 Replies

8. UNIX for Dummies Questions & Answers

merging two lines in a file

Hi All, I want to merge two lines in a file till the end of the file. So what could be the command to get so. say file name : sample.txt contents: country=1 send apps =1 rece=2 country=2 send apps =3 rece=3 .. ... output: country=1;send apps =1 rece=2 country=2;send apps =3... (6 Replies)
Discussion started by: thaduka
6 Replies

9. Shell Programming and Scripting

Need help for 2 data file merging

Hello Please help me to write Shell script. I want to merge 2 data files . The data files have common columns The data file A have 3 columns Host Version Numberof Failuers The data file B have also 3 coulmns Host Version NumberofFailuers . I want to merge A and B file... (2 Replies)
Discussion started by: getdpg
2 Replies

10. Shell Programming and Scripting

merging two file in an ordered way

actually, it seems somewhat confusing.. let me clearify it i want a file having all the attributes produced by ls -lc command. i want to add one more thing i.e. time of last access to a file attribute. so how can i merge these two things in a single file in a columnar way. i tried with these... (2 Replies)
Discussion started by: raku05
2 Replies
Login or Register to Ask a Question