Passing variable as an input file to AWK comand


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing variable as an input file to AWK comand
# 1  
Old 02-22-2012
Passing variable as an input file to AWK comand

Hi,

I would like to compare 2 files using awk, which I can do by using:
Code:
awk 'NR==FNR{a[$1];next} (NR > 32 && $2 in a) {print $0}' File1 and File2.

If the name of the File1 is in another file (for example, column 4 in File 3) then how can I pass this column 4 to the awk command.

Thanks in advance.

Kind regards,
Ezhil

Last edited by Franklin52; 02-23-2012 at 04:26 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 02-23-2012
maybe something like this ?

Code:
mycol=1
myfile=File1
awk -v c="$mycol" 'NR==FNR{a[$(c)];next}(NR>32)&&($2 in a){print $0}' $myfile File2

If you are willing to check col 4 of File3 against col 2 of File2 :

Code:
mycol=4
myfile=File3
awk -v c="$mycol" 'NR==FNR{a[$(c)];next}(NR>32)&&($2 in a){print $0}' $myfile File2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help on passing an input variable to the voronota program (after third pipe)

Dear UNIX forum members, I am using macbook pro 13 (2015 edition) with MAC OS Mojave and am trying to write the shell script where when it is run through terminal it asks for an input (in the code below an input variable is domains) and then that input becomes capital letter or letters which... (3 Replies)
Discussion started by: Aurimas
3 Replies

2. Shell Programming and Scripting

awk with passing variable

I have file called in in.txt contains with the below lines I want to display the lines between the value which I would be passing. one two three four five ten six seven eight Expected output if I have passed one and ten two three four five (8 Replies)
Discussion started by: mychbears
8 Replies

3. UNIX for Advanced & Expert Users

Passing variable as input & storing output in other variable

I have a below syntax its working fine... var12=$(ps -ef | grep apache | awk '{print $2,$4}') Im getting expected output as below: printf "%b\n" "${VAR12}" dell 123 dell 456 dell 457 Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results... (5 Replies)
Discussion started by: sam@sam
5 Replies

4. Shell Programming and Scripting

awk programming -Passing variable to awk for loop

Hi All, I am new to AWK programming. I have the following for loop in my awk program. cat printhtml.awk: BEGIN -------- <some code here> END{ ----------<some code here> for(N=0; N<H; N++) { for(M=5; M<D; M++) print "\t" D ""; } ----- } ... (2 Replies)
Discussion started by: ctrld
2 Replies

5. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

6. Shell Programming and Scripting

Passing a variable as input to another shell

I have a shell program that calls another shell program the following code works . chkTimeFormat.sh "10/9/12 17:51:19:783."|read c but when I am passing the the time in a variable like in the code below, the shell chkTimeFormat.sh is not returning proper value time="10/9/12... (9 Replies)
Discussion started by: swayam123
9 Replies

7. Shell Programming and Scripting

Passing variable to awk

Hi, I'm new with this stuff, but I hope you can help me. This is what I'm trying to do: for id in $var; do awk '{if ($1 == $id) print $2}' merg_data.dat > neigh.tmp done I need that for every "id", awk search the first column of the file merg_data.dat which contains "id" and... (3 Replies)
Discussion started by: matteo86
3 Replies

8. Shell Programming and Scripting

awk built-in variable for input file

Hi guys, Does awk have a built-in variable which I can use to display the input file it's currently reading? I'm currently concatenating multiple files using awk and later on do some parsing. But for now, I want to add an extra column in the main output data file - basically putting in the... (3 Replies)
Discussion started by: Det7
3 Replies

9. UNIX and Linux Applications

Input a variable and write to a file using awk

Hi I am trying to edit a csv file. Bacically I need to input a search variable and the value that must be changed in one of the fields corresponding to that searched variable. My csv file looks like so: 1,1A,5 1,1B,2 1,1C,3 2,2A,7 2,2B,4 2,2C,0 3,3A,1 3,3B,6 3,3C,4 I want to... (4 Replies)
Discussion started by: ladyAnne
4 Replies

10. UNIX for Dummies Questions & Answers

Passing Shell Input to AWK

I am trying to search a log for a particluar pattern listing the total # of occurences in the end. I thought using a shell script for input then calling awk to search for the paramters specified. I want the script to be usable acorss envs. Code: #! /usr/bin/bash # get the variables... (5 Replies)
Discussion started by: wawa44oz
5 Replies
Login or Register to Ask a Question