Awk Introduction Tutorial - 7 Awk Print Examples
Awk Introduction Tutorial - 7 Awk Print Examples
m A
b e
oF e
ur B
te o
e A r o k
B h B
e i l
vs o
e t g
C o
o f
tt
ha
ec
Ads by Google
Linux Tutorial
aiooitrnk ru otI tn d A T uw c
by S A
S Ion K A J L A N U A R Y 6 , 2 0 1 0
t ee T w
Like
15
RSS
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.
Search
Syntax:
converted by Web2PDFConvert.com
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.
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
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.
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
Awk Example 2. Print the lines which matches with the pattern.
$ awk '/Thomas/ > /Nisha/' employee.txt 100 Thomas Manager Sales 400 Nisha Manager Marketing
Top 5 Best System Monitoring Tools Top 5 Best Linux OS Distributions How To Monitor Remote Linux Host using Nagios 3.0
$5,000 $9,500
Awk Introduction Tutorial 7 Awk Print Examples How to Backup Linux? 15 rsync Command Examples
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.
The Ultimate Wget Download Guide With 15 Awesome Examples Top 5 Best Linux Text Editors Packet Analyzer: 15 TCPDUMP Command Examples
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
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.
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 400 Nisha Manager Marketing 500 Randy DBA Technology $7,000 $9,500 $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.
converted by Web2PDFConvert.com
Operator ~ is for comparing with the regular expressions. If it matches the default action i.e print whole line will be performed.
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.
converted by Web2PDFConvert.com
t ee T w
Like
15
Share
Comment
Tags: Awk ACTION 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
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
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
converted by Web2PDFConvert.com
Knusper
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
Thanks for posting a tutorial on awk with illustrated examples. I Will be expecting other articles on awk
Ramesh Natarajan
@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 very nice comment. Im glad you liked this article. @Knusper, Thanks for following us on twitter.
thalafan
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 Amaral
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
avinash
10
converted by Web2PDFConvert.com
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
11
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
12
Hello forum members, Thanks for AWK tutorials ,it was very help ful to me.
avinash
@ vikas thanks you
13
wish
14
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
15
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
16
This is very help. How about if I want to print the salary seperated by commas, e.g. 2,000 instead of 2000
Ikem
17
converted by Web2PDFConvert.com
sudha
18
eben
19
kalandar
20
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
Hi,
21
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
22
liju
23
Marija
24
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
hi,
25
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
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,800 YONG 7,000 YONG 8,000 YONG 8,5000 Anyone can help.thanks @muss
nails carmody
26
I know its late, but #!/bin/bash awk { # skip the first two lines if (NR == 1 || NR == 2) continue for(i=2; i<=NF; i++) printf("%s %s\n", $1, $i) } ' datafile.txt Nice site! I learned some new things about sed I didn't know.
Sudhanshu
27
Leave a Comment
Name E-mail Website
Submit
converted by Web2PDFConvert.com
P N
R E
E X
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
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
Copyright 20082011 Ramesh Natarajan. All rights reserved | Terms of Service | Advertise
converted by Web2PDFConvert.com