If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > FILENAME variable not working

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-13-11, 09:54
jason_bourne jason_bourne is offline
Registered User
 
Join Date: Aug 2011
Posts: 11
FILENAME variable not working

I am using FILENAME variable of AWK to display the filename in the out put but its not getting displayed in the output. I am using Ubuntu 11.04.

Here is the code file1.sh

#!/bin/sh
for i in $@ ;
do
if [ -f "$i" ] ; then
awk 'BEGIN { printf "%s\t",FILENAME ; }
/^ *$/ { x+=1 ; }
END { ave=100*(x/NR) ; printf " %s\t%3.1f\n",x,ave; }
' "$i"
else
echo "ERROR: $i not a file." >&2
fi
done

.........Here is the file :

cat gigs.txt
http://www.cusa.berkeley.edu/~ranga
Cisco Systems, Inc
ftp://prep.ai.mit.edu/pub/gnu/
ftp://ftp.redhat.com/
Yahoo!
ranga@kanchi:/home/ranga/pub
ranga@soda:/home/ranga/docs/book/ch01.doc

I did this : ./file1.sh gigs.txt ----- In the out put I am only getting

4 36.4 but not the name of the file i.e. urls.txt however if i do echo $i then i am getting the filename........ Kindly let me know where i am wrong ?
Reply With Quote
  #2 (permalink)  
Old 09-16-11, 11:47
n_i n_i is online now
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,453
If you check the manual for awk you'll see that the FILENAME variable is not defined in the BEGIN block.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On