Find the answer to your Linux question:
Results 1 to 2 of 2

Thread: awk command

Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    awk command

    I have a file which contains 5 columns of demographic data with column headings. The table has 5 rows of data all stored in a text file. The second column contains numerical data, however it has imbedded commas; i.e. 123,456,789. I would like to use awk or gawk to output the total of all of the numbers. I know how to set it up for another column which has no commas in the data and get the correct result.

    If I only have a single column with the data in it I was able to get the desired result with this command:

    gawk --posix -F, '/^,/ (tot+=$1 $2 $3); END{print tot}' file.txt

    The computer echo'd each row and then printed the total at the bottom. My challenge is how do I get it to ignore the first column and use the second column in the original file.

    Can anyone help with this problem? Thanks.

    Last edited by larry3639; 03-24-2011 at 08:51 PM.

  2. #2
    Is there any reason why you don't want to do this:
     gawk --posix -F, '/^,/ (tot+=$2 $3); END{print tot}' file.txt
    If you want to completely ignore the first column printing and summing then try this:
    cat file.txt|awk -F, '{print $2","$3}'|awk -F, '(tot+=$1 $2); END{print tot}'

Posting Permissions

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