Find the answer to your Linux question:
Results 1 to 5 of 5
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Anyone know Awk scripting

    Im new here and not sure if this is a good forum to seek help with an awk script.

    The other forum I was on Is not as populated as I thought so I figured I would try here. I have here a partial copy of my post.

    On another note I need to try and learn some awk math. I need to figure out what percentage of total logins on the server are done by user1

    var1=`last | grep user1 | wc -l`
    var2=`last | wc -l`
    user1p=`echo $var1 $var2 | awk '{print 100 * ($1 / $2)}'`

    I am getting an error on the first line on the first backtick

    it says invalid char '`' in expression, im fairly sure i should be using backticks there. any suggestions?

    Thank you,


  2. #2
    Linux User
    Join Date
    Jan 2007
    welcome to the forum

    try something like this:

    export a="`last | grep user1 | wc -l` `last | wc -l` "
    echo $a | awk ' { printf ("%3f\n", $1/$2)}'

    the best source for info about awk is "The Awk Programming Language," by Aho, Kernighan and
    Last edited by tpl; 03-31-2013 at 05:40 PM.
    the sun is new every day (heraclitus)

  3. #3
    Im using vim and got these errors doesnt make any sense, when I tried my basic attempt at a script I had some weird errors like these as well when the syntax does look correct.
    Sorry Im still pretty noob at this.

    $ awk -f awkscript22
    awk: awkscript22:2: export a="`last | grep elschemm | wc -l`"
    awk: awkscript22:2: ^ syntax error
    awk: awkscript22:3: echo $a | awk '{printf("%3\n", $1/$2)}'
    awk: awkscript22:3: ^ syntax error
    awk: awkscript22:3: echo $a | awk '{printf("%3\n", $1/$2)}'
    awk: awkscript22:3: ^ invalid char ''' in expression

    when I run it in the command line

    $ export a="`last | grep user1 | wc -l`" echo $a | awk '{printf("&3\n", $1/$2)}'
    -bash: export: `875': not a valid identifier
    Last edited by Kromletch; 04-01-2013 at 09:26 PM.

  4. $spacer_open
  5. #4
    after you say
    u need to say.
    export var1
    same with var2..
    this worked for me.

    echo $var1 $var2| awk '{print 100 * ($1 / $2)}'

  6. #5
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    3rd rock from sun - Often seen near moon
    $ user=`last | grep laks | wc -l`
    $ total=`last | wc -l`
    After retrieving values , you can use -v option to pass bash variable to awk.

    echo | awk -v "U=$user" -v T="$total" '{print 100*U/T}'
    First they ignore you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    FOSS India Award winning ext3fs Undelete tool Online Linux Terminal

Posting Permissions

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