My awk executable is not printing FILENAME...: why?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting My awk executable is not printing FILENAME...: why?
# 1  
Old 06-13-2018
My awk executable is not printing FILENAME...: why?

Hello, there!

I am running an executable awk script with 'source.awk input_file' and found that when I need to print FILENAME, or ENVIRON["AWKPATH"] or even FNR nothing happens [except for an empty line]...
However, if I run it with 'awk -f source.awk input_file', then those variables are printed...
What is the reason for that behaviour?
Thanking you in advance.
# 2  
Old 06-13-2018
What shebang are you using on the first line of source.awk? Are you aware that a .awk extension in itself serves no function?
# 3  
Old 06-13-2018
Yes, Scrutinizer, I am aware that the .awk extension is optional; but it should not prevent awk from printing those variables, I think...
As for the shebang, I am using '#!/usr/local/bin/awk -f'
The source file is on the same directory I use for executable Bash scripts, and the input_file is in another directory.
Only if I call the program with 'awk -f source.awk input_file' do I get those variables printed.
I am not trying to print them from inside the BEGIN bloc; I've put the print command between blocs and on the END bloc, but it does nothing to make it work...
# 4  
Old 06-13-2018
No error messages? Does it execute at all? Is the x permission set correctly?
# 5  
Old 06-13-2018
No, RudiC: no error messages, and the script does execute well for other instructions.
It is only when attempting to print those variables that I notice something is amiss.
The source.awk has the same permissions as the Bash scripts on the same directory: it does execute, but ignores the instruction where it should print those variables:
{ print FNR, FILENAME, ENVIRON["AWKPATH"] }
# 6  
Old 06-13-2018
FILENAME and FNR are not accessible from the BEGIN block, therefore printing them will return empty string/0 respectively.
For some awk-s FNR is accessible in the END block - for some it's not.
Printing these vars from anywhere BUT BEGIN/END, should work.
What is it that you're trying to achieve? Is your awk==gawk or something else?
This User Gave Thanks to vgersh99 For This Post:
# 7  
Old 06-13-2018
Does "awk" run "/usr/local/bin/awk" ?
Test with
Code:
which awk
type awk

and
Code:
/usr/local/bin/awk -f source.awk input_file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

My awk executable is not printing FILENAME...: why?

Hello, there! I am running an executable awk script with 'source.awk input_file' and found that when I need to print FILENAME, or ENVIRON or even FNR nothing happens ... However, if I run it with 'awk -f source.awk input_file', then those variables are printed... What is the reason for that... (1 Reply)
Discussion started by: fbird3
1 Replies

2. Shell Programming and Scripting

Issue when printing filename through cygwin using a variable with awk

Hi, If i were to do this an print out the file, it will show as it is in the command $ awk '/Privilege Use/ {P=0} /Object Access/ {P=1} P' AdvancedAudit.txt Object Access File System No Auditing Registry No Auditing Kernel... (1 Reply)
Discussion started by: alvinoo
1 Replies

3. UNIX for Dummies Questions & Answers

Command - filename as arguments - make executable to all users.

Edit: Sorry. Mistakenly posted - please delete (3 Replies)
Discussion started by: Reddax
3 Replies

4. Shell Programming and Scripting

Make multiple awk files into an executable

Hello everyone, The following are my input files. The following are my sequence of steps. Can someone please let me know about how to make these bunch of steps into a single script so that I start the script with 1.txt and 2.txt, after execution gives me the final... (11 Replies)
Discussion started by: jacobs.smith
11 Replies

5. UNIX for Advanced & Expert Users

fgrep - printing pattern and filename

Hi, I have a patternfile with following pattern cat dog cow pig Let's say I have thousand files file0001 file0002 file0003 . . . file1000 Each pattern can occur multiple times in multiple files. How can I search for pattern so the output of pattern and the filename is printed... (1 Reply)
Discussion started by: yoda9691
1 Replies

6. Shell Programming and Scripting

Help with printing the calling script filename

Consider the following sample scripts..... filename: f1 # Call f3 f3 filename: f2 # Call f3 f3 filename: f3 # f3 echo "$x called me" (2 Replies)
Discussion started by: frozensmilz
2 Replies

7. Shell Programming and Scripting

problem in making Awk executable script

Guys I placed #!path/awk -f placed awk script and used $1 to call 1st inputfile inside the script. But some where I did mistake. Could you please help me run this script as executable I forgot to mention I also used BEGIN before placing awk script. But nothing worked out. Script ... (2 Replies)
Discussion started by: repinementer
2 Replies

8. UNIX for Dummies Questions & Answers

Pattern matching and Printing Filename

Hi, My requirement is to search for a paritcular string from a group of .gz files and to print the lines containing that string and the name of the files in which that string is present. Daily 500 odd .gz files will be generated in a directory(directory name will be in the form of... (4 Replies)
Discussion started by: krao
4 Replies

9. UNIX for Dummies Questions & Answers

Filename Manipulation in AWK

I have created a nawk program and passing a file as an input parameter. The filename will be /home/dir/ksh/test.out I need to extract the filename alone. Is there anyway to get this ? Input : /home/dir/ksh/test.out Output -1: test.out Output -2 : t Input :... (1 Reply)
Discussion started by: sivakumar.rj
1 Replies

10. Shell Programming and Scripting

gzcat into awk and then change FILENAME and process new FILENAME

I am trying to write a script that prompts users for date and time, then process the gzip file into awk. During the ksh part of the script another file is created and needs to be processed with a different set of pattern matches then I need to combine the two in the end. I'm stuck at the part... (6 Replies)
Discussion started by: timj123
6 Replies
Login or Register to Ask a Question