Results 1 to 4 of 4

Thread: take data from cvs file to stdout, print column headings every 20 lines

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default take data from cvs file to stdout, print column headings every 20 lines

    Im trying to take data from a cvs file and display column heading names every 20lines or so.

    file.cvs | post.sh

    Code:
    #!/bin/bash
    
    
    while read y; do
            printf "Year\t%s\Eng. Disp.\t%sCyl.\t%sCity FE\t%sHwy FE\t%sModel" #heading
    
    
            awk -F ":" '{printf "%s\t%s\t%s\t%s\t%s\t%s\n", $1, $4, $5, $6, $7, $2}' #data
    done
    want it to look like
    Year Eng. Disp. Cyl. City FE Hwy FE Model
    2012 1.5 6 42 45 BMW
    2012 1.5 6 42 45 BMW
    2012 1.5 6 42 45 BMW
    2012 1.5 6 42 45 BMW
    2012 1.5 6 42 45 BMW
    2012 1.5 6 42 45 BMW
    2012 1.5 6 42 45 BMW
    2012 1.5 6 42 45 BMW
    2012 1.5 6 42 45 BMW
    2012 1.5 6 42 45 BMW
    2012 1.5 6 42 45 BMW
    ...

    Year Eng. Disp. Cyl. City FE Hwy FE Model
    2012 1.5 6 42 45 BMW
    Last edited by jmanv888; 17th March 2013 at 12:36 AM.

  2. #2
    Senior Member
    Join Date
    Aug 2011
    Posts
    448
    Thanks
    0
    Thanked 62 Times in 58 Posts
    Rep Power
    10

    Default

    hi,

    awk can do it alone.
    Code:
    awk -F ":" 'NR==20 
    NR%20==0{print "Year\tEng. Disp.\tCyl.\tCity FE\tHwy FE\tModel"}
    {printf("%s\t%s\t%s\t%s\t%s\t%s\n", $1, $4, $5, $6, $7, $2)}' urFile
    A problem clearly stated is a problem half solved.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    Quote Originally Posted by Watael View Post
    hi,

    awk can do it alone.
    Code:
    awk -F ":" 'NR==20 
    NR%20==0{print "Year\tEng. Disp.\tCyl.\tCity FE\tHwy FE\tModel"}
    {printf("%s\t%s\t%s\t%s\t%s\t%s\n", $1, $4, $5, $6, $7, $2)}' urFile

    Can you also count the number of lines processed? I need to learn more about awk!

    And print it out like There where $x number of records processed?

    Thanks for the help,
    Jason
    Last edited by jmanv888; 17th March 2013 at 02:23 AM.

  4. #4
    Senior Member
    Join Date
    Aug 2011
    Posts
    448
    Thanks
    0
    Thanked 62 Times in 58 Posts
    Rep Power
    10

    Default

    for every line printed, you increment a variable, and print it whenver you want.

    you'll learn awk from its man page, and its user's manual
    A problem clearly stated is a problem half solved.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. print lines that do not match the pattern
    By si203n in forum Shell scripting
    Replies: 3
    Last Post: 6th March 2013, 11:18 PM
  2. [Solved] Awk if else conditional column data retrieval
    By trisno in forum Shell scripting
    Replies: 7
    Last Post: 5th November 2012, 01:35 PM
  3. stdout redirection to a file
    By asim.mcp in forum Linux software
    Replies: 10
    Last Post: 10th November 2009, 10:43 PM
  4. How to print some lines from a file
    By Prahlad in forum Shell scripting
    Replies: 3
    Last Post: 23rd April 2008, 11:25 PM
  5. print data to txt
    By rock in forum Shell scripting
    Replies: 1
    Last Post: 10th March 2006, 02:01 AM

Posting Permissions

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