LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-03-2007, 10:56 PM   #1
ovince
Member
 
Registered: Mar 2007
Posts: 77

Rep: Reputation: 15
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
 
Old 05-04-2007, 12:30 PM   #2
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
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.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
Bash Sleep vs crontab and bash serial port nutthick Programming 4 06-01-2006 02:42 AM
Bash: Print usage statement & exit; otherwise continue using Bash shorthand operators stefanlasiewski Programming 9 02-07-2006 05:20 PM
$LINENO can't be modified in bash 3.0, while it can be in bash 2.05b Darwish Linux - Software 1 11-07-2005 02:57 PM
why did bash 2.05b install delete /bin/bash & "/bin/sh -> bash"? johnpipe Linux - Software 2 06-06-2004 06:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration