Login Register






Introduction to the AWK programming language filter_list
Author
Message
RE: Introduction to the AWK programming language #2
What you described here very well is actually not the main use of awk.

awk is a command line tool to extract data and scripts initially where only written to summarize some command line tasks, but not to have an interactive application that works on its own. The latter is just a side effect, because awk happens to be a turing complete language, but I wouldn't advice learning it to write applications.

This is one example usage on the command line to get the battery status as percentage (the pathes might be different):

Code:
awk '/last full capacity/ {c = $4} /remaining capacity/ {print int( $3/c * 100 )}' /proc/acpi/battery/BAT1/info /proc/acpi/battery/BAT1/state

Here you see the typical structure. First comes the regexp, afterwards the block where you state what to do with the data. New statements are on a new line and the input file(s) come last. So here you have two statements, the first reads the maximum capacity of the battery in the info file and the second reads the remaining capacity and calculates and prints the percentage.

The manual summarizes the general structure like this:

Quote:Syntactically, a rule consists of a pattern followed by an action. The action is enclosed in curly braces to separate it from the pattern. Newlines usually separate rules. Therefore, an awk program looks like this:

pattern { action }
pattern { action }
...

That's what actually would be useful to explain in my opinion, how to utilize awk for the command line to extract data.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply





Messages In This Thread