Combine two files using shell script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Combine two files using shell script
# 1  
Old 08-09-2011
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 200
C 30 xxx
D 40 400
E XXX 500
How can this be done with shell script. Please advise.
# 2  
Old 08-09-2011
Code:
 awk '
  FNR == NR { as[$1] = $2 }
  FNR != NR { bs[$1] = $2 }
  END {
    for (a in as)
      if (a in bs)
        print a, as[a], bs[a]
      else
        print a, as[a], "XXX"
    for (b in bs)
      if (b in as)
        print b, as[b], bs[b]
      else
        print b, "XXX", bs[b]
}' file1 file2 | sort -u

This User Gave Thanks to yazu For This Post:
# 3  
Old 08-10-2011
Thanks for the response. I needed some clarification regarding the response as I am very very new to awk & shell scripting.

I copied the code as it is and saved it as test.ksh and tried executing it. But I get error :
Code:
awk: syntax error near line 6
awk: illegal statement near line 6
awk: syntax error near line 8
awk: illegal statement near line 8
awk: syntax error near line 11
awk: illegal statement near line 11
awk: syntax error near line 13
awk: illegal statement near line 13

The file1 & file2 are located in the same directory where test.ksh is placed, but still I get the error. Can you please point the mistake I am doing.

Is there a way I can redirect the output to a third file file3?

Last edited by pludi; 08-10-2011 at 05:09 AM..
# 4  
Old 08-10-2011
Try to use nawk instead of awk. To redirect the output use the standard shell tool: add >OUTPUTFILE_NAME to the end of the command.
This User Gave Thanks to yazu For This Post:
# 5  
Old 08-10-2011
It worked. Thank you so much.
 
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 shell files

I am hoping the attached shell file is at least somewhat close to this. Combining two shell file into one, where depending on the user input of"y" or "n" different commands are run. Thank you :). So first he user is asked for an ID to match, once the id is entered a script is run that uses... (4 Replies)
Discussion started by: cmccabe
4 Replies

4. Shell Programming and Scripting

Combine shell files

The script below is an attempt to combine 3 shells into 1. The first part: match.sh prompts the user for an id of a patient and runs a match script based on the response of "y" or "n". After completing the user is asked if there are additional patients, and based on "y" or "n" a certain action... (2 Replies)
Discussion started by: cmccabe
2 Replies

5. Shell Programming and Scripting

Combine two files using script

Hi please help me to combine below two files into one file file1 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 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 ... (5 Replies)
Discussion started by: surender reddy
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

how to combine two files into one file using shell scrip

eg. file 1 has: 4 0 8628380 653253 0 0 0 0 0 0 2 0 8626407 655222 0 0 0 0 0 0 4 0 8633729 647892 0 0 0 0 0 0 5 0 8646253 635367 0 0 0 0 0 0 file 2 has: 4798 48717 11554 5408 56487 14359 6010 58415 15220 5541 41044... (2 Replies)
Discussion started by: netbanker
2 Replies

8. Shell Programming and Scripting

combine 3 excel files using shell.

Is there any way to combine 3 excel files into one comma separated file, after removing the header row from all the three files. Is this possible? I have looked in FAQ and I did not find anything. Appreciate any suggestions or links to resources. Radhika. (11 Replies)
Discussion started by: radhika
11 Replies

9. Shell Programming and Scripting

Combine file using shell script

Hi, i have 2 file 1.txt : 2.txt then i would the result become : can anybody help me.. regards, (1 Reply)
Discussion started by: justbow
1 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