|
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.txtLet 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.txtOnce 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.txtApples 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 |
|||
|
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 |
|||
|
12-19-2009, 09:17 PM
Post: #3
|
|||
|
|||
|
RE: BASIC AWK example
yea it is perfect for grabbing data by columns and rows
|
|||
|
« Next Oldest | Next Newest »
|






