Find the answer to your Linux question:
Results 1 to 5 of 5
I am trying to count the number of records read into awk but cannot figure out the syntax. My code is below, thank you for the help. Code: #!/bin/bash x=0 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2013
    Posts
    3

    Help counting records with awk (SOLVED!)


    I am trying to count the number of records read into awk but cannot figure out the syntax. My code is below, thank you for the help.

    Code:
    #!/bin/bash
    
    x=0
            while read y; do
    
                    awk -F ":" 'NR==20
                    NR%20==0{print "\nYear\tEng. Disp.\tCyl.\tCity FE\tHwy FE\tModel"}
                    {printf("%s\t%s\t\t%s\t%s\t%s\t%s\n", $1, $4, $5, $6, $7, $2)}'
            done
    echo There were $x records processed.
    Last edited by jmanv888; 03-16-2013 at 11:53 PM.

  2. #2
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    238
    hi,

    I'm there too.

    I've told you that awk is able to work on files alone, no need of a bash while loop.

    in awk, increment a variable, and at the END print the result.

  3. #3
    Just Joined!
    Join Date
    Mar 2013
    Posts
    3
    Quote Originally Posted by watael View Post
    hi,

    I'm there too.

    I've told you that awk is able to work on files alone, no need of a bash while loop.

    in awk, increment a variable, and at the END print the result.

    ha ha ha thanks. I cannot find examples or anything to x++ within awk. I appreciate your help. I guess ill just wait for a coworker who knows awk better to help me out.

    thanks

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    238
    it's quite easy
    Code:
    awk -F ":" 'NR%20==0{print "\nYear\tEng. Disp.\tCyl.\tCity FE\tHwy FE\tModel"}
    {t++; printf("%s\t%s\t\t%s\t%s\t%s\t%s\n", $1, $4, $5, $6, $7, $2)}
    END{ print t" records treated"' urFile

  6. #5
    Just Joined!
    Join Date
    Mar 2013
    Posts
    3
    Quote Originally Posted by watael View Post
    it's quite easy
    Code:
    awk -F ":" 'NR%20==0{print "\nYear\tEng. Disp.\tCyl.\tCity FE\tHwy FE\tModel"}
    {t++; printf("%s\t%s\t\t%s\t%s\t%s\t%s\n", $1, $4, $5, $6, $7, $2)}
    END{ print t" records treated"' urFile
    wateal, I dont care what anyone says, you are awesome! Thank you for all your help. I wish and example like this was online and in the man pages. Thank you. It worked perfectly.

    Jason

Posting Permissions

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