Compare two text files and write the output to a third file

March 3, 2013 at 18:39:11
Specs: Windows 7
I have two text files
File1(2 columns)
F0100010 BTA-29644-no-rs
F0100020 BTA-29515-no-rs
F0100030 BTA-29334-no-rs
F0100040 BTA-28763-no-rs
F0100050 BTA-32647-no-rs
F0100060 BTA-103663-no-rs
F0100070 Hapmap49509-BTA-17127


ABO_1444 F0100010 C C
ABO_1444 F0100020 G G
ABO_1444 F0100030 A T
ABO_1444 F0100040 A A
ABO_1444 F0100050 A A
ABO_1444 F0100060 G G
ABO_5555 F0100010 G G
ABO_5555 F0100020 G G

Desired output
ABO_1444 F0100010 C C BTA-29644-no-rs
ABO_1444 F0100020 G G BTA-29515-no-rs
ABO_1444 F0100030 A T BTA-29334-no-rs
ABO_1444 F0100040 A A BTA-28763-no-rs
ABO_1444 F0100050 A A BTA-32647-no-rs
ABO_1444 F0100060 G G BTA-103663-no-rs
ABO_5555 F0100010 G G BTA-29644-no-rs
ABO_5555 F0100020 G G BTA-29515-no-rs

column 1 of file1 is to be matched to column2 of file two. Files are unsorted and have different number of rows.
Can you please suggest an awk one liner for my requirement?

See More: Compare two text files and write the output to a third file

Report •

March 3, 2013 at 20:23:35
Sorry, my awk is virtually non-existant, especially for this application. Since you spec'd win-7, and not "unix", here's a batch solution that might serve:
::==== begin batchscript
@echo off & setlocal enabledelayedexpansion
for /f "tokens=1-2" %%a in (cells1) do set %%a=%%b
for /f "tokens=1-2*" %%e in (cells2) do (
echo %%e %%f %%g !%%f!
::====== end batch
ps: re-post into "programming" for a better solution! ;-)

Report •

March 3, 2013 at 21:52:01
Thanks nbrane..but i'm not familiar with the batchfile thing..i'm quite a newbie to programming world...awk, sed or perl solution would be a lil more easy for me to understand...

Report •

March 4, 2013 at 07:48:21
Are you performing this in a Windows environment, or a POSIX one? Awk, sed, and perl are not native Windows tools. If the environment is Win7, then PowerShell becomes a valid option.

EDIT: Powershell version:

$a = @{ }
Get-Content File1 | ForEach-Object { $k, $v = -split $_ ; $a[$k] = $v }
Get-Content File2 | ForEach-Object { "$_ $($a[(-split $_)[1]])" }

How To Ask Questions The Smart Way

Report •

Related Solutions

March 6, 2013 at 17:26:15
I figured out how to do it..used hash..worked great for me..Thanks nbrane and razor2.3 for you help :-)

Report •

Ask Question