Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk script improvement

Status
Not open for further replies.

pb0y

Technical User
Feb 27, 2002
16
US
I am running the following programs within one script. It works, however I would like a more professional way than to than to run 5 separate instances of awk. Any insight would be greatly appreciated.
Thanks.

nawk '/TABLE1/, /:TABLE2/' file | nawk -v X=$1 'BEGIN {FS=","} $3==X {len="%-8s"

printf "HEAD1\n"
printf len, "FIELD1"
printf len, "FIELD2"
printf len, "FIELD3"
printf len, "FIELD4"
printf len, "FIELD5"
printf "\n"
printf len"\n", $0, "\n"
printf "\n"}'
nawk '/TABLE3/, /TABLE4/' file |nawk -v X=$1 'BEGIN {FS=","} $3==X {len="%-8s"

printf "HEAD2\n"
printf len, "FIELDA"
printf len, "FIELDB"
printf len, "FIELDC"
printf len, "FIELDD"
printf len, "FIELDE"
printf len, "FIELDF"
printf "\n"
printf len"\n", $0, "\n"
printf "\n"}'

VAR=`nawk '/TABLE1/, /TABLE2/' file | nawk -v X=$1 'BEGIN {FS=","} $3==X {print $1}'`

nawk '/TABLE5/, /TABLE6/' file |nawk -v X=$VAR 'BEGIN {FS=","} $1==X {len="%-8s"

printf "HEAD3\n"
printf len, "FIELD"
printf len, "VALUE"
printf "\n"
printf len"\n", $0, "\n"
printf "\n"}'
nawk '/TABLE7/, /TABLE8/' file |nawk -v X=$VAR 'BEGIN {FS=","} $1==X {len="%-8s"

printf "HEAD4\n"
printf len, "ENTITY"
printf len, "VALUE"
printf "\n"
printf len"\n", $0, "\n"
printf "\n"}'
 
More info:

I am scanning certain tables in a comma-delimited file for some number (shell variable $1) in the 3 field of each record and then getting the index number of that record (field $1) when it finds a match. Then printing the output of the record under a table heading for each field. However, the last two records do not have a 3rd field, so I need to locate them by refering to the 1st field of one of the previous record matches. I hope this helps.

Thanks.
 
Could you try the following ?
It will not work if the TABLE1 ... TABLE2 lines are after the TABLE5 ... TABLE8 lines because the variable VAR will not be set in this case.


Code:
awk -v X=$1 '
BEGIN {
  FS = ",";
  len = "%-8s";
  VAR="undefined-value";
}

/TABLE1/,/TABLE2/ {
  if (X == $3) {
    VAR = $1;
    printf "HEAD1\n";
    printf(len len len len len "\n", 	   "FIELD1", "FIELD2", 	   "FIELD3", "FIELD4", "FIELD5");
    printf("%s\n\n", $0);
  }
}

/TABLE3/,/TABLE4/ {
  if (X == $3) {
    printf "HEAD2\n";
    printf(len len len len len len "\n", 	   "FIELDA", "FIELDB", 	   "FIELDC", "FIELDD", 	   "FIELDE", "FIELDF");
    printf("%s\n\n", $0);
  }
}

/TABLE5/,/TABLE6/ {
  if (VAR == $1) {
    printf "HEAD3\n";
    printf(len len "\n", 	   "FIELD", "VALUE");
    printf("%s\n\n", $0);
  }
}

/TABLE7/,/TABLE8/ {
  if (VAR == $1) {
    printf "HEAD4\n";
    printf(len len "\n", 	   "ENTITY", "VALUE");
    printf("%s\n\n", $0);
  }
}'[code]
 
works great! thanks!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top