The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
join two files koti_rama Shell Programming and Scripting 5 08-05-2008 05:20 AM
Join two files koti_rama Shell Programming and Scripting 4 06-10-2008 07:15 AM
how to join files jxh461 UNIX for Dummies Questions & Answers 5 08-23-2007 08:11 AM
join files mohan705 Shell Programming and Scripting 3 03-15-2007 06:51 AM
Join Files choppas Shell Programming and Scripting 2 10-18-2006 11:03 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-27-2009
pinnacle pinnacle is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 182
Left join on files using awk

Quote:
File_A:
NY
NJ
PA
CA
VA
TN
Quote:
File_B:
NY hello
NJ 3
CA 1
VA 5
Quote:
ouptut:
NY,Found
NJ,Found
PA,
CA,Found
VA,Found
TN,
Code:
nawk 'NR==FNR{a[$1];next} {if($1 in a) print $1,"Found" else print}'  OFS="," File_B File_A
The above code is not working help is appreciated
  #2 (permalink)  
Old 05-27-2009
tonan tonan is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 11
well this was easy, you are missing on the syntax

nawk 'NR==FNR{a[$1];next} {if($1 in a) print $1,"Found";print}' OFS="," File_B File_A

-----Post Update-----

sample output

bash-3.00$ nawk 'NR==FNR{a[$1];next} {if($1 in a) print $1,"Found";print}' OFS="," File_B File_A
NY,Found
NY
NJ,Found
NJ
PA
CA,Found
CA
VA,Found
VA
TN
  #3 (permalink)  
Old 05-27-2009
pinnacle pinnacle is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 182
Quote:
Originally Posted by tonan View Post
well this was easy, you are missing on the syntax

nawk 'NR==FNR{a[$1];next} {if($1 in a) print $1,"Found";print}' OFS="," File_B File_A

-----Post Update-----

sample output

bash-3.00$ nawk 'NR==FNR{a[$1];next} {if($1 in a) print $1,"Found";print}' OFS="," File_B File_A
NY,Found
NY
NJ,Found
NJ
PA
CA,Found
CA
VA,Found
VA
TN

This is not the output i am looking for. please see the required output
  #4 (permalink)  
Old 05-27-2009
devtakh devtakh is offline
Registered User
  
 

Join Date: Oct 2007
Location: Bangalore
Posts: 514
Code:
awk 'FNR==NR{a[$1]++;next}{if($1 in a)print $1,"Found"; else printf("%s%s\n", $1,OFS)}' OFS="," B A

-Devaraj Takhellambam
  #5 (permalink)  
Old 05-27-2009
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,875
Or (use gawk, nawk or /usr/xpg4/bin/awk on Solaris)

Code:
awk 'NR == FNR { _[$1]; next }
($2 = $1 in _ ? "Found" : x) || 1
' OFS=, file_b file_a

Last edited by radoulov; 05-27-2009 at 02:54 PM..
  #6 (permalink)  
Old 05-27-2009
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,538
if you have Python, an alternative
Code:
#!/usr/bin/env python
file2=[i.split()[0] for i in open("file2").read().split("\n")]
for line in open("file1"):
    line=line.strip().split()
    if line[0] in file2:
        print line[0]," found"
    else:
        print line[0],","
output
Code:
# ./test.py
NY  found
NJ  found
PA ,
CA  found
VA  found
TN ,
  #7 (permalink)  
Old 05-27-2009
durden_tyler's Avatar
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2009
Posts: 535
Quote:
...
ouptut:
NY,Found
NJ,Found
PA,
CA,Found
VA,Found
TN,
...
And if you have perl, then:

Code:
perl -ne 'BEGIN{open(F,"fileb"); while(<F>){split;$found{$_[0]}="Found"} close(F)} {chomp; print "$_,$found{$_}\n"}' filea
Test:

Code:
$
$ cat filea
NY
NJ
PA
CA
VA
TN
$
$ cat fileb
NY hello
NJ 3
CA 1
VA 5
$
$ perl -ne 'BEGIN{open(F,"fileb"); while(<F>){split;$found{$_[0]}="Found"} close(F)} {chomp; print "$_,$found{$_}\n"}' filea
NY,Found
NJ,Found
PA,
CA,Found
VA,Found
TN,
$
tyler_durden
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:39 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0