How to Pass filename to AWK in bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to Pass filename to AWK in bash script
# 1  
Old 08-09-2012
How to Pass filename to AWK in bash script (SOLVED)

I have written a script which works fine, to remove patterns contained in EXCLUDE.DAT from input.txt

Code:
awk 'BEGIN {n=0;while (getline < "EXCLUDE.DAT" > 0){ex[n]=$0;n++}}  {for(var in ex){print  var "-" ex[var] $0 ;i++}}' input.txt


The last problem I need to solve is how to pass the file EXCLUDE.DAT to the script from the shell so that EXCLUDE.DAT can be any file. Obviously

Code:
excludefile=somefile.dat
awk 'BEGIN {n=0;while (getline < "$excludefile" > 0){ex[n]=$0;n++}}  {for(var in ex){print  var "-" ex[var] $0 ;i++}}' input.txt

won't work because of the single quotes.

I also tried


Code:
excludefile=somefile.dat
awk -v EXF=${exludefile}  'BEGIN {n=0;while (getline < EXF > 0){ex[n]=$0;n++}}  {for(var in ex){print  var "-" ex[var] $0 ;i++}}' input.txt

which gives the error

awk: fatal: expression for `<' redirection has null string value

Any help or hints how to solve this would be much appreciated.

Last edited by nixie; 08-12-2012 at 11:44 AM.. Reason: Edited to mark (SOLVED)
# 2  
Old 08-09-2012
You have missed one letter in the variable name:
Code:
excludefile=somefile.dat
awk -v EXF=${excludefile}  'BEGIN {n=0;while (getline < EXF > 0){ex[n]=$0;n++}}  {for(var in ex){print  var "-" ex[var] $0 ;i++}}' input.txt

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 08-09-2012
Try using FILENAME with a little restructuring:
Code:
excludefile=somefile.dat
infile=anotherfile.dat

awk v- in="$infile" -v exf="$excludefile" '
       FILENAME==exf 
         {n=0;while (getline < "$excludefile" > 0){ex[n]=$0;n++}}  
       FILENAME==in  
       {for(var in ex)
       {print  var "-" ex[var] $0 ;i++}}' $excludefile $infile

If you are totaly into one-liners you can one-line this if you want.
This User Gave Thanks to jim mcnamara For This Post:
# 4  
Old 08-10-2012
How to Pass filename to AWK in bash script (Solved)

Thanks bartus11... you got it! Can't believe I missed that... wasted about an hour messing with this code before posting here.

Thanks also to jim mcnamara for the reply... didn't have to bother restructuring because the problem was just a stupid typo.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Pass config file to bash script

I just want to make sure I am understanding how to pass a config file to a bash script . In the below I pass to arguments to a script, then define them in the script as id and config. I then source config using ., if I understand correctly the variables in the config file can now be used by the... (11 Replies)
Discussion started by: cmccabe
11 Replies

2. UNIX for Beginners Questions & Answers

Pass RegEx to java program in bash script

I can't seem to get this right. I've tried it every way imaginable using every trick I see on stackexchange and such. No luck. So nothing major here, something like: #!/bin/bash SEARCH="ARG1 ARG2 '((^EXACT$)|(.*InTheMiddle*)|(^AtBeginning*))'" java -cp /my/class/path MyClassName $SEARCH... (3 Replies)
Discussion started by: stonkers
3 Replies

3. Shell Programming and Scripting

Curl , download file with user:pass in bash script

Hello, My question is about curl command. (ubuntu14.04) In terminal, I am able to download my mainfile with: curl -u user1:pass1 http://11.22.33.44/******* When I convert it into bash script like this: #!/bin/bash cd /root/scripts computer_ip=11.22.33.44 curl -u $1:$2... (8 Replies)
Discussion started by: baris35
8 Replies

4. Shell Programming and Scripting

Replacing filename with sed in bash script

I need to cat two files with similar names. I am using the following script: #!/bin/bash if ] then file=$1 file2="${file%R1.fastq}R2.fastq" echo fetching data from R2 file ... sleep 3 cat $file $file2 > infile else echo "Input_file passed... (2 Replies)
Discussion started by: Xterra
2 Replies

5. Shell Programming and Scripting

How to pass enter key or selected character in bash script?

hi, i've bash script thats working... but now i need to add a line....that prompts for user input....like yes and 1 as complete install.... so here's how it looks... $ cd 9200 (cd into directory) $./install (hv to type ./install to run install then ask for) ----do you want to... (4 Replies)
Discussion started by: kernel11
4 Replies

6. Shell Programming and Scripting

Pass arguments to bash script

myscript.sh #!/bin/bash ARGA=$1 if ; then echo "${ARGA}:Confirmed" else echo "${ARGA}:Unconfirmed" fi when I run the above script from the command line, i run it as: ./myscript.sh jsmith now some times, i need to runn it this way: (8 Replies)
Discussion started by: SkySmart
8 Replies

7. Shell Programming and Scripting

How to pass filename as arguement to awk command?

Hi, I am facing one issue. The awk command works fine if i hardcode the file name but if is pass it as an arguement it doesn't work. For e.g:Below commands works fine awk -v A="$type" '{F=substr($0,23,8) "_LTD_" A ".txt"; print $0 >> F; close(F) }' RL004.txt But the below command does not... (2 Replies)
Discussion started by: Neelkanth
2 Replies

8. Shell Programming and Scripting

Help parsing filename with bash script

Hi all! Looking for some help parsing filenames in bash. I have a directory full of files named "livingroom-110111105637.avi". The format is always date and time (yymmddhhmmss). I'm looking to parse the filenames so they are a little more easily readable. Maybe rename them to... (4 Replies)
Discussion started by: mtehonica
4 Replies

9. Shell Programming and Scripting

How to pass value from plsql script to unix bash?

Hi This is my bash script.i am calling validation.sql and passing a value to it using ${flds}. i want the cnt variable in plsql script to be passed to unix. LOADREC=`sqlplus -s $ORACLE_USR <<-EOF spool $ORACLE_LOG_FILE; echo "barani" @validation.sql #calling the plsql script ${flds}... (6 Replies)
Discussion started by: barani75
6 Replies

10. Shell Programming and Scripting

Bash script pass sentence in block

Hello, I want to know is it possible to pass a block of sentence using bash. For example, I have a script called Test.sh that takes in $1 and $2. and I'm calling Test.sh in a.sh so in a.sh Test.sh '' 'This is a sentence' Because block are separated by space so when I do that, I get... (6 Replies)
Discussion started by: katrvu
6 Replies
Login or Register to Ask a Question