Find the answer to your Linux question:
Results 1 to 6 of 6
Ok, I'm trying to use awk to get the file size of a file. I am calling awk from a bash script. The hard part is that the file name ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2004
    Location
    Hamilton, Canada
    Posts
    13

    AWK troubles


    Ok, I'm trying to use awk to get the file size of a file. I am calling awk from a bash script. The hard part is that the file name is stored in a variable in the bash script. I also want the file size to be stored in a variable.

    I originally tried this:

    ls -la | awk '{ if ( $9 == $filename ) filesize=$5 }'

    but that didn't work. So I tried:

    ls -la | awk '$9 == $filename { filesize=$5 }'

    but still nothing. Apparently the condition isn't met. Also I don't think awk likes variables other than the $1, $2 etc...

    So I tried this:

    ls -la | grep $filename | awk 'END { filesize=$5 }'

    Still not working. Finally:

    ls -la | grep $filename | awk 'END { print $5 }'

    which DOES print the correct answer. So, how do I get that printed value into a variable in my bash script?

  2. #2
    Just Joined!
    Join Date
    May 2004
    Location
    Hamilton, Canada
    Posts
    13
    Well I guess I posted too soon, I just found an answer.

    Basically you take this command:

    ls -la | grep $filename | awk 'END { print $5 }'

    and wrap it in back ticks and put filesize= in front of it to give:

    filesize=`ls -la | grep $filename | awk 'END { print $5 }'`

  3. #3
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    Location
    St. Petersburg, FL
    Posts
    5,035
    Code:
    foo="$(ls -la | grep $filename | awk 'END { print $5 }' )"

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Engineer
    Join Date
    Jan 2005
    Location
    Chicago (USA)
    Posts
    1,028
    Why do you need awk? ls -lh FILE|cut -d" " -f6 works fine.

  6. #5
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by a thing
    Why do you need awk? ls -lh FILE|cut -d" " -f6 works fine.
    He may want to total the file sizes in the directory.

    If you learn to use quotes properly you'll get your awk script to work:
    Code:
    ls -la | awk '{ if ( $9 == '"$filename"' ) filesize=$5 }'
    With awk you need single quotes to stop the shell from seeing the numbered field variables, but double quotes around the shell variable so the shell can interpolate it. Your pipeline isn't the best way to do this (subject to precisely what you're trying to achieve), but it illustrates the quoting issue for you.

    Steve

  7. #6
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    Lots of ways to do the same thing in *nix.

    Code:
    FILESIZE1=`du -k "$FILENAME1" | cut -f 1`
    should get you the same results, if I'm understanding what you want.

    In this case you don't have to list and iterate through the directory contents.

    If you insist on using awk for this, then there have already been some good suggestions.

Posting Permissions

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