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
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