Find the answer to your Linux question:
Results 1 to 5 of 5
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2013
    Posts
    2

    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,

    Kromletch

  2. #2
    tpl
    tpl is offline
    Linux User
    Join Date
    Jan 2007
    Location
    cleveland
    Posts
    478
    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
    Weinberger
    Last edited by tpl; 03-31-2013 at 06:40 PM.
    the sun is new every day (heraclitus)

  3. #3
    Just Joined!
    Join Date
    Mar 2013
    Posts
    2
    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 10:26 PM.

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Apr 2013
    Posts
    7
    after you say
    var1=`comand`
    u need to say.
    export var1
    same with var2..
    this worked for me.

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

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

    Code:
    echo | awk -v "U=$user" -v T="$total" '{print 100*U/T}'
    HTH
    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 www.giis.co.in. Online Linux Terminal http://www.webminal.org

Posting Permissions

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