Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2011
    Posts
    11

    Unanswered: 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 ?

  2. #2
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1
    If you check the manual for awk you'll see that the FILENAME variable is not defined in the BEGIN block.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •