join requires the files to be in the same order, so doesn't sound like what you need.
awk is an entire self-contained programming language for dealing with flat files, with powerful abilities to select columns, use associative arrays, and match regular expressions. In some ways it's even more powerful than perl, or at least more convenient -- columns are built deeply into the language in a way perl's mostly abandoned. But perl is a general-purpose language, and awk is not...
$ awk -F":" 'NR==FNR{A[$1]=$0; next}; $1 in A { print A[$1] }' file2 file1
Monkey:loves:banana
Human:hates:banana
$
@Corona68::
thanks.. but doesn't work with my files... nor with the examples given above..
i get no output at all..
im using gawk with windows.. maybe thats why??
i converted the quotes::
gawk -F":" "NR==FNR{A[$1]=$0; next}; $1 in A { print A[$1] }" 11.txt 22.txt
i am learning awk and have started looking into one liners shown in this forum. i have doubt in this. i have just expanded this oneliner for my understanding and trying to execute it but it throws error. please help me
bash-3.00# cat nawktest
#!/usr/bin/nawk -f
BEGIN {FS=":"}
if (FNR==NR)
{
a[$1]=$2;
next;
}
for ($2 in a)
{
print $0 FS a[$2];
}
bash-3.00# ./nawktest test.txt test1.txt
/usr/bin/nawk: syntax error at source line 3
context is
>>> if <<< (FNR==NR)
/usr/bin/nawk: bailing out at source line 3
bash-3.00#
finally this has been mastered::
this could be useful to some1::
gawk -F: "FNR==NR{a[($1)]=$2;next}$1 in a{print $0 FS a[$1]}"
^ matching field file1 ^ matching field file2
^ the field we want to append from file1
^ matching field file2(agian)
@M@LIK, the parentheses are superfluous ( FNR==NR{a[$1]=$2 .. ).
So to be clear, this is gawk running straight on windows? This could never function on Unix since there should be single quotes around the awk statements, otherwise the shell will interpret the variables...
@Scrutinizer::
O.o
it was about awk not windows or dos??
yea.. on windows you have to use double quotes.. unix single quote.. thats a minor thing
and yes its working very well.. u can try it on unix also after replacing the quotes.. the rest is the SAME
-_-
I wouldn't call cmd.exe a "shell" but it is fun putting quotes inside of your quotes. I suggest using files and gawk -f input.awk for any awk which involves a string on Windows.
@Scrtinizer: I do think it should still be in the awk forum, since the original post wasn't pertaining to cmd.exe quoting issues.