awk-wordness simplify and speed up your code with awk

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Message
Author
Bruce B

#16 Post by Bruce B »

goingnuts wrote:Cant get it running (named the script "test"):
We aren't supposed to name executables 'test', because test already exists.

~

goingnuts
Posts: 932
Joined: Sun 07 Dec 2008, 13:33
Contact:

#17 Post by goingnuts »

technosaurus wrote:... didn't you already do one using only xlib and xpm though?
Yes - a single applet (pmmon) and one with place for 5 (pmmon5).link
Bruce B wrote:We aren't supposed to name executables 'test', because test already exists.
I will remember that - in this case I do not think that was the problem though...

starhawk
Posts: 4906
Joined: Mon 22 Nov 2010, 06:04
Location: Everybody knows this is nowhere...

#18 Post by starhawk »

Somewhat off-topic, Technosaurus, did you get my PM? I'm posting here because I think you're likely to see it ;)

penguinpowerppp
Posts: 5
Joined: Sat 14 Apr 2012, 23:49

#19 Post by penguinpowerppp »

goingnuts wrote:
technosaurus wrote:... didn't you already do one using only xlib and xpm though?
Yes - a single applet (pmmon) and one with place for 5 (pmmon5).link
...
I'll have to take a look and see if I can adapt my sit arg parsing logic to it so that it can be unlimited ... which reminds me, I found some nicer fonts in aicon to add to my txt2xpm

Edit ... oops my wife was logged on -technosaurus

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#20 Post by technosaurus »

adding a template:

Code: Select all

#!/bin/awk -f
#FILENAME (name of current file) $FILENAME (contents of current file)
#NF number of fields, $NF last field
#NR line number in all files      #FNR line number in current file
#ORS (default is "\n")            #RS  (default is "\n")
#OFS (default is " ")            #FS (default is [ \t]*)
#system(command) run a command      #close(filename) close(command)
#ARGC, ARGV similar to C, but skips some stuff
#IGNORECASE (default is 0) set to non-0 or use toupper() or tolower()
#ENVIRON array of env vars ex. ENVIRON["SHELL"] (equivalent of $SHELL)
#getline var < file ... close file or command | getline var
#index(haystack, needle) find needle in haystack
#length(string)
#match(string, regexp) returns where the regex starts, or 0
#RLENGTH length of /match/ substring or -1
#RSTART position where the /match/ substring starts, or 0
#split(string, array, fieldsep) split string into an array separated by fieldsep
#printf(format, expression1,...) print format-ted replacing %* with expressions
#%{c,d/i,e,f,g,o,s,x,X,%} char, decimal int, exp notation, float, shortest of
#   exp/float, octal, string, hex int, capitalized hex int, a '%' character
#sprintf(format, expression1,...) store printf in a variable
#sub(regexp, replacement, target) replace first regex with replacement in target
#gsub(regexp, replacement, target) like gsub but for all regex in target
#substr(string, start, length)get substring of string from start to start+length
#print > /dev/stdin, /dev/stdout, /dev/stderr, /dev/fd/# or filename
#output can be piped like print $0 | command
#comparisons <,>,<=,>=,==,!=,~,!~,in use && for AND, || for OR, ! for NOT
#   (~ is for regexp and "in" looks for subscript in array)
#/word/{...} like if match(...) {...} equivalent of grep
#(condition) ? if-true-exp : if-false-exp or use if (condition){}
#math +,-,*,/,%,**,log(x),exp(x),,sqrt(x),cos(x),sin(x),atan2(y,x),
#rand(),srand(x),time(),ctime()
#
#function name (parameter-list) {
#     body-of-function
#}

BEGIN {
#actions that happen before any files are read in
}
#
{
#actions to do on files
}
#
END {
#actions to do after all files are done
}
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#21 Post by technosaurus »

here is an example of how to give awk sane and useable "arguments" (see the argc and argv portions... note that "-" is stdin, so the arguments are read first followed by the rest of the files)

The rest of the example demonstrates how to store a 2 dimensional associative (named) array and iteratively print it after all files are processed

Code: Select all

#useage: status [Options]
#Options
#Name:State:Tgid:Pid:PPid:TracerPid:Uid:Gid:FDSize:Groups:VmPeak
#VmSize:VmLck:VmPin:VmHWM:VmRSS:VmData:VmStk:VmExe
#VmLib:VmPTE:VmSwap:Threads:SigQ:SigPnd:ShdPnd:SigBlk:SigIgn
#SigCgt:CapInh:CapPrm:CapEff:CapBnd:Seccomp:Cpus_allowed
#Cpus_allowed_list:voluntary_ctxt_switches:nonvoluntary_ctxt_switches
status(){
echo $@ | awk 'BEGIN{FN=0}
	FNR==1{FN++}
	FN==1{
		argc=NF
		for(j=0;j<NF;j++){
			argv[j]=$(j+1)
			field[FN][$(j+1)]=$(j+1)
		}
	}
	FN>1{
		title=substr($1,0,length($1)-1)
		$1=""
		field[FN][title]=$0
	}
	END{
		for(i=1;i<FN;i++){
			for(j=0;j<argc;j++){
				printf "%-20s\t", field[i][argv[j]]
			}
			printf "\n"
		}
	}
' - /proc/*/status
}
and here is a bonus to get the init cmdline ... in case you were running in pfix=ram or something

Code: Select all

awk 'BEGIN{RS="\0"}{print}' /proc/1/environ
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#22 Post by seaside »

technosaurus wrote:here is an example of how to give awk sane and useable "arguments" (see the argc and argv portions... note that "-" is stdin, so the arguments are read first followed by the rest of the files)

The rest of the example demonstrates how to store a 2 dimensional associative (named) array and iteratively print it after all files are processed

Code: Select all

#useage: status [Options]
#Options
#Name:State:Tgid:Pid:PPid:TracerPid:Uid:Gid:FDSize:Groups:VmPeak
#VmSize:VmLck:VmPin:VmHWM:VmRSS:VmData:VmStk:VmExe
#VmLib:VmPTE:VmSwap:Threads:SigQ:SigPnd:ShdPnd:SigBlk:SigIgn
#SigCgt:CapInh:CapPrm:CapEff:CapBnd:Seccomp:Cpus_allowed
#Cpus_allowed_list:voluntary_ctxt_switches:nonvoluntary_ctxt_switches
status(){
echo $@ | awk 'BEGIN{FN=0}
	FNR==1{FN++}
	FN==1{
		argc=NF
		for(j=0;j<NF;j++){
			argv[j]=$(j+1)
			field[FN][$(j+1)]=$(j+1)
		}
	}
	FN>1{
		title=substr($1,0,length($1)-1)
		$1=""
		field[FN][title]=$0
	}
	END{
		for(i=1;i<FN;i++){
			for(j=0;j<argc;j++){
				printf "%-20s\t", field[i][argv[j]]
			}
			printf "\n"
		}
	}
' - /proc/*/status
}
and here is a bonus to get the init cmdline ... in case you were running in pfix=ram or something

Code: Select all

awk 'BEGIN{RS="\0"}{print}' /proc/1/environ
technosaurus,

This looks really handy.. However, this is what I get -

Code: Select all

 status Name
awk: cmd. line:7:          field[FN][$(j+1)]=$(j+1)
awk: cmd. line:7:                   ^ syntax error
awk: cmd. line:7:          field[FN][$(j+1)]=$(j+1)
awk: cmd. line:7:                          ^ syntax error
awk: cmd. line:13:       field[FN][title]=$0
awk: cmd. line:13:                ^ syntax error
awk: cmd. line:18:             printf "%-20s\t", field[i][argv[j]]
awk: cmd. line:18:                                       ^ syntax error
awk: cmd. line:18:             printf "%-20s\t", field[i][argv[j]]
awk: cmd. line:18:                                               ^ syntax error
Any ideas on what might be off?

Regards,
s

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#23 Post by technosaurus »

seaside wrote:This looks really handy.. However, this is what I get -

Code: Select all

 status Name
awk: cmd. line:7:          field[FN][$(j+1)]=$(j+1)
awk: cmd. line:7:                   ^ syntax error
awk: cmd. line:7:          field[FN][$(j+1)]=$(j+1)
awk: cmd. line:7:                          ^ syntax error
awk: cmd. line:13:       field[FN][title]=$0
awk: cmd. line:13:                ^ syntax error
awk: cmd. line:18:             printf "%-20s\t", field[i][argv[j]]
awk: cmd. line:18:                                       ^ syntax error
awk: cmd. line:18:             printf "%-20s\t", field[i][argv[j]]
awk: cmd. line:18:                                               ^ syntax error
Any ideas on what might be off?

Regards,
s
I used the awk included in Precise, I still haven't tested it with busybox awk, maybe it doesn't support 2-dimensional arrays? ... I guess It doesn't even need "field", that was just a random choice so the 2nd dimension can be the array name instead (so there would be an array named for each "Option") ... and as a bonus it would simplify the script.

BTW, the second script could be really useful for things like shutdown and install/loader scripts, but IDK who is writing those these days (sfs loaders, remasters, etc...) for example if it has pfix=ram,copy you would assume they wanted to load it in place without copying as the default action
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

Post Reply