Combine two files using script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combine two files using script
# 1  
Old 02-12-2013
Combine two files using script

Hi

please help me to combine below two files into one file

file1
HTML Code:
10.238.54.1 enk-ras-bng-cse-01 10.10.10.10
10.238.56.225 ngp-ras-bng-cto-01 10.10.10.10
file2

HTML Code:
10.238.54.1 enk-ras-bng-cse-01 20.20.20.20
10.238.56.225 ngp-ras-bng-cto-01 20.20.20.20
Required output file

HTML Code:
10.238.54.1 enk-ras-bng-cse-01 10.10.10.10 20.20.20.20
10.238.56.225 ngp-ras-bng-cto-01 10.10.10.10 20.20.20.20
# 2  
Old 02-13-2013
Code:
join -1 1 -2 1 -o 1.1 1.2 1.3 2.3 file1 file2

This User Gave Thanks to Yoda For This Post:
# 3  
Old 02-13-2013
hi

tnx ,its working.

HTML Code:
root@blr-svr-oclan-01 # join -1 1 -2 1 -o 1.1 1.2 1.3 2.3 z1 z2
10.238.54.1 enk-ras-bng-cse-01 10.10.10.10 20.20.20.20
10.238.56.225 ngp-ras-bng-cto-01 10.10.10.10 20.20.20.20
root@blr-svr-oclan-01 # 
can you explain below line in detail, as iam having similar requirement for other reports.
HTML Code:
join -1 1 -2 1 -o 1.1 1.2 1.3 2.3 z1 z2
# 4  
Old 02-13-2013
join -1 1 -2 1- from join manual page:
Quote:
-1 f Join on field f of file 1. Fields are numbered starting with 1.
-2 f Join on field f of file 2. Fields are numbered starting with 1.
-o 1.1 1.2 1.3 2.3 - from join manual page:
Quote:
-o list Each output line comprises the fields specified in list, each element of which has the form n.m, where n is a file number and m is a field number.
The common field is not printed unless specifically requested.
This User Gave Thanks to Yoda For This Post:
# 5  
Old 02-13-2013
Code:
awk 'NR==FNR { a[$1]=$3; next }  $1 in a { print $0,a[$1] }' file1.txt file2.txt

# 6  
Old 02-13-2013
Code:
cat file2|cut -f3 -d ' ' > file3
paste -d ' ' file1 file3


Last edited by Scrutinizer; 02-13-2013 at 05:39 AM.. Reason: code tags
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

Korn shell Script to combine Two files in one

Hello All , I am new to this Forum, I am trying to write a script to combine two data files with 1 column in common and others columns are different . File1 Apple 29 tomatao 4 grapes 25 File2 Apple fruit tomatao veg grapes fruit other (3 Replies)
Discussion started by: gagan0119
3 Replies

3. Shell Programming and Scripting

Combine 2 files

How to cilnd desided output from file1 and file2 with awk. file1 1 2 7 8 9 14 15 16 21 22 23 28 29 30 file2 aa bb cc dd ee ff gg hh ii jj kk ll (3 Replies)
Discussion started by: theshashi
3 Replies

4. Shell Programming and Scripting

combine two files...

Hi, i have two files. i want to combine records from these two files in below manner :- first line from first file(1st line) 2nd line from 2nd file(1st line) 3rd line from 1st file(2nd line) 4th line from 2nd file(2nd line) so on.... (1 Reply)
Discussion started by: deepakiniimt
1 Replies

5. UNIX for Dummies Questions & Answers

Combine two files using shell script

I need to combine two files based on the content in first column and combine it into one file . For example : file1: A 10 B 20 C 30 D 40 File2: B 200 E 500 A 100 D 400 Need the output in this format: file 3 : column 1 Column 2 Column 3 A 10 100 B 20 ... (4 Replies)
Discussion started by: tsm2011
4 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

Combine new files

Hi All , Any one help me to combine two files in shell scripting .Below is my requrement i have 2 files as follows filea newyork America Texas America london Engalad Fileb abc def xyz i have to combine this file as follows newyork America abc Texas ... (1 Reply)
Discussion started by: ajmalc
1 Replies

8. Shell Programming and Scripting

Combine files with same name

I need a script that combines files with the same name. These files are on a windows directory but the PC has Cygwin so i have a limited unix command set. What I've got; WebData_9_2007-09-20.txt WebData_9_2007-09-20.txt WebData_9_2007-09-21.txt WebData_9_2007-09-20.txt... (4 Replies)
Discussion started by: jmwhitford
4 Replies

9. UNIX for Dummies Questions & Answers

Combine 2 files

Some one plz give me the answer we have 3 files 1 and 2 are given and we need to get 3 file by using some trick file_one.dat AMITH ARUN ARVIND file_two.dat (these are Variables) X Y Z and we need to write scirpt in file_two.data and get the answer in file_three.dat as fallows... (2 Replies)
Discussion started by: Nekki
2 Replies

10. UNIX for Dummies Questions & Answers

how to combine two files

i need to combine two file. These two files have the same line number, and i need to combine each corresponding line. I tried the paste, but i need coma as the delimeter. are there anyway to do it? thanks. (4 Replies)
Discussion started by: tao
4 Replies
Login or Register to Ask a Question