Creating a Third File from Information Contained in Two Files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating a Third File from Information Contained in Two Files
# 1  
Old 08-27-2012
Creating a Third File from Information Contained in Two Files

In advance, I appreciate any help or tips. I apologize for not providing examples of scripts I have already tried, the reason is that I am new to programming and do not really no where to start. I think this is possible with awk, but do not know how to go about learning how to write the script that would do the trick (ie, which commands to read up on).


I have two files:

File1
Code:
ID    X
001 2   
002 2
003 1
004 1
005 2
006 1
007 2
008 1
009 1
010 1

File2
Code:
ID  Y
003 1 
004 1 
008 1 
010 1

I would like to create a third file, File3, from File1 and File2 based on the following rules:
-the first two columns of File3 will be identical File1
-the third column in File3 will:
---contain the value from column2,File2 if the value from column1,File1 is present in column1,File2.
---contain the value from column2,file1 if the value from column2, file1 was 2
---contain the value "missing" if both (a) the ID in column1,File1 is not present in Column1,File2 and (b) the value in column2,File1 is not 2

Desired Output
Code:
ID    X Y
001 2 2    
002 2 2 
003 1 1 
004 1 1
005 2 2
006 1 missing
007 2 2
008 1 1
009 1 missing
010 1 1

Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 08-27-2012 at 08:10 PM.. Reason: code tags, please!
# 2  
Old 08-27-2012
for me the easiest way it's to use join :
Code:
join -a 1 -e MISS -o 1.1,1.2,2.2 file1 file2
001 2 MISS
002 2 MISS
003 1 1
004 1 1
005 2 MISS
006 1 MISS
007 2 MISS
008 1 1
009 1 MISS
010 1 1

and after you could use a simple awk to do your stuff.

explains :
-a 1 : will show line in the firt event if they aren't in the second file
-o : format the output (it's not necessary in this case)
# 3  
Old 08-27-2012
Code:
awk 'FNR==NR{f2[$1]=$2;next} {print $0, ($1 in f2)?f2[$1]:($2==2)?$2:"missing"}' file2 file1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Looping the files for getting the file size information

Am using Linux 3.10 years.txt is input source file which has the list of year like below 2013 2014 2015 2016 Other Input files are XX_TEST1_YR2016_01012018.csv XX_TEST1_YR2015_01012018.csv XX_TEST1_YR2014_01012018.csv XX_TEST1_YR2013_01012018.csv XX_TEST2_YR2016_01012018.csv... (2 Replies)
Discussion started by: weknowd
2 Replies

2. UNIX for Beginners Questions & Answers

Get information from one files, based on data from other file

Hello. I am trying to get some info from log file. I have fileA , which contains all the country prefixes (the file contains one column and "n" rows ). And i have fileB, which contains huge data of phone numbers (the file contains one column and "n" rows). What i want to do is, to count... (7 Replies)
Discussion started by: dragonfly85
7 Replies

3. Shell Programming and Scripting

awk multiply values contained in 2 different files

Hi Everyone ! I have two files with the same configuration and I want to multiply corresponding values and write the result in a file. Let say 2 header lines and then lines of values (with not constant number of columns): more file1.txt --> BLABLABLA BLABLABLA 1 2 3 4 1 2 3 1 2 1... (7 Replies)
Discussion started by: Youm
7 Replies

4. UNIX for Dummies Questions & Answers

Searching for terms contained in 2 separate files

Hi All ! As a dummy, I try to search for multiple string of characters contained in 2 types of list. Not clear? No it's not. Example: I have a tab-delimited file called "inventory" with 2 columns that looks like that: #ref #year,color xrt3 2000,blue gf5 2000,red,green,yellow... (4 Replies)
Discussion started by: lucasvs
4 Replies

5. AIX

Find the patch that contained a particular file.

Hi All, I have an AIX executable (/usr/bin/shell) in my system. I want to know if this file was updated by any of the installed fixes/apars on my system. What is the AIX way of finding this information? Greetings, Peter (1 Reply)
Discussion started by: petervg
1 Replies

6. Shell Programming and Scripting

Extracting information and creating numeric values using awk

I have some variables containing for example m02-npt02-sr40-syn-dv0p01-16x12drw m02-npt02-sr40-syn-dv0p02-16x12drw m02-npt02-sr40-dv0p03-syn-16x12drw I want to extract the dv entry for example dv0p01 dv0p02 dv0p03 Then I want to convert to a numeric, the p specifies the... (5 Replies)
Discussion started by: kristinu
5 Replies

7. Shell Programming and Scripting

File renaming from list of names contained in another file

I have to rename a large number of files so that the name of each file corresponds to a code number that is given side by side in a list (textfile). The list contains in column A the filename of the actual files to be renamed and in column B the name (a client code, 9 digits) that has to be... (7 Replies)
Discussion started by: netfreighter
7 Replies

8. UNIX for Dummies Questions & Answers

Modifying a particulae data in a line contained in File

Hi, I have a file containing data: systemname/userid/password/comment systemname1/userid1/password1/comment1 systemname2/userid2/password2/comment2 I want to modify the "password" using script. Can anybody help me with this problem?:confused: (2 Replies)
Discussion started by: pgarg1989
2 Replies

9. Shell Programming and Scripting

Find a string under a directory that is contained in another file

Hi I am loking for some help in writing a script that will take a number that is located in one file and search a folder structure for that string in any file under that directory. I can do this manually with : find /"directory" -type f -exec grep -l 'Number String' {} \; But now I will... (3 Replies)
Discussion started by: BMC
3 Replies

10. UNIX for Advanced & Expert Users

How to read an Xml record contained in a file--urgent

Hi I have an xml file which has multiple xml records.. I don't know how to read those records and pipe them to another shell command the file is like <abc>z<def>y<ghi>x........</ghi></def></abc> (1st record) <jkl>z<mno>y<pqr>x........</pqr></mno></jkl> (2nd record) Each record end... (4 Replies)
Discussion started by: aixjadoo
4 Replies
Login or Register to Ask a Question