Using Awk to Print Select Output

(1 vote, average 5.00 out of 5)

Awk is a very powerful utility, but its syntax is less than intuitive to a beginner. Here are a few common but very powerful things you can do with awk:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# print line 2 of the file
awk 'NR == 2' myFile.txt
 
# print column 2 of the file
awk '{print $2}' myFile.txt
 
# add up column 2
awk '{s += $4} END {print s}'
 
# print all the lines with more than 80 columns
awk 'length($0) > 80' myFile.txt
 
# print the number of lines in the file
awk 'END { print NR }' myFile.txt
 
# count the number of lines where column 2 is greater than column 1
awk '$2 > $1 {print i + "1"; i++}' myFile.txt
Partner Links:
Last Updated on Sunday, 18 July 2010 12:38  
Related Articles

» How to install Adobe Photoshop CS5 In Ubuntu Linux using WINE

These instructions were tested with Adobe Photoshop CS5.1 (the 30-day trial from Adobe's website) under Ubuntu 11.10 using WINE 1.4-rc5. I was not able to get the installer to work, however the method of installing on Windows and then copying over did work (see ). The directories I copied were:C:\Program Files\Adobe\ C:\Program Files\Common Files\Adobe C:\Program Files (x86)\Adobe C:\Program Files (x86)\Common Files\Adobe C:\Users\All Users\Application Data\Adobe\CS5Note: you must copy each...

» Using Emacs to Insert or Remove Block Comments

When using some languages that don't support block comments (such as or ), it can be pretty frustrating trying to comment out large portions of code.  Fortunately emacs provides an interface which can be used to insert or remove text in multiple lines called 'rectanges'.  Rectangles are a hugely powerful tool that can be used for much more than just block comments, but for the purpose of this article we'll focus solely on how to use them for commenting out multiple lines of...

» Introduction to LaTeX

LaTeX is a self-described document preparation system that is often used for research papers or other technical publications.  It is, in essence, an extremely useful computer language that interprets code and produces a well-formatted document.  It can greatly increase productivity due to the ease of formatting, and other nicities like automatic figure and reference numbering and easy mathematical representation.  Unfortunately, it also have a very steep learning curve. ...