User:Ks sudhir/Awk
From Wikipedia, the free encyclopedia
Sample awk scripts
awk 'BEGIN{x=0;n=1000;}{if (length($0) > x ) x = length($0);if (length($0)<n) n= length($0);}END{print "Max=",x," Min=",n;}' /tmp/my_element_file
awk -v theDate=$CURDATE -v tmpDir=${CLEANUPTEMPDIR} '{ # split the config line into different fields count = split($0,a," "); if (($0 !~ "^#") && (count >= 4)) # ignore if else { a[3] = "+" a[3]; name = a[2]; gsub(/\*/,"\\*",name); execCmd = ""; findCmd = sprintf("find %s -name %s -mtime %s ! -type d |sort >%s/t; if (a[4] == "mv") { if (count != 5) {print "ERROR incorrect args->",$0;break;} execCmd = sprintf("%s |xargs -i mv {} %s" ,findCmd,a[5]); } else if (a[4] == "rm") { if (count != 4) {print "ERROR incorrect args->",$0;break;} execCmd = sprintf("%s |xargs -i rm -f {}",findCmd); } else if (a[4] == "tar") { if (count < 5) {print "ERROR incorrect args->",$0;break;} tarFile = sprintf("%s/%s_%s.tar",a[5],a[6],theDate); if (a[6] == "") a[6] = "myproject"; findCmd = sprintf("cd %s;find %s -mtime %s ! -type d " \ ,a[1],a[2],a[3]); findRmCmd = sprintf("%s -exec rm -f {} \;",findCmd); execCmd = sprintf("cd %s;tar cf %s `%s` && gzip -f %s && %s" \ ,a[1],tarFile,findCmd,tarFile,findRmCmd); } else print "ERROR unknown cmd->",$0; if (execCmd == "" ) break; print "Cmd->",execCmd; print("Processing files :"); retVal = system(findCmd); if (retVal == 0) { retVal = system(execCmd); if (retVal != 0) print "ERROR running command :",retVal; } } }' $CONFIGFILE >> $LOGFILE 2> $ERRFILE