Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
I need to migrate some scripts running on SunOS to Linux, script running on SunOs used to get the filename that was passed as a paramenter to the .awk script ...
  1. #1
    Just Joined!
    Join Date
    Dec 2007
    Posts
    11

    How to get the Filename in .awk running on Linux

    I need to migrate some scripts running on SunOS to Linux,
    script running on SunOs used to get the filename that was passed as a paramenter to the .awk script as

    outFile=substr(FILENAME,1,3)

    the problem is variable FILENAME does not work on Linux, is ther any other alternative?

    thanks in advance for the help.

  2. #2
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    $1 is first parameter passed to a shell script

  3. #3
    Just Joined!
    Join Date
    Dec 2007
    Posts
    11
    its an awk script not shell, the command is
    awk -f compareSum.awk prod.dat test.dat

    and I need to get prod.dat into some variable.

  4. #4
    drl
    drl is online now
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    It works for me in this situation:
    Code:
    #!/usr/bin/env sh
    
    # @(#) a1       Demonstrate variable FILENAME in (g)awk.
    
    set -o nounset
    echo
    
    ## Use local command version for the commands in this demonstration.
    
    echo "(Versions displayed with local utility \"version\")"
    version >/dev/null 2>&1 && version bash awk
    
    echo
    
    # Use nawk or /usr/xpg4/bin/awk on Solaris.
    
    echo " Hello, world." > t1
    awk '
            { print
              print " Currently reading FILENAME", FILENAME }
    ' t1
    
    exit 0
    Producing:
    Code:
    % ./a1
    
    (Versions displayed with local utility "version")
    GNU bash 2.05b.0
    GNU Awk 3.1.4
    
     Hello, world.
     Currently reading FILENAME t1
    The man page may be of value ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  5. #5
    Just Joined!
    Join Date
    Dec 2007
    Posts
    11
    is this working on Linux?, variable FILENAME is working for me on SunOS but not Linux.

  6. #6
    Just Joined!
    Join Date
    Nov 2004
    Posts
    18

    Smile

    Well, here is one example how to do it in your compareSum.awk:
    Code:
    #!/usr/bin/awk -f
    
    BEGIN {}
    {
        print FILENAME;
    }
    As you can see, FILENAME works just fine

  7. #7
    Just Joined!
    Join Date
    Dec 2007
    Posts
    11
    doesn't work on Linux, works on Solaris.

  8. #8
    Just Joined!
    Join Date
    Nov 2004
    Posts
    18

    Smile

    And I thought I was running Linux on all my machines...

    Execute this on your computer and post here the result:
    Code:
    uname -a
    awk --version | head -1
    What Linux distribution do you use?

  9. #9
    Just Joined!
    Join Date
    Dec 2007
    Posts
    11
    uname -aLinux enycxapramaps017 2.6.9-22.0.2.ELsmp #1 SMP Thu Jan 5 17:11:56 EST 2006 x86_64 x86_64 x86_64 GNU/Linux


    awk --version | head -1
    GNU Awk 3.1.3

  10. #10
    Just Joined!
    Join Date
    Nov 2004
    Posts
    18
    It has to work on your system. Try this one (although it's not preferable):
    Code:
    #!/usr/bin/awk -f
    
    BEGIN {}
    {
        print ARGV[1];
    }

Page 1 of 2 1 2 LastLast

Posting Permissions

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