Awk Introduction Tutorial – 7 Awk Print Examples

download Awk Introduction Tutorial – 7 Awk Print Examples

of 10

Transcript of Awk Introduction Tutorial – 7 Awk Print Examples

  • 8/3/2019 Awk Introduction Tutorial 7 Awk Print Examples

    1/10

    9 Tweet 6 15Like

    AwkIntroducbyS A S I K A L AonJ A N U A R Y 6 , 2 0 1 0

    8

    This is the first article on the new awk tutorial

    series. Well be posting several articles on awk

    in the upcoming weeks that will cover all

    features of awk with practical examples.

    In this article, let us review the fundamental

    awk working methodology along with 7

    practical awk print examples.

    Note: Make sure you review our earlier Sed Tutorial Series.

    AwkIntr

    Awk is a programming language which allows easy manipulation of structured data and the

    generation of formatted reports. Awk stands for the names of its authors Aho, Weinberger, and

    Kernighan

    The Awk is mostly used for pattern scanning and processing. It searches one or more files to see if

    they contain lines that matches with the specified patterns and then perform assoc iated actions.

    Some of the key features of Awk are:

    Awk views a text file as records and fields.

    Like common programming language, Awk has variables, conditionals and loops

    Awk has arithmetic and string operators.

    Awk can generate formatted reports

    Awk reads from a file or from its standard input, and outputs to its standard output. Awk does not

    get along with non-text files.

    Syntax:

    Sign up for our free email newsletter

    [email protected] Sign Up

    RSS Twitter Facebook

    Search

    E B O O K S

    Ads by Google Linux Command Line Linux Tutorial Unix Shell Script

    H o m eA b o u tF r e ee B o o k

    A r c h i v e sB e s t o f t h eB l o g

    C o n t a c t

    converted by Web2PDFConvert.com

    http://www.thegeekstuff.com/http://www.web2pdfconvert.com/?ref=PDFhttp://www.web2pdfconvert.com/?ref=PDFhttp://www.web2pdfconvert.com/?ref=PDFhttp://www.thegeekstuff.com/vim-101-hacks-ebook/http://www.thegeekstuff.com/sed-awk-101-hacks-ebook/http://www.thegeekstuff.com/bash-101-hacks-ebook/http://www.thegeekstuff.com/linux-101-hacks-ebook/http://ad.doubleclick.net/click%3Bh%3Dv8/3be6/3/0/%2a/s%3B246407184%3B3-0%3B0%3B71041187%3B4307-300/250%3B45679955/45697363/1%3B%3B~sscs%3D%3fhttp://www.ibm.com/systems/info/x86servers/cloud/index.html?cmp=usbrb&cm=b&csr=agus_itintelcloud-20111212&cr=idg_technetwork&ct=usbrb301&cn=intelcloudfoundhttp://www.facebook.com/thegeekstuffhttp://twitter.com/thegeekstuffhttp://www.thegeekstuff.com/feedhttp://www.thegeekstuff.com/feedhttp://www.thegeekstuff.com/tag/sed-tips-and-tricks/http://www.thegeekstuff.com/contact/http://www.thegeekstuff.com/best-of-the-blog/http://www.thegeekstuff.com/archives-2/http://www.thegeekstuff.com/linux-101-hacks-ebook/http://www.thegeekstuff.com/about/http://www.thegeekstuff.com/http://www.thegeekstuff.com/
  • 8/3/2019 Awk Introduction Tutorial 7 Awk Print Examples

    2/10

    awk '/search pattern1/ {Actions}

    /search pattern2/ {Actions}' file

    In the above awk syntax:

    search pattern is a regular expression.

    Actions statement(s) to be performed.

    several patterns and actions are possible in Awk.

    file Input file.

    Single quotes around program is to avoid shell not to interpret any of its special characters.

    AwkWorkinge

    1. Awk reads the input files one line at a time.

    2. For each line, it matches with given pattern in the given order, if matches performs the

    corresponding action.

    3. If no pattern matches, no action will be performed.

    4. In the above syntax, either search pattern or action are optional, But not both.

    5. If the search pattern is not given, then Awk performs the given actions for each line of the

    input.

    6. If the act ion is not given, print all that lines that matches with the given patterns which is the

    default action.

    7. Empty braces with out any act ion does nothing. It wont perform default printing operation.

    8. Each statement in Actions should be delimited by semicolon.

    Let us create employee.txt file which has the following content, which will be used in the

    examples mentioned below.

    $cat employee.txt

    100 Thomas Manager Sales $5,000

    200 Jason Developer Technology $5,500

    300 Sanjay Sysadmin Technology $7,000

    400 Nisha Manager Marketing $9,500

    500 Randy DBA Technology $6,000

    Awk Example 1. Default behavior of Awk

    By default Awk prints every line from the file.

    $ awk '{print;}' employee.txt

    100 Thomas Manager Sales $5,000

    200 Jason Developer Technology $5,500

    300 Sanjay Sysadmin Technology $7,000

    400 Nisha Manager Marketing $9,500

    500 Randy DBA Technology $6,000

    In the above example pattern is not given. So the actions are applicable to all the lines.

    Action print with out any argument prints the whole line by default. So it prints all the

    lines of the file with out fail. Actions has to be enclosed with in the braces.

    Awk Example 2. Print the lines which matches with the pattern.

    $ awk '/Thomas/

    > /Nisha/' employee.txt

    100 Thomas Manager Sales $5,000

    400 Nisha Manager Marketing $9,500

    In the above example it prints all the line which matches with the Thomas or Nisha. It has two

    patterns. Awk accepts any number of patterns, but each set (patterns and its corresponding

    actions) has to be separated by newline.

    Awk Exam le 3. Print onl s ecific field.

    P O P U L A R P O S

    12 Amazing and Essential Linux Books To Enrich Your

    Brain and Library

    50 UNIX / Linux Sysadmin Tutorials

    50 Most Frequently Used UNIX / Linux Commands (With

    Examples)

    How To Be Productive and Get Things Done Using GTD

    30 Things To Do When you are Bored and have a

    Computer

    Linux Directory Structure (File System Structure)

    Explained with Examples

    Linux Crontab: 15 Awesome Cron Job Examples

    Get a Grip on the Grep! 15 Practical Grep Command

    Examples

    Unix LS Command: 15 Practical Examples

    15 Examples To Master Linux Command Line History

    Top 10 Open Source Bug Tracking System

    Vi and Vim Macro Tutorial: How To Record and Play

    Mommy, I found it! -- 15 Practical Linux Find Command

    Examples

    15 Awesome Gmail Tips and Tricks

    15 Awesome Google Search Tips and Tricks

    RAID 0, RAID 1, RAID 5, RAID 10 Explained with

    Diagrams

    Can You Top This? 15 Practical Linux Top Command

    Examples

    Top 5 Best System Monitoring Tools

    Top 5 Best Linux OS Distributions

    How To Monitor Remote Linux Host using Nagios 3.0

    Awk Introduction Tutorial 7 Awk Print Examples

    How to Backup Linux? 15 rsync Command Examples

    The Ultimate Wget Download Guide With 15 Awesome

    Examples

    Top 5 Best Linux Text Editors

    Packet Analyzer: 15 TCPDUMP Command Examples

    The Ultimate Bash Array Tutorial with 15 Examples

    converted by Web2PDFConvert.com

    http://www.web2pdfconvert.com/?ref=PDFhttp://www.web2pdfconvert.com/?ref=PDFhttp://www.thegeekstuff.com/2010/06/bash-array-tutorial/http://www.thegeekstuff.com/2010/08/tcpdump-command-examples/http://www.thegeekstuff.com/2009/07/top-5-best-linux-text-editors/http://www.thegeekstuff.com/2009/09/the-ultimate-wget-download-guide-with-15-awesome-examples/http://www.thegeekstuff.com/2010/09/rsync-command-examples/http://www.thegeekstuff.com/2008/06/how-to-monitor-remote-linux-host-using-nagios-30/http://www.thegeekstuff.com/2009/06/top-5-best-linux-os-distributions/http://www.thegeekstuff.com/2009/09/top-5-best-network-monitoring-tools/http://www.thegeekstuff.com/2010/01/15-practical-unix-linux-top-command-examples/http://www.thegeekstuff.com/2010/08/raid-levels-tutorial/http://www.thegeekstuff.com/2009/06/expertise-in-google-search/http://www.thegeekstuff.com/2009/07/best-gmail-tips-and-tricks/http://www.thegeekstuff.com/2009/03/15-practical-linux-find-command-examples/http://www.thegeekstuff.com/2009/01/vi-and-vim-macro-tutorial-how-to-record-and-play/http://www.thegeekstuff.com/2010/08/bug-tracking-system/http://www.thegeekstuff.com/2008/08/15-examples-to-master-linux-command-line-history/http://www.thegeekstuff.com/2009/07/linux-ls-command-examples/http://www.thegeekstuff.com/2009/03/15-practical-unix-grep-command-examples/http://www.thegeekstuff.com/2009/06/15-practical-crontab-examples/http://www.thegeekstuff.com/2010/09/linux-file-system-structure/http://www.thegeekstuff.com/2010/03/30-things-to-do-when-you-are-bored-and-have-a-computer/http://www.thegeekstuff.com/2010/08/gtd-getting-things-done/http://www.thegeekstuff.com/2010/11/50-linux-commands/http://www.thegeekstuff.com/2010/12/50-unix-linux-sysadmin-tutorials/http://www.thegeekstuff.com/2009/01/12-amazing-and-essential-linux-books-to-enrich-your-brain-and-library/http://ad.doubleclick.net/click%3Bh%3Dv8/3be6/3/0/%2a/l%3B250202930%3B1-0%3B0%3B38965644%3B4307-300/250%3B45222627/45240410/1%3Bu%3D%2Cidgt-10313086953_1324612777%2C1248cd4c1400c8c%2Ccareers%2Cax.170%3B~aopt%3D2/1/bc/0%3B~sscs%3D%3fhttp://idgtechnetwork.com/design/dev/intel/nanosite_lite.htmlhttp://www.thegeekstuff.com/nagios-core-ebook/
  • 8/3/2019 Awk Introduction Tutorial 7 Awk Print Examples

    3/10

    Awk has number of built in variables. For each record i.e line, it splits the record delimited by

    whitespace character by default and stores it in the $n variables. If the line has 4 words, it will be

    stored in $1, $2, $3 and $4. $0 represents whole line. NF is a built in variable which represents total

    number of fields in a record.

    $ awk '{print $2,$5;}' employee.txt

    Thomas $5,000

    Jason $5,500

    Sanjay $7,000

    Nisha $9,500

    Randy $6,000

    $ awk '{print $2,$NF;}' employee.txt

    Thomas $5,000

    Jason $5,500

    Sanjay $7,000

    Nisha $9,500

    Randy $6,000

    In the above example $2 and $5 represents Name and Salary respectively. We can get the Salary

    using $NF also, where $NF represents last field. In the print statement , is a concatenator.

    Awk Example 4. Initialization and Final Action

    Awk has two important patterns which are specified by the keyword called BEGIN and END.

    Syntax:

    BEGIN { Actions}

    {ACTION} # Action for everyline in a file

    END { Actions }

    # is for comments in Awk

    Actions specified in the BEGIN section will be executed before starts reading the lines from the

    input.

    END actions will be performed after completing the reading and processing the lines from the

    input.

    $ awk 'BEGIN {print "Name\tDesignation\tDepartment\tSalary";}

    > {print $2,"\t",$3,"\t",$4,"\t",$NF;}

    > END{print "Report Generated\n--------------";

    > }' employee.txt

    Name Designation Department Salary

    Thomas Manager Sales $5,000

    Jason Developer Technology $5,500

    Sanjay Sysadmin Technology $7,000

    Nisha Manager Marketing $9,500

    Randy DBA Technology $6,000

    Report Generated

    --------------

    In the above example, it prints headline and last file for the reports.

    Awk Example 5. Find the employees who has employee id greater than 200

    $ awk '$1 >200' employee.txt

    300 Sanjay Sysadmin Technology $7,000

    400 Nisha Manager Marketing $9,500

    500 Randy DBA Technology $6,000

    In the above example, first field ($1) is employee id. So if $1 is greater than 200, then just do the

    default print action to print the whole line.

    3 Steps to Perform SSH Login Without Password Using

    ssh-keygen & ssh-copy-id

    Unix Sed Tutorial: Advanced Sed Substitution Examples

    UNIX / Linux: 10 Netstat Command Examples

    The Ultimate Guide for Creating Strong Passwords

    6 Steps to Secure Your Home Wireless Network

    Turbocharge PuTTY with 12 Powerful Add-Ons

    converted by Web2PDFConvert.com

    http://www.web2pdfconvert.com/?ref=PDFhttp://www.web2pdfconvert.com/?ref=PDFhttp://www.thegeekstuff.com/2008/08/turbocharge-putty-with-12-powerful-add-ons-software-for-geeks-3/http://www.thegeekstuff.com/2008/08/6-steps-to-secure-your-home-wireless-network/http://www.thegeekstuff.com/2008/06/the-ultimate-guide-for-creating-strong-passwords/http://www.thegeekstuff.com/2010/03/netstat-command-examples/http://www.thegeekstuff.com/2009/10/unix-sed-tutorial-advanced-sed-substitution-examples/http://www.thegeekstuff.com/2008/11/3-steps-to-perform-ssh-login-without-password-using-ssh-keygen-ssh-copy-id/
  • 8/3/2019 Awk Introduction Tutorial 7 Awk Print Examples

    4/10

    Awk Example 6. Print the list of employees in Technology department

    Now department name is available as a fourth field, so need to check if $4 matches with the string

    Technology, if yes print the line.

    $ awk '$4 ~/Technology/' employee.txt

    200 Jason Developer Technology $5,500

    300 Sanjay Sysadmin Technology $7,000

    500 Randy DBA Technology $6,000

    Operator ~ is for comparing with the regular expressions. If it matches the default action i.e print

    whole line will be performed.

    Awk Example 7. Print number of employees in Technology department

    The below example, checks if the department is Technology, if it is yes, in the Action, just

    increment the count variable, which was initialized with zero in the BEGIN section.

    $ awk 'BEGIN { count=0;}

    $4 ~ /Technology/ { count++; }

    END { print "Number of employees in Technology Dept =",count;}' employee.txt

    Number of employees in Tehcnology Dept = 3

    Then at the end of the process, just print the value of count which gives you the number of

    employees in Technology department.

    Recommended Reading

    Sed and Awk 101 Hacks, by Ramesh Natarajan. I spend several

    hours a day on UNIX / Linux environment dealing with text files (data,

    config, and log files). I use Sed and Awk for all my my text

    manipulation work. Based on my Sed and Awk experience, Ive written

    Sed and Awk 101 Hacks eBook that contains 101 practical examples

    on various advanced features of Sed and Awk that will enhance your

    UNIX / Linux life. Even if youve been using Sed and Awk for several

    years and have not read this book, please do yourself a favor and

    read this book. Youll be amazed with the capabilities of Sed and Awk

    utilities.

    Additional Awk Articles

    Awk User-defined Variables with 3 Practical Examples

    8 Powerful Awk Built-in Variables FS, OFS, RS, ORS, NR, NF, FILENAME, FNR

    7 Powerful Awk Operators Examples (Unary, Binary, Arithmetic, String, Assignment,

    Conditional, Reg-Ex Awk Operators)

    4 Awk If Statement Examples ( if, if else, if else if, : ? )

    Caught In the Loop? Awk While, Do W hile, For Loop, Break, Continue, Exit Examples

    converted by Web2PDFConvert.com

    http://www.web2pdfconvert.com/?ref=PDFhttp://www.web2pdfconvert.com/?ref=PDFhttp://www.thegeekstuff.com/2010/02/unix-awk-do-while-for-loops/http://www.thegeekstuff.com/2010/02/awk-conditional-statements/http://www.thegeekstuff.com/2010/02/unix-awk-operators/http://www.thegeekstuff.com/2010/01/8-powerful-awk-built-in-variables-fs-ofs-rs-ors-nr-nf-filename-fnr/http://www.thegeekstuff.com/2010/01/awk-tutorial-understand-awk-variables-with-3-practical-examples/http://www.thegeekstuff.com/sed-awk-101-hacks-ebook/
  • 8/3/2019 Awk Introduction Tutorial 7 Awk Print Examples

    5/10

    9 Tweet 6 Share15Like

    1

    2

    3

    Comment

    If you enjoyed this article, you might also like..

    1. 50 Linux Sysadmin Tutorials

    2. 50 Most Frequently Used Linux Commands

    (With Examples)

    3. Top 25 Best Linux Performance Monitoring and

    Debugging Tools4. Mommy, I found it! 15 Practical Linux Find

    Command Examples

    5. Linux 101 Hacks 2nd Edition eBook

    Awk Introduction 7 Awk Print Examples

    Advanced Sed Substitution Examples

    8 Essential Vim Editor Navigation

    Fundamentals

    25 Most Frequently Used Linux IPTablesRules Examples

    Turbocharge PuTTY with 12 Powerful

    Add-Ons

    Tags:Awk AC TION Block,Awk BEGIN Block,Awk Begin End,Awk END Block,Awk Tutorial Examples, Linux Awk Examples, Linux

    Awk Tutorial, Unix Awk Examples, Unix Awk Tutorial

    {27 comments read them below or add one}

    Steve Mills January 6, 2010 at 3:38 am

    I have only just started reading these articles. So far I think they are well written and the

    explanations are clearly done with an awareness as to how they might possibly be

    misunderstood and hence extra layers of detail are presented where that might happen. For

    example, pointing out that the tilde (~) is used to compare with regular expressions when the

    reader might have otherwise expected an equals sign without the explanation the reader

    might have decided that the tilde represented the same thing as an equals sign.

    I shall be reading more.

    Thanks for posting these articles.

    Kind Regards

    Steve

    Daniel Reimann January 6, 2010 at 6:02 am

    Thank you for the post here on awk. I use it frequently, but it is always good to have some

    updates and reminders. Happy New Year.

    Lawrence January 7, 2010 at 4:34 am

    awk is awesome! thanks for your sharing.

    Best Regards,

    Lawrence

    converted by Web2PDFConvert.com

    http://www.web2pdfconvert.com/?ref=PDFhttp://www.web2pdfconvert.com/?ref=PDFhttp://go-linux.blogspot.com/http://www.danielreimann.us/http://www.sandcmills.co.uk/http://www.thegeekstuff.com/tag/unix-awk-tutorial/http://www.thegeekstuff.com/tag/unix-awk-examples/http://www.thegeekstuff.com/tag/linux-awk-tutorial/http://www.thegeekstuff.com/tag/linux-awk-examples/http://www.thegeekstuff.com/tag/awk-tutorial-examples/http://www.thegeekstuff.com/tag/awk-end-block/http://www.thegeekstuff.com/tag/awk-begin-end/http://www.thegeekstuff.com/tag/awk-begin-block/http://www.thegeekstuff.com/tag/awk-action-block/http://www.thegeekstuff.com/vim-101-hacks-ebook/http://www.thegeekstuff.com/nagios-core-ebook/http://www.thegeekstuff.com/sed-awk-101-hacks-ebook/http://www.thegeekstuff.com/bash-101-hacks-ebook/http://www.thegeekstuff.com/2008/08/turbocharge-putty-with-12-powerful-add-ons-software-for-geeks-3/http://www.thegeekstuff.com/2011/06/iptables-rules-examples/http://www.thegeekstuff.com/2009/03/8-essential-vim-editor-navigation-fundamentals/http://www.thegeekstuff.com/2009/10/unix-sed-tutorial-advanced-sed-substitution-examples/http://www.thegeekstuff.com/linux-101-hacks-ebook/http://www.thegeekstuff.com/2009/03/15-practical-linux-find-command-examples/http://www.thegeekstuff.com/2011/12/linux-performance-monitoring-tools/http://www.thegeekstuff.com/2010/11/50-linux-commands/http://www.thegeekstuff.com/2010/12/50-unix-linux-sysadmin-tutorials/http://www.addthis.com/bookmark.php?v=250&pub=xa-4ac6c0d77fc11b75
  • 8/3/2019 Awk Introduction Tutorial 7 Awk Print Examples

    6/10

    4

    5

    6

    7

    8

    9

    10

    Knusper January 9, 2010 at 5:15 pm

    Hi Good article now I know what ark is, and what I could use it for well written. I follow

    you now on twitter!

    Harsh January 10, 2010 at 10:08 pm

    Thanks for posting a tutorial on awk with illustrated examples.

    I Will be expecting other articles on awk

    Ramesh Natarajan January 14, 2010 at 9:23 pm

    @Steve,

    Yeah. ~ can be little confusing in this context, if not explained properly. Thanks for you

    comment.

    @Daniel,

    Yeah. Most other readers of the blog are in similar position like you. So, we are here to give

    constant updated and remainders of the functionality that they already know.

    @Lawrence, Harsh,

    Thanks for the v ery nice comment. Im glad you liked this article.

    @Knusper,

    Thanks for following us on twitter.

    thalafan March 21, 201 0 at 10:24 am

    Nandraka Ulladhu!!!

    I guess the example 2 can be done without a new line like below? Pattern as regex.

    ~/bin$ awk /Jason|Randy/ employee

    200 Jason Developer Technology $5,500

    500 Randy DBA Technology $6,000

    And also what does the ; stands for? End of Pattern?

    Andreia AmaralApril 7, 2010 at 5:14 am

    Hi,

    I want to use an if else statement like this:

    if $10>10 print $0 > filename1

    else print $0> filename2

    but its not working it is not creating filename1 or filename2, how can I do this?

    thanks?

    Ashu Agrawal August 6, 2010 at 10 :31 am

    Grt post.Thanks for making me understand the awk working

    avinash October 1, 2010 at 7:30 am

    converted by Web2PDFConvert.com

    http://www.web2pdfconvert.com/?ref=PDFhttp://www.web2pdfconvert.com/?ref=PDFhttp://www.twitter.com/thegeekstuffhttp://twitter.com/knusper2000
  • 8/3/2019 Awk Introduction Tutorial 7 Awk Print Examples

    7/10

    11

    12

    13

    14

    15

    16

    17

    hi, this is avinash.

    suppose u have a emp table like this:

    id name designation salary

    1 avi manager 1000

    2 ash manager 1500

    3 nash manager 2000

    4 varma trainee 500

    5 chow trainee 600

    6 hemanth trainee 800

    using awk command, i hav to print manager total salary and trainee total salary.

    i need a program.. can any one plz post it

    vikas October 13, 2010 at 1:34 am

    Hi..@Avinash..

    u can try this one.

    awk BEGIN {man_sal=0;trainee_sal=0;}

    $3 ~/manager/ {man_sal+=$NF}

    /trainee/ {trainee_sal+=$NF}

    END {print Total salary of managers and trainees are=man_sal,trainee_sal} in_file.name

    siva October 15, 2010 at 1:44 am

    Hello forum members,

    Thanks for AWK tutorials ,it was very help ful to me.

    avinash October 19, 2010 at 5:12 am

    @ v ikas

    thanks you

    wish October 21, 2010 at 3:36 am

    hi all,

    if i have a issue file like:

    101 add open vish iuo

    if exit and login again i should get the increment of the first field like

    102 add open vish iuo

    mounika October 27, 2010 at 10:40 pm

    its simply superb to understand

    its is very useful for the beginning learners and its is very help in exams time also

    so guys read and enjoy wd the unix

    Lynda Zhang November 17, 2010 at 12:36 pm

    This is very help. How about if I want to print the salary seperated by commas, e.g. 2,000

    instead of 2000

    Ikem December 30, 2010 at 8:22 pm

    converted by Web2PDFConvert.com

    http://www.web2pdfconvert.com/?ref=PDFhttp://www.web2pdfconvert.com/?ref=PDF
  • 8/3/2019 Awk Introduction Tutorial 7 Awk Print Examples

    8/10

    18

    19

    20

    21

    22

    23

    24

    25

    Youve made a little typo:

    > Number of employees in _Tehcnology_ Dept = 3

    sudha February 2, 2011 at 12:38 pm

    vary v ary healpul to every one

    eben March 27, 2011 at 11:28 pm

    Its very useful for beginers like me

    kalandar April 7, 2011 at 8:58 am

    Hi,

    I found this article to be very useful. Anybody who wants to know what an awk is , this will give

    a fair idea. Looking forward to similar articles on other topics of unix.

    Thanks

    Bhagyaraj April 24, 2011 at 1:38 am

    Hi,

    Good,

    Please try teach in Youtube to try differntly.

    It will be more success.

    Keep it up,

    I need to take an exam on awk, let me see how much I can successed.

    kernelkid June 10, 2011 at 6 :48 am

    very simple and easy to understand, thanks a lot, it help me a lot

    liju June 14, 2011 at 3:05 am

    good simple article

    Marija June 30, 2011 at 9 :03 am

    I have read few geekstuff articles until now, explanations provided are the best I have ever seen

    so far! Great job Thanks a lot

    Muslim July 19, 2011 at 12:02 pm

    hi,

    i have the question that how to using print command awk to sort or transpose this data from

    many coloums to 2 columns only

    #input file

    NAME JAN FEB MARCH APRIL MAY JUNE JULY

    - - - BEN 5,000 6,000 7,000 8,000 6,500 7,500 9,000

    YONG 4,000 5,500 6,000 5,800 7,000 8,000 8,5000

    # output should be as below.

    BEN 5,000

    converted by Web2PDFConvert.com

    http://www.web2pdfconvert.com/?ref=PDFhttp://www.web2pdfconvert.com/?ref=PDF
  • 8/3/2019 Awk Introduction Tutorial 7 Awk Print Examples

    9/10

    26

    27

    BEN 6,000

    BEN 7,000

    BEN 8,000

    BEN 6,500

    BEN 7,500

    BEN 9,000

    YONG 4,000

    YONG 5,500

    YONG 6,000

    YONG 5,800YONG 7,000

    YONG 8,000

    YONG 8,5000

    Anyone can help.thanks

    @muss

    nails carmody August 9, 2011 at 1:08 pm

    I know its late, but

    #!/bin/bash

    awk {

    # skip the first two lines

    if (NR == 1 || NR == 2)

    continue

    for(i=2; i

  • 8/3/2019 Awk Introduction Tutorial 7 Awk Print Examples

    10/10

    P R E V I O U S P O S T :Can You Top This? 15 Practical Linux Top Command Examples

    N E X T P O S T :6 Steps for Minimal Ubuntu Installation Using debootstrap

    About The Geek Stuff

    My name is Ramesh Natarajan. I

    will be posting instruction guides,

    how-to, troubleshooting tips and

    tricks on Linux, database, hardware,security and web. My focus is to

    write articles that will either teach

    you or help you resolve a problem.

    Read more about Ramesh Natarajan and the blog.

    Support Us

    Support this blog by purchasing one of my

    ebooks.

    Bash 101 Hacks eBook

    Sed and Awk 101 Hacks eBook

    Vim 101 Hacks eBook

    Nagios Core 3 eBook

    Contact Us

    Email Me : Use this Contact Form to get in touch

    me with your comments, questions or

    suggestions about this site. You can also simply

    drop me a line to say hello!.

    Follow us on Twitter

    Become a fan on Facebook

    Copyright 20082011 Ramesh Natarajan. All rights reserved | Terms of Se rvice |Advertise

    http://www.eyewonderlabs.com/ct2.cfm?ewbust=1324612802139&guid=lOL4GXW1naOGYD2fgr6BOf&ewadid=170875&eid=1584051&file=failover.jpg&pnl=MainBanner&type=0&name=Clickthru-failover&num=1&time=0&diff=0&clkX=&clkY=&click=http%3A//ad.doubleclick.net/click%253Bh%253Dv8/3be6/3/0/%252a/o%253B250243195%253B0-0%253B0%253B38965644%253B3454-728/90%253B45547099/45564577/1%253Bu%253D%252Cidgt-10322453329_1324612777%252C1248cd4c1400c8c%252Ccareers%252Cax.170%253B%257Eaopt%253D2/1/bc/0%253B%257Esscs%253D%253fhttp%3A//ad.doubleclick.net/clk%3B245509256%3B72274144%3Bihttp://www.thegeekstuff.com/advertisehttp://www.thegeekstuff.com/copyrighthttp://www.thegeekstuff.com/2010/01/awk-introduction-tutorial-7-awk-print-examples/http://www.facebook.com/thegeekstuffhttp://twitter.com/thegeekstuffhttp://www.thegeekstuff.com/contacthttp://www.thegeekstuff.com/nagios-core-ebook/http://www.thegeekstuff.com/vim-101-hacks-ebook/http://www.thegeekstuff.com/sed-awk-101-hacks-ebook/http://www.thegeekstuff.com/bash-101-hacks-ebook/http://www.thegeekstuff.com/about/http://www.thegeekstuff.com/2010/01/debootstrap-minimal-debian-ubuntu-installation/http://www.thegeekstuff.com/2010/01/15-practical-unix-linux-top-command-examples/