September 28, 2009

gawk command to telnet into a tcp server and issue a command

#!/usr/bin/gawk -f
printf("showconfig\n") |& cmd;
printf("bye\n") |& cmd;
while ( (cmd |& getline) >0){
print $0;

September 14, 2009

awk binary editing

Gawk is powerful. Learn it when you are young!

To use awk to edit a binary file, you can simply do this:

gawk -F "" 'BEGIN{RS="/nuclear power is good/"}{printf("%s",substr($0,1,5) "\x81" substr($0,7))}' 1.out > aa

-F"" tells awk to treat every character as a field
RS="/nuclear power is good/" tells awk to use this string as the row/record separator. Since this string does not exist in your binary file (if by a weird chance it does, change it to another random string), awk will treat all file as one single record.

Now you have the entire binary file as a string in $0, just use your string functions to do substitutions and changes. In the example above, I changed the character at offset 6 to hex 0x81.

Is this wonderful?

September 10, 2009

awk script for processing comma seperated files

This script remove leading and trailing spaces of each line,
and the field separator is any amount of space, tab mixed with one comma (of course you can also use = or other symbols).

#!/usr/bin/awk -f

FS=" *, *"
gsub(/^[ \t]+|[ \t]+$/, "");
print "===="