LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   help on bash (https://www.linuxquestions.org/questions/programming-9/help-on-bash-551146/)

ovince 05-03-2007 10:56 PM

help on bash
 
Hi,

I have this simple awk script that cross-match 2 tables/files using for example their 3th columns

awk '{v=FNR==1?"\n":$3} NR==FNR
{a[v]=$3; b[v]=$0; next}
v in a {print b[v],$0}' fileName1 fileName2


I would like to put this function into .bashrc

function awkcrossmatch() {
tmp=$1
awk '{v=FNR==1?"\n":$tmp} NR==FNR
{a[v]=$tmp; b[v]=$0; next}
v in a {print b[v],$0}' $2 $3
}

Why this calling sequence doesnot work?

awkcrossmatch '$3' fileName1 fileName2


How to write this properly?


Thanks
oliver

archtoad6 05-04-2007 12:30 PM

Please put your code in "Code" tabs -- makes it much easier to read. Also, posting the errors you received can be very useful in diagnosing your problem

Try this:
Code:

awkcrossmatch () {
  awk -v col="$1" '{v=FNR==1?"\n":col} NR==FNR
  {a[v]=col; b[v]=$0; next}
  v in a {print b[v],$0}' $2 $3
}

I think you were running afoul of awk's quoting rules. Suggest reading the gawk info page. (##gawk in Konqueror.)

What I have written above assumes you will supply the column # w/ the leading '$' sign, as in your example.


All times are GMT -5. The time now is 12:19 AM.