Awk - A nifty little tool for text manipulation and more.
In this thread, I shall show some examples on how one can use this tool for easy file/text manipulation in a Windows batch environment. (if you can download 3rd party tools not installed by default). This is mostly for beginners to using awk or looking for tools to parse strings/text.
The syntax for awk is simple
Code: Select all
pattern { action }
For example, to print a file
Code: Select all
C:\> awk "{print}" myFile.txt
In the above example, the "action" is "print". This is the equivalent of the command
Code: Select all
type myFile.txt
The cmd.exe on windows doesn't like single quotes, so we have use double quotes for the "action" part.
awk has a "BEGIN" and "END" pattern block. The "BEGIN" pattern only executes once before the first record is read by awk. For example, you can initialize variables inside this block
Code: Select all
awk "BEGIN{a=10} ....." myFile.txt
or just do some calculation (simple calculator)
Code: Select all
C:\>awk "BEGIN { print 1+2 } "
3
Likewise the "END" pattern is executed once only after all the records in the file has been read. For example, you want to print the last line of the file
Code: Select all
C:\> more myFile.txt
C:\original\1\2\3
C:\original\1\2\4
C:\original\1\2\5
C:\original\1\2\36
test
C:\>awk "END{ print $0} " myFile.txt
test
"$0" means the current line/record.
to be continued...
-berserker