Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
BASIC AWK example
12-19-2009, 08:03 AM
Post: #1
BASIC AWK example
check this link out about printing output in awk
http://www.math.utah.edu/docs/info/gawk_7.html

You can either pipe lines to awk or you can it a file. Here I have made a file and attached it. It is called foods and this is how it looks:

Apples Oranges Candy
8 2 3
9 8 7
2 9 4
6 8 4
3 4 6

By default awk uses spaces a delimiters. No matter how many paces you have in between letters it will count it as 1 space. You can change the delimiter by using FS variable. Example of how it would look is FS = 'er'. Every time er is found it makes a split. Since I am doing this example from interactively I can give it the parameter -F and change the file separator also. I am only using the space file separator so I don't need to implement any of these actions.

To display a files entire contents you can do it in 2 ways
Here is the first way:
Code:
awk '{ print }' foods.txt

Let me explain how this works. First of all when doing a awk command you want to put everything in single quotes that way BASH won't treat any characters as special. The next step is to put your all your commands inside the { }. This groups your commands into the main body of awk. I am not going to explain anything outside the main body in this tutorial. The print command prints each whole line to the standard output from the foods.txt file

Here is the second way of doing this
Code:
awk '{ print $0 }' foods.txt
The $0 by defaults prints the entire line. If I was do do $1 it would just print the first column or if I did $3 is would just print the 3rd column. In this next example I will show you to print only the 1st and 3rd column.

Once again I am using the foods.txt
Apples Oranges Candy
8 2 3
9 8 7
2 9 4
6 8 4
3 4 6

I need to know the founds for only apples and candy because my boss doesn't care for the oranges count. This is how I would do this. Here is uses the printf command for spaces. If you ever programmed in c c++ or used javas print f you know exactly whats going on
Code:
awk '{ printf "%s %10s\n", $1, $3 }' foods.txt
This is the output:
Apples Candy
8 3
9 7
2 4
6 4
3 6

if I just wanted to print apples on the screen I would just do
awk '{print $1}' foods.txt.

It is kinda hard to show examples in awk without teaching you the language itself. For more information
Code:
man awk


Attached File(s)
.zip  foods.txt.zip (Size: 215 bytes / Downloads: 1)
Visit this user's website Find all posts by this user
Quote this message in a reply
12-19-2009, 09:13 PM
Post: #2
RE: BASIC AWK example
Very interesting. I didn't work in awk ever. It seems that it has some functions same as in C.

"I dont know with what weapons World War 3 will be fought with, but i know World War 4 will be fought with stones and sticks" - Albert Einstein
Visit this user's website Find all posts by this user
Quote this message in a reply
12-19-2009, 09:17 PM
Post: #3
RE: BASIC AWK example
yea it is perfect for grabbing data by columns and rows
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: