Combine rows from two files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combine rows from two files
# 1  
Old 12-18-2012
Combine rows from two files

I have two files, called "File A" and "File B".

File A

HTML Code:
hdisk120:
iprod_0000151 
dprod_0000135 
iprod_0000148 
dprod_0000148 
iprod_0000157 
iprod_0000155 

hdisk118:
dprod_0000141 
dprod_0000134 
dprod_0000155 
iprod_0000166 
dprod_0000129 
lprod_0000017 
iprod_0000146 
dprod_0000148 

hdisk20:
loglv03 
stagelv /dbbackup

hdisk19:
iprod_0000075 
iprod_0000032 
iprod_0000087 
iprod_0000039 
iprod_0000074 
iprod_0000067 
iprod_0000052 
dprod_0000078 
iprod_0000051 
iprod_0000089 
dprod_0000097 
iprod_0000050 
dprod_0000047 
....

File B

HTML Code:
dprod_0000120            D_PERSON_PRSNL1178
dprod_0000121            D_R_M003
dprod_0000122            D_724ACCESS
dprod_0000123            D_A_M001
dprod_0000124            D_A_M002
dprod_0000125            D_A_M005
dprod_0000126            CER_CONF_DEF
dprod_0000127            CER_PREF_DEF
dprod_0000128            D_A_SMALL
dprod_0000129            D_CHARGE_EVENT0035
dprod_0000130            D_OMF_CHARGE_S4991
dprod_0000131            D_CLINICAL_EVE1333
dprod_0000132            D_WH_D_MEDIUM
dprod_0000133            D_OMF_CHARGE_S4991
dprod_0000134            D_PROD_DISPENS1603
dprod_0000135            D_CHARGE_EVENT0034
dprod_0000136            D_WH_F_SMALL
dprod_0000137            D_WH_RC_CLN_AREA
dprod_0000138            D_CE_EVENT_PRS2966
dprod_0000139            D_A_LARGE
dprod_0000140            D_A_LARGE
dprod_0000141            D_A_W002
dprod_0000142            D_BO
dprod_0000143            D_CLINICAL_EVE1333
dprod_0000144            D_A_MEDIUM
dprod_0000145            D_CHARGE_EVENT1155
dprod_0000146            MISC
dprod_0000147            D_WH_F_LARGE
dprod_0000148            D_CE_EVENT_PRS2966
dprod_0000149            D_CE_CODED_RES0028
dprod_0000150            D_DISPENSE_HX1608
dprod_0000151            D_CE_STRING_RE1340
dprod_0000152            D_OTG
dprod_0000153            D_CHARGE1156
I need to combine two files and put the rows from "File B" onto "File A" if the first column matches, i.e. If no match, "-------------------------" can be put or leave as blank.

Output File

HTML Code:
hdisk120:
iprod_0000151      dprod_0000151            D_CE_STRING_RE1340
dprod_0000135      dprod_0000135            D_CHARGE_EVENT0034
iprod_0000148       ---------------------------------------------
dprod_0000148      dprod_0000148            D_CE_EVENT_PRS2966
iprod_0000157       ---------------------------------------------
iprod_0000155       ---------------------------------------------

hdisk118:
dprod_0000141      dprod_0000141            D_A_W002
dprod_0000134      dprod_0000134            D_PROD_DISPENS1603
dprod_0000155      ---------------------------------------------
iprod_0000166       ---------------------------------------------
dprod_0000129      ---------------------------------------------
lprod_0000017       ---------------------------------------------
iprod_0000146       ---------------------------------------------
dprod_0000148      dprod_0000148            D_CE_EVENT_PRS2966

hdisk20:
loglv03 
stagelv /dbbackup  ---------------------------------------------

hdisk19:
iprod_0000075       ---------------------------------------------
iprod_0000032      ---------------------------------------------
iprod_0000087 
iprod_0000039 
iprod_0000074 
iprod_0000067 
iprod_0000052 
dprod_0000078 
iprod_0000051 
iprod_0000089 
dprod_0000097 
iprod_0000050 
dprod_0000047
Please advise. Smilie
# 2  
Old 12-18-2012
Code:
awk ' FILENAME=="FileB" {arr[$1]=$0}
        FILENAME=="FileA" { print $0, arr[$0] } ' FileB  FileA > newfile

Try that.
# 3  
Old 12-18-2012
It just returns FileA. None of FileB is appended.
Please advise.
PHP Code:
cat newfile
hdisk120
:
iprod_0000151
dprod_0000135
iprod_0000148
dprod_0000148
iprod_0000157
iprod_0000155

hdisk118
:
dprod_0000141
dprod_0000134
dprod_0000155
iprod_0000166
dprod_0000129
lprod_0000017
iprod_0000146
dprod_0000148

hdisk20

# 4  
Old 12-18-2012
try also:
Code:
awk -F"[_ ]" 'NR==FNR {a[$2]=$0;next} a[$2] {$0=$1"_"$2"\t"a[$2]}1' fileb filea

# 5  
Old 12-18-2012
awk uses associative arrays. the values should be $1 (red) - try:

Code:
awk ' FILENAME=="FileB" {arr[$1]=$0}
        FILENAME=="FileA" { print $1, arr[$1] } ' FileB  FileA > newfile

My bad....
This User Gave Thanks to jim mcnamara For This Post:
# 6  
Old 12-19-2012
Yes, it works perfect!
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

Combine multiple rows based on selected column keys

Hello I want to collapse a file with multiple rows into consolidated lines of entries based on selected columns as the 'key'. Example: 1 2 3 Abc def ghi 1 2 3 jkl mno p qrts 6 9 0 mno def Abc 7 8 4 Abc mno mno abc 7 8 9 mno mno abc 7 8 9 mno j k So if columns 1, 2 and 3 are... (6 Replies)
Discussion started by: linuxlearner123
6 Replies

3. Shell Programming and Scripting

Combine rows with awk

I have a file of 100,000 entries that look like: chr1 980547 980667 + chr1:980547-980667 chr1 980728 980848 + chr1:980728-980848 chr1 980793 980913 + chr1:980793-980913 I am trying to reformat them to into 5 columns that are tab delineated: chr1 980547 980667 + ... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. Shell Programming and Scripting

How to find DISTINCT rows and combine in one row?

Hi , i need to display only one of duplicated values and merged them in one record only when tag started with 3100.2.128.8 3100.2.97.1=192.168.0.12 3100.2.128.8=418/66/03e9/0044801 3100.2.128.8=418/66/03ea/0044601 3100.2.128.8=418/66/03e9/0044801 3100.2.128.8=418/66/03ea/0044601... (5 Replies)
Discussion started by: OTNA
5 Replies

5. Shell Programming and Scripting

Combine 2 files by rows

Hi, I have 2 files, each files contain about 1 millions lines and I want to joint them to build the new files which contain each 2 lines of file1 and 1 lines of file2. Newfiles: line1-file1 line2-file1 line1-file2 line3-file1 line4-file1 line2-file2 ..... Could someone help... (8 Replies)
Discussion started by: thinhnguyenfr
8 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

Deleting specific rows in large files having rows greater than 100000

Hi Guys, I need help in modifying a large text file containing more than 1-2 lakh rows of data using unix commands. I am quite new to the unix language the text file contains data in a pipe delimited format sdfsdfs sdfsdfsd START_ROW sdfsd|sdfsdfsd|sdfsdfasdf|sdfsadf|sdfasdf... (9 Replies)
Discussion started by: manish2009
9 Replies

8. Shell Programming and Scripting

3 files combine into one help please

Ok here is what I have file a {{BEGIN}} {{FAX File b 5555464584 5555465292 5555465828 5555485930 5555474939 File C }} ON ORDERS LESS THAN 70 LBS AND THE PACKAGE IS A COMBINED LENGTH AND GIRTH EQUAL TO OR LESS THAN 108" PLEASE UTILIZE UPS. ... (4 Replies)
Discussion started by: sctxms
4 Replies

9. HP-UX

How to combine 2 different files

Hi : I have a file containing the print queues with their IP address. I wanted to combine the 'lpstat' output with their respective IP address. For example : zebhtrmb-6078 lgonzale priority 0 Mar 17 11:50 on zebhtrmb with zebhtrmb-6078 lgonzale priority 0 ... (1 Reply)
Discussion started by: rdasari
1 Replies

10. Shell Programming and Scripting

combine two files

I have two files: file1 and file2 the content of files1 is: 13 22 333 42 56 55 ... the content of file2 is: aa dd cc ee ff gg ... (1 Reply)
Discussion started by: fredao1
1 Replies
Login or Register to Ask a Question