Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Wed 30 Jul 2014, 05:38
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
awk-wordness simplify and speed up your code with awk
Post new topic   Reply to topic View previous topic :: View next topic
Page 2 of 2 [23 Posts]   Goto page: Previous 1, 2
Author Message
Bruce B


Joined: 18 May 2005
Posts: 11080
Location: The Peoples Republic of California

PostPosted: Wed 23 May 2012, 11:56    Post subject:  

goingnuts wrote:
Cant get it running (named the script "test"):


We aren't supposed to name executables 'test', because test already exists.

~

_________________
New! Puppy Linux Links Page
Back to top
View user's profile Send private message 
goingnuts

Joined: 07 Dec 2008
Posts: 779

PostPosted: Wed 23 May 2012, 14:07    Post subject:  

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...
Back to top
View user's profile Send private message Visit poster's website 
starhawk

Joined: 22 Nov 2010
Posts: 2777
Location: Everybody knows this is nowhere...

PostPosted: Wed 23 May 2012, 17:23    Post subject:  

Somewhat off-topic, Technosaurus, did you get my PM? I'm posting here because I think you're likely to see it Wink
_________________
Loving X-Slacko 1.1! Get the PetGet patch on pg8; it's important.
Next system will be an HP MOCA-AR + Core2Duo in an innovative case... if I ever get off my butt and build it Razz
Back to top
View user's profile Send private message 
penguinpowerppp

Joined: 14 Apr 2012
Posts: 5

PostPosted: Fri 25 May 2012, 22:40    Post subject:  

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
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4284

PostPosted: Mon 18 Feb 2013, 15:01    Post subject:  

adding a template:

Code:
#!/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
}

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4284

PostPosted: Wed 08 May 2013, 09:19    Post subject:  

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:
#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:
awk 'BEGIN{RS="\0"}{print}' /proc/1/environ

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
seaside

Joined: 11 Apr 2007
Posts: 886

PostPosted: Thu 09 May 2013, 11:19    Post subject:  

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:
#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:
awk 'BEGIN{RS="\0"}{print}' /proc/1/environ

technosaurus,

This looks really handy.. However, this is what I get -
Code:
 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
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4284

PostPosted: Thu 09 May 2013, 17:13    Post subject:  

seaside wrote:
This looks really handy.. However, this is what I get -
Code:
 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

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 2 of 2 [23 Posts]   Goto page: Previous 1, 2
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0922s ][ Queries: 13 (0.0048s) ][ GZIP on ]